From: "k0kubun (Takashi Kokubun) via ruby-core" Date: 2026-11-20T00:23:32+00:00 Subject: [ruby-core:127639] [Ruby Bug#22598] A thread that ends holding two mutexes corrupts the next owner's mutex list Issue #22598 has been updated by k0kubun (Takashi Kokubun). Backport changed from 3.3: UNKNOWN, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: UNKNOWN, 3.4: REQUIRED, 4.0: DONE ruby_4_0 commit:a2d94e318ea5d2abf51b61176f25b0ec8726ef21 merged revision(s) commit:e1d5a61acb9109f023398f26c68e447f69e310d3. ---------------------------------------- Bug #22598: A thread that ends holding two mutexes corrupts the next owner's mutex list https://bugs.ruby-lang.org/issues/22598#change-120158 * Author: jhawthorn (John Hawthorn) * Status: Closed * ruby -v: ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: REQUIRED, 4.0: DONE ---------------------------------------- Reproduces on 4.0.9 and master: ```ruby m1 = Thread::Mutex.new m2 = Thread::Mutex.new Thread.new { m1.lock; m2.lock }.join Thread.new { m2.synchronize { } }.join # [BUG] invalid keeping_mutexes: Attempt to unlock a mutex which is not locked ``` When a thread exits, `rb_threadptr_unlock_all_locking_mutexes` pops each mutex off `th->keeping_mutexes` before unlocking it, so `rb_mutex_unlock_th` never clears its `next_mutex`. The next thread to lock `m2` inherits `m1` in its list and unlocks it when it exits, which crashes if `m1` is unlocked, releases it out from under another thread if it's locked, and is a use after free if it's been collected. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/