Project

General

Profile

Bug #11040

Mutex can be locked by multiple threads, causing Monitor to sometimes hang

Added by jvdp (Jaap van der Plas) over 5 years ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0dev (2015-04-07 trunk 50178) [x86_64-darwin13]
[ruby-core:<unknown>]

Description

I have found a case where Monitor sometimes does not release the lock on its mutex where it should, because the internal counter is increased by another thread than the one holding it. From the trace it seems that Mutex#lock succeeds while another thread has not yet unlocked it.

This seems to be triggered when a monitor is accessed by a freshly spawned thread. I have attached a small test case (based on monitor.rb) that should trigger the bug reliably and a trace that shows what goes wrong. The most important bits: (showing only c-returns from Mutex#lock and Mutex#unlock)

...
 #<Thread:0x007fea210a8fd0> c-return mutex_bug.rb:12       lock    Mutex  0
 #<Thread:0x007fea21098c70> c-return mutex_bug.rb:12       lock    Mutex  0
 #<Thread:0x007fea210a8fd0> c-return mutex_bug.rb:22     unlock    Mutex  0
 #<Thread:0x007fea21061450> c-return mutex_bug.rb:12       lock    Mutex #<Thread:0x007fea21098c70> 1

That last line means that the thread acquired a lock on the mutex, while an owner and count of 1 are already set (ie. it should not be lockable.)

(I first came across this bug on a Rails app when Rack::Timeout seemed to cause requests to hang when logging messages.)


Files

mutex_bug.rb (1003 Bytes) mutex_bug.rb jvdp (Jaap van der Plas), 04/07/2015 11:37 AM
mutex_bug_trace.txt (105 KB) mutex_bug_trace.txt jvdp (Jaap van der Plas), 04/07/2015 11:38 AM
mutex_bug.rb (913 Bytes) mutex_bug.rb jvdp (Jaap van der Plas), 04/07/2015 12:09 PM
mutex_bug_minimal.rb (498 Bytes) mutex_bug_minimal.rb jvdp (Jaap van der Plas), 04/07/2015 12:40 PM
monitor_patch.patch (679 Bytes) monitor_patch.patch jvdp (Jaap van der Plas), 04/09/2015 08:19 PM
#1

Updated by jvdp (Jaap van der Plas) over 5 years ago

I attached a slightly simpler test case.

#2

Updated by jvdp (Jaap van der Plas) over 5 years ago

Attached what I think is the minimal test case.

#3

Updated by jvdp (Jaap van der Plas) over 5 years ago

Aha, it seems that killing the thread releases the mutex lock directly, while the counter is not decreased. Is this something Monitor should be able to deal with?

#4

Updated by jvdp (Jaap van der Plas) over 5 years ago

I have attached a patch for Monitor that should prevent this issue.

Updated by jvdp (Jaap van der Plas) over 5 years ago

So now that I added a patch for lib/monitor.rb I guess this should be assigned to Shugo Maeda (shugo) per https://bugs.ruby-lang.org/projects/ruby/wiki/MaintainersStdlib, could someone do that for me? Or should I make a new issue?

Updated by jeremyevans0 (Jeremy Evans) over 1 year ago

  • Status changed from Open to Closed

Also available in: Atom PDF