Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/src/main/java/org/jruby/ext/thread/Mutex.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public IRubyObject lock(ThreadContext context) {

checkRelocking(context);

if (this.lockingThread == parentThread) {
RubyThread lockingThread = this.lockingThread;
if (lockingThread == parentThread && context.getFiber() != lockingThread.getContext().getFiber()) {
throw context.runtime.newThreadError("deadlock; lock already owned by another fiber belonging to the same thread");
}

Expand All @@ -123,7 +124,7 @@ public IRubyObject lock(ThreadContext context) {
}
}

this.lockingThread = thread;
this.lockingThread = parentThread;

// always check for thread interrupts after acquiring lock
try {
Expand Down Expand Up @@ -204,7 +205,7 @@ public IRubyObject owned_p(ThreadContext context) {

private void checkRelocking(ThreadContext context) {
if (lock.isHeldByCurrentThread()) {
throw context.runtime.newThreadError("Mutex relocking by same thread");
throw context.runtime.newThreadError("deadlock; recursive locking");
}
}

Expand Down
25 changes: 23 additions & 2 deletions spec/ruby/core/mutex/lock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,32 @@

# Unable to find a specific ticket but behavior change may be
# related to this ML thread.
it "raises a ThreadError when used recursively" do
it "raises a deadlock ThreadError when used recursively" do
m = Mutex.new
m.lock
-> {
m.lock
}.should raise_error(ThreadError)
}.should raise_error(ThreadError, /deadlock/)
end

it "raises a deadlock ThreadError when multiple fibers from the same thread try to lock" do
m = Mutex.new

m.lock
f0 = Fiber.new do
m.lock
end
-> { f0.resume }.should raise_error(ThreadError, /deadlock/)

m.unlock
f1 = Fiber.new do
m.lock
Fiber.yield
end
f2 = Fiber.new do
m.lock
end
f1.resume
-> { f2.resume }.should raise_error(ThreadError, /deadlock/)
end
end
Loading