Skip to content

Commit 4b33521

Browse files
authored
[test] Fix flakiness in test_pthread_wait32/64_notify (#20412)
Avoid racing between the main thread and the worker by only triggering the main thread to notify once the workers done printing all its messages.
1 parent dcc9d4b commit 4b33521

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

test/test_browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5296,12 +5296,12 @@ def test_wasm_worker_hardware_concurrency_is_lock_free(self):
52965296
# Tests emscripten_atomic_wait_u32() and emscripten_atomic_notify() functions.
52975297
@also_with_minimal_runtime
52985298
def test_wasm_worker_wait32_notify(self):
5299-
self.btest('wasm_worker/wait32_notify.c', expected='2', args=['-sWASM_WORKERS'])
5299+
self.btest('wasm_worker/wait32_notify.c', expected='3', args=['-sWASM_WORKERS'])
53005300

53015301
# Tests emscripten_atomic_wait_u64() and emscripten_atomic_notify() functions.
53025302
@also_with_minimal_runtime
53035303
def test_wasm_worker_wait64_notify(self):
5304-
self.btest('wasm_worker/wait64_notify.c', expected='2', args=['-sWASM_WORKERS'])
5304+
self.btest('wasm_worker/wait64_notify.c', expected='3', args=['-sWASM_WORKERS'])
53055305

53065306
# Tests emscripten_atomic_wait_async() function.
53075307
@also_with_minimal_runtime

test/test_core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,11 +2923,13 @@ def test_pthread_run_script(self):
29232923

29242924
@node_pthreads
29252925
def test_pthread_wait32_notify(self):
2926+
self.set_setting('EXIT_RUNTIME')
29262927
self.do_run_in_out_file_test(test_file('wasm_worker/wait32_notify.c'))
29272928

29282929
@node_pthreads
29292930
@no_wasm2js('https://github.com/WebAssembly/binaryen/issues/5991')
29302931
def test_pthread_wait64_notify(self):
2932+
self.set_setting('EXIT_RUNTIME')
29312933
self.do_run_in_out_file_test(test_file('wasm_worker/wait64_notify.c'))
29322934

29332935
def test_tcgetattr(self):

test/wasm_worker/wait32_notify.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ void run_test() {
3333
assert(ret == ATOMICS_WAIT_TIMED_OUT);
3434

3535
emscripten_out("Waiting for infinitely long should return 'ok'");
36-
ret = emscripten_atomic_wait_u32((int32_t*)&addr, 1, /*timeout=*/-1);
36+
emscripten_atomic_store_u32((void*)&addr, 3);
37+
ret = emscripten_atomic_wait_u32((int32_t*)&addr, 3, /*timeout=*/-1);
3738
assert(ret == ATOMICS_WAIT_OK);
3839

3940
emscripten_out("Test finished");
@@ -43,8 +44,6 @@ void run_test() {
4344
// This test run in both wasm workers and pthreads mode
4445
#ifdef __EMSCRIPTEN_WASM_WORKERS__
4546

46-
char stack[1024];
47-
4847
void worker_main() {
4948
run_test();
5049

@@ -66,15 +65,14 @@ void* thread_main(void* arg) {
6665

6766

6867
EM_BOOL main_loop(double time, void *userData) {
69-
if (addr == 1) {
68+
if (addr == 3) {
7069
// Burn one second to make sure worker finishes its test.
7170
emscripten_out("main: seen worker running");
7271
double t0 = emscripten_performance_now();
7372
while(emscripten_performance_now() < t0 + 1000);
7473

7574
// Wake the waiter
7675
emscripten_out("main: waking worker");
77-
addr = 2;
7876
emscripten_atomic_notify((int32_t*)&addr, 1);
7977

8078
#ifndef __EMSCRIPTEN_WASM_WORKERS__
@@ -90,6 +88,7 @@ int main() {
9088
emscripten_out("main: creating worker");
9189

9290
#ifdef __EMSCRIPTEN_WASM_WORKERS__
91+
static char stack[1024];
9392
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));
9493
emscripten_wasm_worker_post_function_v(worker, worker_main);
9594
#else

test/wasm_worker/wait64_notify.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ void run_test() {
3434
assert(ret == ATOMICS_WAIT_TIMED_OUT);
3535

3636
emscripten_out("Waiting for infinitely long should return 'ok'");
37-
ret = emscripten_atomic_wait_u64((int64_t*)&addr, 0x100000000ull, /*timeout=*/-1);
37+
emscripten_atomic_store_u64((void*)&addr, 0x300000000ull);
38+
ret = emscripten_atomic_wait_u64((int64_t*)&addr, 0x300000000ull, /*timeout=*/-1);
3839
assert(ret == ATOMICS_WAIT_OK);
3940

4041
emscripten_out("Test finished");
@@ -65,15 +66,14 @@ void* thread_main(void* arg) {
6566
#endif
6667

6768
EM_BOOL main_loop(double time, void *userData) {
68-
if (addr == 0x100000000ull) {
69+
if (addr == 0x300000000ull) {
6970
// Burn one second to make sure worker finishes its test.
7071
emscripten_out("main: seen worker running");
7172
double t0 = emscripten_performance_now();
7273
while(emscripten_performance_now() < t0 + 1000);
7374

7475
// Wake the waiter
7576
emscripten_out("main: waking worker");
76-
addr = 0x200000000ull;
7777
emscripten_atomic_notify((int32_t*)&addr, 1);
7878

7979
return EM_FALSE;

test/wasm_worker/wait64_notify.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Waiting for >0 nanoseconds should return 'timed-out'
88
Waiting for infinitely long should return 'ok'
99
main: seen worker running
1010
main: waking worker
11+
Test finished

0 commit comments

Comments
 (0)