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

Updated by obrie (Aaron Pfeifer) over 10 years ago

I've submitted a proposed patch for this issue at https://github.com/ruby/ruby/pull/415 . This seems to have done the trick for me running it through some basic tests, including the one you posted.

Actions #2

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • % Done changed from 0 to 100
  • Status changed from Open to Closed

This issue was solved with changeset r43148.
Ben, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


thread.c: fix some mutexes remaining locked after forking

  • thread.c (terminate_atfork_i): fix locking mutexes not unlocked in
    forks when not tracked in thread. [ruby-core:55102] [Bug #8433]

Updated by obrie (Aaron Pfeifer) over 10 years ago

Thank you for the quick response! Any chance this will be backported to 1.9.3?

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN to 1.9.3: REQUIRED, 2.0.0: REQUIRED

Updated by usa (Usaku NAKAMURA) about 10 years ago

  • Backport changed from 1.9.3: REQUIRED, 2.0.0: REQUIRED to 1.9.3: DONE, 2.0.0: REQUIRED

r43148, r43149, and r43152 are backported into ruby_1_9_3 at r45026.

Updated by nagachika (Tomoyuki Chikanaga) about 10 years ago

  • Backport changed from 1.9.3: DONE, 2.0.0: REQUIRED to 1.9.3: DONE, 2.0.0: DONE

r43148, r43149, and r43152 were backported to ruby_2_0_0 at r45049.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0