WasmFS: take directory locks parent-first in renameat (fixes rename deadlock) - #27685
Open
Lunarsong wants to merge 1 commit into
Open
WasmFS: take directory locks parent-first in renameat (fixes rename deadlock)#27685Lunarsong wants to merge 1 commit into
Lunarsong wants to merge 1 commit into
Conversation
__syscall_renameat locked both parent directories and then walked the new parent's ancestors, locking each on the way up: child before parent. Every path lookup locks parent before child, so two threads in one directory tree deadlocked, and every later rename in the process blocked behind them on renameMutex. A destination directly under the root never hit it because the walk exits before taking a lock. Run the ancestor walk before either parent is locked, one lock at a time (renames are serialized and only a rename re-parents a directory, so the chain cannot change under the walk), then lock the two parents ancestor-first. Fixes emscripten-core#27684.
Author
|
The only red check is (Posted by the same AI agent as the PR, on behalf of the account owner.) |
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.
Fixes #27684.
Problem
With
-sWASMFS -pthread, arename()into a directory two or more levels below the root deadlocks against any other thread resolving a path in the same tree, and every laterrenamein the process then blocks behind it onrenameMutex.__syscall_renameatlocked both parent directories and then walked the new parent's ancestors, locking each one on the way up: child before parent. Every path lookup locks parent before child (parseParentwalking down,Directory::Handle::cacheChildlocking the child under the parent). Two threads in one tree close the cycle. A destination directly under the root never deadlocks because the walk exits before taking a lock, which is why the bug depends on depth. The same order also arises when the two parents locked at the top of the function are an ancestor and a descendant, i.e. moving a file one level up or down.Change
renameatnow takes directory locks in the same order as every lookup:renameMutex, and only a rename re-parents a directory, so the chain cannot change under the walk.Error results are unchanged; the
ENOENTandEBUSYchecks that depended only on the source now run before the walk.Test
test/wasmfs/wasmfs_rename_race.c(other.test_wasmfs_rename_race): three threads publish files by temp-then-rename in/a/b/c, two move files between/a/b/cand/a/b, three resolve paths in the same tree. On unpatched 6.0.5 it hangs within the first few operations (killed by a 45 s timeout); with the fix it printsok. A larger stress harness (4 or 8 threads, 800 to 3200 renames, with and without directory sweeps and ASYNCIFY) stalls at 4 to 26 operations unpatched and completes every configuration with the fix.How it was tested here
The test and the stress harness were built with the emsdk 6.0.5 toolchain against a copy of its emscripten tree carrying this exact
syscalls.cppchange (therenameatcode is identical between 6.0.5 andmain);main's libc needs a newer LLVM than the local SDK, sotest/runner.py other.test_wasmfs_rename_racecould not be run locally and the registration follows the siblingtest_wasmfs_*tests. CI is expected to run it.