Commit f3fef05
authored
Signal before unlocking proxy context mutex in emscripten_proxy_finish (#26582)
When finishing a proxied call, the following race condition can happen:
thread 1 in
[emscripten_proxy_finish](https://github.com/emscripten-core/emscripten/blob/main/system/lib/pthread/proxying.c#L337):
```
pthread_mutex_lock(&ctx->sync.mutex);
ctx->sync.state = DONE;
remove_active_ctx(ctx);
pthread_mutex_unlock(&ctx->sync.mutex);
--> thread is preempted or suspends here <---
pthread_cond_signal(&ctx->sync.cond);
```
thread 2 in emscripten_proxy_sync_with_ctx:
(ctx is on this thread's stack)
```
pthread_mutex_lock(&ctx.sync.mutex); <-- locks after unlock above
while (ctx.sync.state == PENDING) { <--- reads sync.state == DONE
pthread_cond_wait(&ctx.sync.cond, &ctx.sync.mutex); <-- doesn't run
}
pthread_mutex_unlock(&ctx.sync.mutex);
int ret = ctx.sync.state == DONE;
em_proxying_ctx_deinit(&ctx); <--- frees ctx and returns
```
Then thread 1 tries to run pthread_cond_signal on the freed ctx.
This same logic applies to cancel_ctx which is also called on the target
thread.1 parent 9d9b267 commit f3fef05
File tree
3 files changed
+10
-6
lines changed- system/lib/pthread
- test/codesize
3 files changed
+10
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
339 | 339 | | |
340 | 340 | | |
341 | 341 | | |
342 | | - | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
343 | 345 | | |
| 346 | + | |
344 | 347 | | |
345 | 348 | | |
346 | 349 | | |
| |||
365 | 368 | | |
366 | 369 | | |
367 | 370 | | |
368 | | - | |
| 371 | + | |
369 | 372 | | |
| 373 | + | |
370 | 374 | | |
371 | 375 | | |
372 | 376 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
0 commit comments