Joey Hess: bracketing and async exceptions in haskell
I've been digging into async exceptions in haskell, and getting more
and more concerned. In particular, bracket seems to be often used in ways
that are not async exception safe. I've found multiple libraries with problems.
Here's an example:
withTempFile a = bracket setup cleanup a
where
setup = openTempFile "/tmp" "tmpfile"
cleanup (name, h) = do
hClose h
removeFile name
This looks reasonably good, it makes sure to clean up after itself even
when the action throws an exception.
But, in fact that code can leave stale temp files lying around.
If the thread receives an async exception when hClose is
running, it will be interrupted before the file is removed.
We normally think of bracket as masking exceptions, but it
doesn't prevent async exceptions in all cases.
See Control.Exception on "interruptible operations",
which can receive async exceptions even when other exceptions are masked.
It's a bit surprising, but hClose is such an interruptable operation,
because it flushes the write buffer. The only way to know is to
read the code.
It can be quite hard to determine if an operation is interruptable, since
it can come down to whether it retries a STM transaction, or uses a MVar
that is not always full. I've been auditing libraries and I often have
to look at code several dependencies away, and even then may not be sure
if a library has this problem.
- process's withCreateProcess could fail to wait on the process, leaving a zombie. Might also leak file descriptors?
- http-client's withResponse might fail to close a network connection. (If a MVar happened to be empty when it's called.) Worth noting that there are plenty of examples of using http-client to eg, race downloading two urls and cancel the slower download. Which is just the kind of use of an async exception that could cause a problem.
- persistent's withSqlPool and withSqlConn might fail to clean up, when used with persistent-postgresql. (If another thread is using the connection and so a MVar over in postgresql-simple is empty.)
- concurrent-output has some locking code that is not async exception safe. (My library, so I've fixed part of it, and hope to fix the rest.)
bracket
or onException or the like probably have this problem.
What can libraries do?
- Document whether these things are async exception safe. Or perhaps there should be an expectation that "withFoo" always is, but if so the Haskell comminity has some work ahead of it.
- Use
finally. Good mostly in simple situations; more complicated things would be hard to write this way.hClose h finally removeFile name
- Use
uninterruptibleMask, but it's a big hammer and is often not the right tool for the job. If the operation takes a while to run, the program will not respond to ctrl-c during that time. - May be better to run the actions in worker threads, to insulate
them from receiving any async exceptions.
bracketInsulated :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c bracketInsulated a b = bracket (uninterruptibleMask $ \u -> async (u a) >>= u . wait) (\v -> uninterruptibleMask $ \u -> async (u (b v)) >>= u . wait)
(Note use of uninterruptibleMask here in case async itself does an interruptable operation. My first version got that wrong.. This is hard!)
race or cancel or withAsync or the like, unless the thread is small
and easy to audit for these problems. Kind of a shame, since I had wanted to
be able to cancel a thread that is big and sprawling and uses all the
libraries mentioned above.
This work was sponsored by Jake Vosloo and Graham Spencer on Patreon.
I've written about what


Well, I'm sure many of you know "normal" XMPP clients such as Gajim or Dino on Linux, Conversations on Android or Siskin/Monal/ChatSecure on Apple IOS. There are plenty of other clients of course and maybe you used an XMPP client in the past without knowing it. For example Jitsi Meet is based on XMPP and you can still download the Jitsi Desktop client and use it as a full-featured multi-protocol client, e.g. for XMPP and SIP. In fact Jitsi Desktop is maybe the client that comes closest to Cisco Jabber as a chat/voice/video client. In fact I already connected Jitsi Desktop to Cisco CUCM/IM&P infrastructure, but of course you won't be able to use all those Cisco proprietary extensions, but you can see the benefit of open, standardized protocols such as XMPP and SIP: you are free to use any standard compliant client that you want.
So, while Jitsi supported voice/video calls for a long time, even before they focussed on Jitsi Meet as a WebRTC based conference service, Conversations added this feature last month, as already stated. This had a huge effect to the whole XMPP federation, because you need an XMPP server that supports XEP-0215 to make these audio/video calls work. The well-known
fully assembled chromebook duet
As announced in a few tweets leading up to it, I took the date of what would have been the annual R/Finance conference as an opportunity to hold the one-hour tutorial / workshop with introductory Rcpp material which I often present on the first morning preceding the conference as a self-organized webinar. The live-streaming worked actually reasonably well via
This weekend I had some pretty good uncle time. My eleven year old nephew (after beating me in a fair match of tennis) wanted to play with his Lego Mindstorms set. He never even touches it unless I am around to help him, but then he quiet enjoys. As expected, when I ask him what we should try to build, he tends to come up with completely unrealistic or impossible ideas, and we have to somehow reduce the scope to something doable.
This time round, inspired by their autonomous vacuum cleaner, he wanted to build something like that. That was convenient, because now I could sell him what I wanted to try to do, namely make the robot follow a wall at more or less constant distance, as a useful first step.
We mounted the infra red distance sensor at an 45 angle to the front-left, and then I built a very simple control loop measure the distance, subtract 40, apply a factor, and feed that into the steering input of the drive action, and repeat. I must admit that I was pretty proud to see just how well that very simple circuit worked: The little robot turned sharp corners in both directions, and otherwise drove nicely parallel and with constant distance to the wall.
I was satisfied with that achievement and would have happily ended it here before we might be disappointed by more ambitious and then failing goals.
But my nephew had not forgotten about the vacuum cleaner, and casually asked if I could make the robot draw an outline of its path, and thus of the room, on the display. Ok, where do I begin to explain just how unfeasible that is yes, it seemed one can draw to the display. But how should the robot know where it is? How far it turns? How far it went? This is programming by connecting boxes, and I would not expect such an interface to allow for complex logic (
I ve been less good at taking adequate typing breaks during the
lockdown and I ve become concerned about how much chording my left
hand does on its own during typical Emacs usage, with caps lock
rebound to control, as I ve had it for years.
I thought that now was as good a time as any to do something drastic
about this. Here are my rebindings:
It's (yet again!) been a while since I blogged last, sorry...
It's been over ten years since I started
in 
Where do I fit in? Pexip is a relatively small company with a very
flat setup in engineering, so that's a difficult question to answer!
I'll be starting working in the team developing and maintaining PexOS,
the small Linux-based platform on which other things depend. (No
prizes for guessing which distro it's based on!) But there's lots of
scope to get involved in all kinds of other areas as needs and
interests arise. I can't wait to get stuck in!
Although I'm no longer going to be working on Debian arm port
issues on work time, I'm still planning to help where I can. Let's see
how that works...
Not bad, but not Smokeping
Looks more like Smokeping!








### The blockade has ended! For some introduction..
Back in 2016, Telmex Mexico's foremost communications provider and,
through the brands grouped under the *Am rica M vil* brand, one of
Latin America's most important ISPs set up rules to block connecitons
to (at least) seven of Tor's *directory authorities* (*DirAuths*). We
believe they might have blocked all of them, in an attempt to block
connections from Tor from anywhere in their networks, but Tor is much
more resourceful than that so, the measure was not too effective.
Only... _Some_ blocking did hurt Telmex's users: The ability to play
an active role in Tor. The ability to host Tor relays at home. Why?
Because the *consensus protocol* requires relays to be reachable in
order to be measured from the network's *DirAuths*.
### Technical work to prove the blocking
We dug into the issue as part of the work we carried out in the
project I was happy to lead between 2018 and 2019, *UNAM/DGAPA/PAPIME
PE102718*. In March 2019, I presented a paper titled [Distributed
Detection of Tor Directory Authorities Censorship in
Mexico](https://www.thinkmind.org/index.php?view=article&articleid=icn_2019_6_20_38010)
([alternative download](http://ru.iiec.unam.mx/4538/) in the [Topic on
Internet Censorship and Surveillance (TICS) track](https://tics.site/)
of the XVIII International Conference on Networks.
Then... We had many talks inside our group, but nothing seemed to move
for several months. We did successfully push for increasing the number
of Tor relays in Mexico (we managed to go from two to eleven stable
relays not much in absolute terms, but quite good relatively, even
more considering most users were not technically able to run one!)
