Skip to content

Mutex Deadlock fix: Track the fiber that owns a Mutex - #9649

Merged
headius merged 1 commit into
jruby:masterfrom
sampokuokkanen:fiber-scheduler-mutex-deadlock
Sep 8, 2026
Merged

headius merged 1 commit into
jruby:masterfrom
sampokuokkanen:fiber-scheduler-mutex-deadlock

Conversation

@sampokuokkanen

@sampokuokkanen sampokuokkanen commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

The Mutex deadlock check asked the owning thread's ThreadContext which fiber it was currently running. That answer is only right while the root fiber holds the lock. The reverse case is a fiber holding the lock while the root fiber tries to take it. There the check never fires, and Mutex#lock blocks forever instead of raising ThreadError. Store the owning fiber on the mutex instead.

Unexcludes TestFiberMutex#test_mutex_deadlock.

Sample code:

m = Mutex.new
f = Fiber.new { m.lock; Fiber.yield }
f.resume
m.lock

Master: hangs
This branch: ThreadError: deadlock; lock already owned by another fiber belonging to the same thread

The deadlock check asked the owning thread's ThreadContext which fiber it was currently running. That answer is only right while the root fiber
holds the lock. The reverse case is a fiber holding the lock while the
root fiber tries to take it. There the check never fires, and Mutex#lock
blocks forever instead of raising ThreadError.

Store the owning fiber on the mutex instead.

Unexcludes TestFiberMutex#test_mutex_deadlock.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, makes sense.

@headius headius added this to the JRuby 10.1.2.0 milestone Sep 8, 2026
@headius
headius merged commit 7f5ab1b into jruby:master Sep 8, 2026
113 of 115 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants