Skip to content

Commit a22b02f

Browse files
authored
Avoid timed waits in test_pthread_proxying_canceled_work (#19083)
In the test cases that proxy work and then exit or cancel the worker thread before the proxied work can be executed, avoid using a timed wait to determine when to cancel or exit the thread. Instead, signal the worker by setting an atomic flag to tell it that the proxied work has been sent. This requires removing the synchronous proxying from these test cases, but that's fairly safe because the synchronous and asynchronous proxying methods have the same internals and the cancellation is tested in combination with synchronous proxying in other test cases. This will fix the flake reported in #18921 if it was caused by the overloaded CI machines taking more than 20ms to execute the proxying calls.
1 parent 48109af commit a22b02f

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

test/pthread/test_pthread_proxying_canceled_work.c

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,32 @@ void test_exit_then_proxy() {
113113
pthread_join(thread, NULL);
114114
}
115115

116-
void* wait_then_cancel(void* running) {
117-
*((_Atomic int*)running) = 1;
116+
struct flags {
117+
_Atomic int running;
118+
_Atomic int proxied;
119+
};
120+
121+
void* wait_then_cancel(void* arg) {
122+
struct flags* flags = arg;
123+
flags->running = 1;
118124

119-
// Wait 20 ms for the proxying to start.
120-
// TODO: Test with with a cancellable async proxying API to avoid the wait.
121-
int wait_val = 0;
122-
int wait_time = 20000000;
123-
__builtin_wasm_memory_atomic_wait32(&wait_val, 0, wait_time);
125+
// Wait for the proxying to start.
126+
while (!flags->proxied) {
127+
}
124128

125129
pthread_cancel(pthread_self());
126130
pthread_testcancel();
127131
assert(0 && "thread should have been canceled!");
128132
return NULL;
129133
}
130134

131-
void* wait_then_exit(void* running) {
132-
*((_Atomic int*)running) = 1;
135+
void* wait_then_exit(void* arg) {
136+
struct flags* flags = arg;
137+
flags->running = 1;
133138

134-
// Wait 20 ms for the proxying to start.
135-
// TODO: Test with with a cancellable async proxying API to avoid the wait.
136-
int wait_val = 0;
137-
int wait_time = 20000000;
138-
__builtin_wasm_memory_atomic_wait32(&wait_val, 0, wait_time);
139+
// Wait for the proxying to start.
140+
while (!flags->proxied) {
141+
}
139142

140143
pthread_exit(NULL);
141144
assert(0 && "thread should have exited!");
@@ -145,11 +148,11 @@ void* wait_then_exit(void* running) {
145148
void test_proxy_then_cancel() {
146149
printf("testing proxy followed by cancel\n");
147150

148-
_Atomic int running = 0;
151+
struct flags flags = (struct flags){.running = 0, .proxied = 0};
149152
pthread_t thread;
150-
pthread_create(&thread, NULL, wait_then_cancel, &running);
153+
pthread_create(&thread, NULL, wait_then_cancel, &flags);
151154

152-
while (!running) {
155+
while (!flags.running) {
153156
}
154157

155158
// The pending proxied work should be canceled when the thread is canceled.
@@ -163,20 +166,18 @@ void test_proxy_then_cancel() {
163166
assert(ret == 1);
164167
add_promise(promise);
165168

166-
ret = emscripten_proxy_sync(queue, thread, explode, NULL);
167-
assert(ret == 0);
168-
169+
flags.proxied = 1;
169170
pthread_join(thread, NULL);
170171
}
171172

172173
void test_proxy_then_exit() {
173174
printf("testing proxy followed by exit\n");
174175

175-
_Atomic int running = 0;
176+
struct flags flags = (struct flags){.running = 0, .proxied = 0};
176177
pthread_t thread;
177-
pthread_create(&thread, NULL, wait_then_exit, &running);
178+
pthread_create(&thread, NULL, wait_then_exit, &flags);
178179

179-
while (!running) {
180+
while (!flags.running) {
180181
}
181182

182183
// The pending proxied work should be canceled when the thread exits.
@@ -190,9 +191,7 @@ void test_proxy_then_exit() {
190191
assert(ret == 1);
191192
add_promise(promise);
192193

193-
ret = emscripten_proxy_sync(queue, thread, explode, NULL);
194-
assert(ret == 0);
195-
194+
flags.proxied = 1;
196195
pthread_join(thread, NULL);
197196
}
198197

0 commit comments

Comments
 (0)