-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for failure to reset runtimeKeepaliveCounter (#21526)
Failure to reset this counter means that we can leak workers under the following circumstances: 1. Thread A calls exit_with_live_runtime causing to to have its keelalive counter set. 2. Thread A is cancelled via pthead_cancel, returning the worker to the pool. 3. Thread B inherits the worker from the pool, with the keelalive counter still in a non-zero state. 4. Thread B exits normally, but is kept alive unnecessarily (it also cannot be join()'d). Each time steps 1-4 is repeated we effectively leak the worker since it can never be re-used.
- Loading branch information
Showing
5 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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,57 @@ | ||
#include <emscripten/emscripten.h> | ||
#include <emscripten/eventloop.h> | ||
#include <emscripten/console.h> | ||
|
||
#include <assert.h> | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
|
||
_Atomic int ready; | ||
|
||
void markReady(void* arg) { | ||
ready = 1; | ||
} | ||
|
||
void* thread_main(void* arg) { | ||
int keepalive = (intptr_t)arg; | ||
emscripten_outf("in thread_main: %d", keepalive); | ||
if (keepalive) { | ||
// Exit with live runtime. Once we are in the event loop call markReady. | ||
emscripten_async_call(markReady, NULL, 0); | ||
emscripten_exit_with_live_runtime(); | ||
} | ||
return NULL; | ||
} | ||
|
||
void checkThreadPool() { | ||
int running = EM_ASM_INT(return PThread.runningWorkers.length); | ||
int unused = EM_ASM_INT(return PThread.unusedWorkers.length); | ||
printf("running=%d unused=%d\n", running, unused); | ||
assert(running == 0); | ||
assert(unused == 1); | ||
} | ||
|
||
int main() { | ||
printf("in main\n"); | ||
checkThreadPool(); | ||
pthread_t t, t2; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
ready = 0; | ||
|
||
// Create a thread that exit's with a non-zero keepalive counter | ||
pthread_create(&t, NULL, thread_main, (void*)1); | ||
while (!ready) {} | ||
pthread_cancel(t); | ||
pthread_join(t, NULL); | ||
checkThreadPool(); | ||
|
||
// Create a second thread that should re-use the same worker. | ||
// This thread should reset its keepalive counter on startup and | ||
// be able to exit normally. | ||
pthread_create(&t2, NULL, thread_main, NULL); | ||
pthread_join(t2, NULL); | ||
checkThreadPool(); | ||
} | ||
printf("done\n"); | ||
} |
This file contains 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,15 @@ | ||
in main | ||
running=0 unused=1 | ||
in thread_main: 1 | ||
running=0 unused=1 | ||
in thread_main: 0 | ||
running=0 unused=1 | ||
in thread_main: 1 | ||
running=0 unused=1 | ||
in thread_main: 0 | ||
running=0 unused=1 | ||
in thread_main: 1 | ||
running=0 unused=1 | ||
in thread_main: 0 | ||
running=0 unused=1 | ||
done |
This file contains 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