Mutexes held by background threads at fork not always released
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.
1. Download the attached mutex_fork.rb
2. Run it
The program should just spin indefinitely, producing no output.
A deadlock is produced, and the following output is seen:
synchronize': No live threads left. Deadlock? from ./mutex_fork.rb:14:inblock (2 levels) in '
fork'block in '
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.
#2 Updated by nobu (Nobuyoshi Nakada) almost 4 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. [Bug #8433]