Fix SMP mutex abandonment lock order during thread termination - #1035
Open
Vikram-Lex wants to merge 1 commit into
Open
Fix SMP mutex abandonment lock order during thread termination#1035Vikram-Lex wants to merge 1 commit into
Vikram-Lex wants to merge 1 commit into
Conversation
Vikram-Lex
marked this pull request as draft
September 8, 2026 04:42
Vikram-Lex
marked this pull request as ready for review
September 8, 2026 04:43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Terminating a thread while another thread calls
mutex-unlock!on one of its mutexes can deadlock the SMP runtime. The terminating thread holds its low-level thread lock and waits for the mutex lock, while the unlocking processor holds the mutex lock and waits for the same thread lock. Both public operations are allowed by the manual.The patch releases the terminating thread's lock before acquiring an owned mutex, then rechecks ownership with both locks held. If another thread has already unlocked or transferred the mutex, cleanup leaves its new state alone. Waiting mutex callers use the existing ordered locking path, with an explicit abandonment flag to preserve their exception behavior.
The continuation's existing
#ttermination marker prevents the thread from running or acquiring new mutexes during cleanup. The end condition variable remains available until cleanup finishes, so boththread-join!and a terminalthread-statereport wait for all owned mutexes to be abandoned. The unicore implementation is unchanged.Validation on macOS arm64, based on
e0236bccd8c2a06c315c2970c21e3dae8673b0bb:mutex-unlock!and##btq-abandon!.09-io/write_read.scmand15-misc/serdes.scm. Each file runs with a 15-second external deadline.A separate 1,000-iteration exception/abandonment diagnostic encountered a crash on both unmodified and patched SMP builds with a 64 MB minimum heap. That broader runtime issue remains outside this fix; this contribution does not claim to resolve all SMP problems.
Builds use
--enable-c-opt=-O1 --enable-c-opt-rts=-O1. SMP also uses--enable-smp --enable-multiple-threaded-vms. Runtime tests use-:p1,m64M,-:p2,m64M, or-:p4,m64Mto avoid the separately reported default heap sizing issue.