Skip to content

Commit 68b1844

Browse files
committed
Fix mutex_free so it doesn't call into Ruby code during GC
While freeing a locked mutex, if there is a waiter for the mutex it could call `rb_fiber_scheduler_unblock` which calls into Ruby code. The function `rb_mutex_unlock_th` was a footgun because it was used during normal Ruby code and also during GC. The fix is to not call this function during GC and simply remove the mutex from th->keeping_mutexes.
1 parent d0a2570 commit 68b1844

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

thread_sync.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ mutex_locked_p(rb_mutex_t *mutex)
103103
return mutex->ec_serial != 0;
104104
}
105105

106+
static void thread_mutex_remove(rb_thread_t *thread, rb_mutex_t *mutex);
107+
106108
static void
107109
mutex_free(void *ptr)
108110
{
109111
rb_mutex_t *mutex = ptr;
110112
if (mutex_locked_p(mutex)) {
111-
const char *err = rb_mutex_unlock_th(mutex, mutex->th, 0);
112-
if (err) rb_bug("%s", err);
113+
thread_mutex_remove(mutex->th, mutex);
113114
}
114115
ruby_xfree(ptr);
115116
}

0 commit comments

Comments
 (0)