Skip to content

Commit 23bfc83

Browse files
committed
Fix Monitor#exit when monitor is not owned by current thread
1 parent faaa4c3 commit 23bfc83

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Bug fixes:
4242
* Don't trigger the `method_added` event when changing a method's visibility or calling `module_function` (@paracycle, @nirvdrum).
4343
* Fix `rb_time_timespec_new` function to not call `Time.at` method directly (@andrykonchin).
4444
* Fix `StringIO#write` to transcode strings with encodings that don't match the `StringIO`'s `external_encoding`. (#2839, @flavorjones)
45+
* Fix `Monitor#exit` to raise `ThreadError` when monitor not owned by the current thread (#2922, @andrykonchin).
4546

4647
Compatibility:
4748

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require_relative '../../spec_helper'
2+
require 'monitor'
3+
4+
describe "Monitor#exit" do
5+
it "raises ThreadError when monitor is not entered" do
6+
m = Monitor.new
7+
8+
-> { m.exit }.should raise_error(ThreadError)
9+
end
10+
end

src/main/java/org/truffleruby/core/monitor/TruffleMonitorNodes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ protected Object enter(RubyMutex mutex) {
7878
public abstract static class MonitorExit extends PrimitiveArrayArgumentsNode {
7979

8080
@Specialization
81-
protected Object exit(RubyMutex mutex) {
81+
protected Object exit(RubyMutex mutex,
82+
@Cached BranchProfile errorProfile) {
8283
final RubyThread thread = getLanguage().getCurrentThread();
84+
MutexOperations.checkOwnedMutex(getContext(), mutex.lock, this, errorProfile);
8385
MutexOperations.unlock(mutex.lock, thread);
8486
return mutex;
8587
}

src/main/java/org/truffleruby/core/mutex/MutexOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public static void internalLockEvenWithException(RubyContext context, ReentrantL
100100
}
101101
}
102102

103+
/** Should be preceded with MutexOperations.checkOwnedMutex call to throw a proper exception */
103104
@TruffleBoundary
104105
public static void unlock(ReentrantLock lock, RubyThread thread) {
105106
unlockInternal(lock);

0 commit comments

Comments
 (0)