unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* System asyncs and mutexes: a combination prone to deadlocks
@ 2013-08-20  3:00 Mark H Weaver
  2013-08-20  3:20 ` Chaos Eternal
  2013-08-23 21:18 ` Mark H Weaver
  0 siblings, 2 replies; 5+ messages in thread
From: Mark H Weaver @ 2013-08-20  3:00 UTC (permalink / raw)
  To: guile-devel

Hello all,

While working on making (ice-9 popen) thread-safe, I've discovered a
serious problem with system asyncs and mutexes.

System asyncs can run while mutexes are locked.  Asyncs can run
arbitrary scheme code, so of course mutexes will often be locked within
asyncs as well.  So what happens if an async tries to lock a mutex that
has already been locked by the same thread?  Deadlock, of course.

Recursive mutexes are not a solution.  They would avoid the deadlock,
but they would leave open the possibility of corrupted data structures,
because the async might be run while a data structure is in an
inconsistent state.  If the async tries to access that data structure,
things could get ugly.

In popen, there are data structures (the port table and the guardian)
that need to be locked both outside and within asyncs, so I addressed
the problem by blocking asyncs before grabbing the lock:

  (define-syntax-rule (with-popen-tables-locked e0 e ...)
    (call-with-blocked-asyncs
     (lambda ()
       (with-mutex popen-mutex e0 e ...))))

This prevents deadlock by this particular mutex, but what about all the
other mutexes used throughout Guile?

The deadlock I happen to be seeing during 'make check' is from the
'overrides_lock' in procprop.c, but there are scores of other mutexes
around the system that could cause the same problem.

It seems to me that system asyncs are a fundamentally flawed concept in
any system that uses mutexes.  They need to be run in a different thread
to prevent these deadlocks.

   Thoughts?
      Mark



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-08-29 20:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-20  3:00 System asyncs and mutexes: a combination prone to deadlocks Mark H Weaver
2013-08-20  3:20 ` Chaos Eternal
2013-08-23 21:11   ` Mark H Weaver
2013-08-23 21:18 ` Mark H Weaver
2013-08-29 20:28   ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).