Skip to content

Commit b721987

Browse files
authored
Second attempt at fixing test_pthread_c11_threads (#15187)
This time I'm pretty sure the deadlock no longer occurs. The issue was the pthread key destcrutor was calling printf. Again this doesn't fix the actual issues but avoids hitting it this test. Fixes: #14579 See: #15186
1 parent 0443aa9 commit b721987

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

tests/pthread/test_pthread_c11_threads.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ void do_once(void) {
1717
printf("in do_once\n");
1818
}
1919

20+
// Because this thread is detached it can still be running
21+
// when the main thread exits. And becasue of an emscripten
22+
// bug (https://github.com/emscripten-core/emscripten/issues/15186).
23+
// this means we can't write to stdout after the main thread
24+
// exits. This means we can't use `thread_main` below because
25+
// the destructor to the `tss_t key` writes to stdout.
26+
int thread_main_detached(void* arg) {
27+
printf("in thread_main_detached %p\n", (void*)thrd_current());
28+
mtx_lock(&mutex);
29+
thread_counter--;
30+
cnd_signal(&cond);
31+
mtx_unlock(&mutex);
32+
return 0;
33+
}
34+
2035
int thread_main(void* arg) {
2136
printf("in thread_main %p\n", (void*)thrd_current());
2237
tss_set(key, (void*)thrd_current());
@@ -87,8 +102,7 @@ int main(int argc, char* argv[]) {
87102

88103
// Test thrd_detach
89104
thrd_t t6;
90-
assert(thrd_create(&t6, thread_main, NULL) == thrd_success);
91-
thread_counter++;
105+
assert(thrd_create(&t6, thread_main_detached, NULL) == thrd_success);
92106
assert(thrd_detach(t6) == thrd_success);
93107

94108
// Wait for the thread to at least be done printing before exiting
@@ -97,9 +111,8 @@ int main(int argc, char* argv[]) {
97111
// where a deadlock can occur between main thread calling fflush()
98112
// during exitRuntime and the detached thread calling print (and
99113
// therefore holding the stdout lock).
100-
// See https://github.com/emscripten-core/emscripten/issues/14579.
101-
while (thread_counter)
102-
assert(cnd_wait(&cond, &mutex) == thrd_success);
114+
// See https://github.com/emscripten-core/emscripten/issues/15186.
115+
assert(cnd_wait(&cond, &mutex) == thrd_success);
103116

104117
printf("done!\n");
105118
return 0;

0 commit comments

Comments
 (0)