Project

General

Profile

Actions

Bug #8433

closed

Mutexes held by background threads at fork not always released

Added by benweint (Ben Weintraub) almost 11 years ago. Updated about 10 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]
[ruby-core:55102]

Description

It appears that the Ruby interpreter attempts to automatically unlock Mutexes held by background threads at the time Process.fork is called in order to avoid deadlocks in the child process. Unfortunately, the logic for doing so appears unreliable.

Reproduction steps:

  1. Download the attached mutex_fork.rb
  2. Run it

Expected results:
The program should just spin indefinitely, producing no output.

Actual results:
A deadlock is produced, and the following output is seen:

$ ./mutex_fork.rb
./mutex_fork.rb:14:in synchronize': No live threads left. Deadlock? from ./mutex_fork.rb:14:in block (2 levels) in '
from ./mutex_fork.rb:13:in fork' from ./mutex_fork.rb:13:in block in '
from ./mutex_fork.rb:11:in loop' from ./mutex_fork.rb:11:in '

Notes:
The child process is deadlocking when attempting to acquire the mutex that was held by a background thread in the parent at the time Process.fork was called. rb_mutex_abandon_all appears to be intended to prevent this problem, but is not reliable as demonstrated by this test case. I suspect that the fork happens after the background thread has acquired the mutex, but before it has updated its keeping_mutexes list, so upon fork, the forking thread does not realize that it should abandon this mutex.

Note that the Mutex#synchronize call on line 12 (just prior to the Process.fork call) is critical to reproduction here. Removing it causes the program to behave correctly.


Files

mutex_fork.rb (235 Bytes) mutex_fork.rb benweint (Ben Weintraub), 05/22/2013 02:52 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0