Ticket #6153 (closed bug: fixed)

Opened 13 months ago

Last modified 12 months ago

writeChan not properly protecting again async exceptions

Reported by: klao Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 7.4.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

In the current code of writeChan from Control.Concurrent.Chan:

writeChan (Chan _ writeVar) val = do
  new_hole <- newEmptyMVar
  modifyMVar_ writeVar $ \old_hole -> do
    putMVar old_hole (ChItem val new_hole)
    return new_hole

if an async exception arrives "between" putMVar old_hole and return, the channel will be left in inconsistent state: the writeVar will be reverted to its original value, but old_hole would remain filled in. Thus all subsequent writers will block on this Chan indefinitely.

The proper solution is to just mask the whole operation. And it's OK, as 'putMVar old_hole' cannot ever block. I attach a patch for this.

Attachments

Chan.diff Download (1.1 KB) - added by klao 13 months ago.
patch for writeChan
writeChan_deadlock.hs Download (0.6 KB) - added by klao 13 months ago.

Change History

Changed 13 months ago by klao

patch for writeChan

Changed 13 months ago by klao

Changed 13 months ago by klao

Also, I'm attaching small program that demonstrates this issue. If you compile it with -O0 in a way that Control.Concurrent.Chan is also compiled with -O0 (for example by copying Control/Concurrent/Chan.hs and uncommenting import Chan instead for Control.Concurrent.Chan), then the deadlock is consistently reproduced.

Changed 12 months ago by simonmar

  • status changed from new to closed
  • difficulty set to Unknown
  • resolution set to fixed
Note: See TracTickets for help on using tickets.