Skip to content

Commit 8876138

Browse files
authored
Remove extra lock-taking in preopen setup (#491)
Issue [#8392] in Wasmtime highlights how preopen registration can result in a hang when compiled for a threaded environment. The problem is that `internal_register_preopened_fd_unlocked` assumes it will not touch the global lock because its caller, `__wasilibc_populate_preopens`, already has taken the lock. Unfortunately, a refactoring in #408 (which introduces `internal_register_preopened_fd_unlocked`) did not catch that the `resize` function called internally also takes the global lock. This change removes that locking in `resize` under the assumption that it will only be called when the lock is already taken. [#8392]: bytecodealliance/wasmtime#8392
1 parent 129ee9b commit 8876138

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

libc-bottom-half/sources/preopens.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ static void assert_invariants(void) {
5959

6060
/// Allocate space for more preopens. Returns 0 on success and -1 on failure.
6161
static int resize(void) {
62-
LOCK(lock);
6362
size_t start_capacity = 4;
6463
size_t old_capacity = preopen_capacity;
6564
size_t new_capacity = old_capacity == 0 ? start_capacity : old_capacity * 2;
6665

6766
preopen *old_preopens = preopens;
6867
preopen *new_preopens = calloc(sizeof(preopen), new_capacity);
6968
if (new_preopens == NULL) {
70-
UNLOCK(lock);
7169
return -1;
7270
}
7371

@@ -77,7 +75,6 @@ static int resize(void) {
7775
free(old_preopens);
7876

7977
assert_invariants();
80-
UNLOCK(lock);
8178
return 0;
8279
}
8380

@@ -101,8 +98,7 @@ static const char *strip_prefixes(const char *path) {
10198
return path;
10299
}
103100

104-
/// Similar to `internal_register_preopened_fd_unlocked` but does not
105-
/// take a lock.
101+
/// Similar to `internal_register_preopened_fd` but does not take a lock.
106102
static int internal_register_preopened_fd_unlocked(__wasi_fd_t fd, const char *relprefix) {
107103
// Check preconditions.
108104
assert_invariants();

0 commit comments

Comments
 (0)