-
Notifications
You must be signed in to change notification settings - Fork 3.5k
WasmFS: take directory locks parent-first in renameat (fixes rename deadlock) #27685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lunarsong
wants to merge
1
commit into
emscripten-core:main
Choose a base branch
from
Lunarsong:wasmfs-renameat-lock-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Concurrent renames, moves between a directory and its parent, and path | ||
| // lookups in the same directory tree must not deadlock. | ||
| #include <assert.h> | ||
| #include <dirent.h> | ||
| #include <fcntl.h> | ||
| #include <pthread.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <sys/stat.h> | ||
| #include <unistd.h> | ||
|
|
||
| #define ITERATIONS 200 | ||
|
|
||
| static void write_file(const char* path) { | ||
| int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
| assert(fd >= 0); | ||
| assert(write(fd, "x", 1) == 1); | ||
| assert(close(fd) == 0); | ||
| } | ||
|
|
||
| // Publish files by writing a temporary and renaming it into place in the same | ||
| // directory, two levels below the root. | ||
| static void* publisher(void* arg) { | ||
| int id = (int)(intptr_t)arg; | ||
| char tmp[64], final[64]; | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| snprintf(tmp, sizeof(tmp), "/a/b/c/tmp%d_%d", id, i); | ||
| snprintf(final, sizeof(final), "/a/b/c/file%d_%d", id, i); | ||
| write_file(tmp); | ||
| assert(rename(tmp, final) == 0); | ||
| assert(unlink(final) == 0); | ||
| } | ||
| return NULL; | ||
| } | ||
|
|
||
| // Move files between a directory and its parent, so the two directories a | ||
| // rename locks are an ancestor and a descendant. | ||
| static void* mover(void* arg) { | ||
| int id = (int)(intptr_t)arg; | ||
| char lower[64], upper[64]; | ||
| for (int i = 0; i < ITERATIONS; i++) { | ||
| snprintf(lower, sizeof(lower), "/a/b/c/move%d_%d", id, i); | ||
| snprintf(upper, sizeof(upper), "/a/b/move%d_%d", id, i); | ||
| write_file(lower); | ||
| assert(rename(lower, upper) == 0); | ||
| assert(rename(upper, lower) == 0); | ||
| assert(unlink(lower) == 0); | ||
| } | ||
| return NULL; | ||
| } | ||
|
|
||
| // Resolve paths through the same tree, which locks each directory on the way | ||
| // down. | ||
| static void* walker(void* arg) { | ||
| struct stat st; | ||
| for (int i = 0; i < ITERATIONS * 4; i++) { | ||
| stat("/a/b/c/absent", &st); | ||
| stat("/a/b/c", &st); | ||
| DIR* dir = opendir("/a/b/c"); | ||
| assert(dir); | ||
| while (readdir(dir)) { | ||
| } | ||
| closedir(dir); | ||
| } | ||
| return NULL; | ||
| } | ||
|
|
||
| int main() { | ||
| assert(mkdir("/a", 0777) == 0); | ||
| assert(mkdir("/a/b", 0777) == 0); | ||
| assert(mkdir("/a/b/c", 0777) == 0); | ||
|
|
||
| pthread_t threads[8]; | ||
| int count = 0; | ||
| for (int i = 0; i < 3; i++) { | ||
| assert(pthread_create(&threads[count++], NULL, publisher, (void*)(intptr_t)i) == 0); | ||
| } | ||
| for (int i = 0; i < 2; i++) { | ||
| assert(pthread_create(&threads[count++], NULL, mover, (void*)(intptr_t)i) == 0); | ||
| } | ||
| for (int i = 0; i < 3; i++) { | ||
| assert(pthread_create(&threads[count++], NULL, walker, NULL) == 0); | ||
| } | ||
| for (int i = 0; i < count; i++) { | ||
| assert(pthread_join(threads[i], NULL) == 0); | ||
| } | ||
| printf("ok\n"); | ||
| return 0; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ok |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two loops are taking locks from children to parents aren't they? Isn't that what we should not be doing?
I was expecting this code to go ancestor-first, as in the description. It could follow the code in
parseParentwhich goes ancestor first. Perhaps even add a utility to check if one path is nested in another, and it could be inpaths.cppand share some code there.Or am I misunderstanding something?
Otherwise this looks great, good find of a real bug!