Skip to content

Properly unlock locked mutexes on thread cleanup. #14262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luke-gruber
Copy link
Contributor

@luke-gruber luke-gruber commented Aug 18, 2025

Mutexes were being improperly unlocked on thread cleanup. This bug was introduced in
050a895.

We must keep a reference from the mutex to the thread, because if the fiber
is collected before the mutex is, then we cannot unlink it from the thread
in mutex_free. If it's not unlinked from the the thread when it's freed, it
causes bugs in rb_thread_unlock_all_locking_mutexes.

Fixes [Bug #21342]

This comment has been minimized.

@luke-gruber luke-gruber force-pushed the issue_21342_fiber_thread_gc branch 3 times, most recently from 3a2fb18 to 4c2c911 Compare August 18, 2025 16:24
thread_sync.c Outdated
{
rb_mutex_t *mutex = ptr;
if (mutex->fiber) {
rb_gc_mark_movable(rb_fiberptr_self(mutex->fiber));
Copy link
Member

Choose a reason for hiding this comment

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

You use rb_gc_mark_movable but mutex_data_type doesn't register a dcompact method.

Copy link
Contributor Author

@luke-gruber luke-gruber Aug 18, 2025

Choose a reason for hiding this comment

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

👋 Doesn't this just mark the fiber without pinning it? Oh right, then we would need our own way to update its references.
Edit: John just mentioned to me that we would also probably need write barriers for fiber. John and I will discuss later this week.

Copy link
Member

Choose a reason for hiding this comment

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

Yes precisely. And if you don't have a dcompact callback, by not pinning it, you allow it to move, if that does happen, the ref might be corrupted.

Now in this case it turns out it's alright because the ref is inside a struct you don't own, and the Fiber will update its own reference. But I'd either add a very clear comment mentioning this assumption, or just implement dcompact here too.

Mutexes were being improperly unlocked on thread cleanup. This bug was
introduced in 050a895.

We must keep a reference from the mutex to the thread, because if the fiber
is collected before mutex is, then we cannot unlink it from the thread in
`mutex_free`. If it's not unlinked from the the thread when it's freed, it
causes bugs in `rb_thread_unlock_all_locking_mutexes`.

Fixes [Bug #21342]
@luke-gruber luke-gruber force-pushed the issue_21342_fiber_thread_gc branch from 4c2c911 to 150b16a Compare August 19, 2025 13:33
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