mask, catch, myThreadId, throwTo

Felipe Almeida Lessa felipe.lessa at
Mon Apr 15 21:57:46 CEST 2013


I have some code that is not behaving the way I thought it should.

The gist of it is

  sleeper =
    mask_ $
    forkIOWithUnmask $ \restore ->
      forever $
        restore sleep `catch` throwBack

  throwBack (Ping tid) = myThreadId >>= throwTo tid . Pong
  throwBack (Pong tid) = myThreadId >>= throwTo tid . Ping

Since (a) throwBack is executed on a masked state, (b) myThreadId is
uninterruptible, and (c) throwTo is uninterruptible, my understanding
is that the sleeper thread should catch all PingPong exceptions and
never let any one of them through.

However, that's not what I am seeing.  Running with -threaded and at
least -N2 I can reliably get sleeper to be killed.  Please see the
complete code at:

The expected result is 100 Pongs and 0 Pings.  Running with -N1 that's
what I get.  Running with -N2 I get a few Pongs and then 1 Ping.
Running with -N3 or more usually gives me just 1 Ping and not a single

I'm testing with GHC 7.4.1.  I've tried to download 7.7 but---either
because of or my ISP---I'm not being able to download it.

(Note that this problem is not academic as I've been bitten by it on
my production code.)

Thanks! =D


More information about the Glasgow-haskell-users mailing list