ANN: SafeSemaphore, Proposal: replace QSem/QSemN

Chris Kuklewicz haskell at
Sat Apr 9 01:53:57 CEST 2011

There is a leak in base.  A leak in the semaphores.  Drip, drip, drip.

I would like to announce a new package SafeSemaphore, at
My intent is for this to supersede the implementation of semaphore in
Control.Concurrent.QSem and Control.Concurrent.QSemN currently in the
base package.

This is needed because there is a quantity loss bug in QSem and QSemN.
Any thread blocked while waiting might be killed.  In this case a later
signal will pass quantity to the dead thread.  This quantity is never
received and is thus lost to the program.  This is logged in GHC's bug
tracker at

The MSem and MSemN modules in SafeSemaphore fix this problem and are
extended replacement for QSem/QSemN with what I think is an improved and
extended API.  Under the hood a MSem/MSemN only ever allocates three
MVars, instead of one MVar for every blocking waiter that QSem/QSemN

The related Haskell wiki page at
shows the included TestKillSem.hs file and its output.  This shows how
the QSem/QSemN bug can be triggered and shows that MSem/MSemN is immune
to this leak.

  Chris Kuklewicz

More information about the Libraries mailing list