Issue #22598 has been updated by nagachika (Tomoyuki Chikanaga). Backport changed from 3.3: UNKNOWN, 3.4: REQUIRED, 4.0: DONE to 3.3: UNKNOWN, 3.4: DONE, 4.0: DONE Merged into `ruby_3_4` at https://github.com/ruby/ruby/commit/c8208e8adc17d0cc2a2c5ea3790088c642dd11c1. ---------------------------------------- Bug #22598: A thread that ends holding two mutexes corrupts the next owner's mutex list https://bugs.ruby-lang.org/issues/22598#change-120174 * 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: DONE, 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/