Skip to content

Commit 2188869

Browse files
authored
Don't free active pthread data during terminateAllThreads (#18676)
This is a workaround for the fact that `worker.terminate()` seems to be async, which means the thread could continue to run after it returns. Fixes: #18675
1 parent 885eafb commit 2188869

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/library_pthread.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,28 +176,21 @@ var LibraryPThread = {
176176
#if PTHREADS_DEBUG
177177
dbg('terminateAllThreads');
178178
#endif
179-
for (var worker of Object.values(PThread.pthreads)) {
180-
#if ASSERTIONS
181-
assert(worker);
182-
#endif
183-
PThread.returnWorkerToPool(worker);
179+
// Attempt to kill all workers. Sadly (at least on the web) there is no
180+
// way to terminate a worker synchronously, or to be notified when a
181+
// worker in actually terminated. This means there is some risk that
182+
// pthreads will continue to be executing after `worker.terminate` has
183+
// returned. For this reason, we don't call `returnWorkerToPool` here or
184+
// free the underlying pthread data structures.
185+
for (var worker of PThread.runningWorkers) {
186+
worker.terminate();
184187
}
185-
186-
#if ASSERTIONS
187-
// At this point there should be zero pthreads and zero runningWorkers.
188-
// All workers should be now be the unused queue.
189-
assert(Object.keys(PThread.pthreads).length === 0);
190-
assert(PThread.runningWorkers.length === 0);
191-
#endif
192-
193188
for (var worker of PThread.unusedWorkers) {
194-
#if ASSERTIONS
195-
// This Worker should not be hosting a pthread at this time.
196-
assert(!worker.pthread_ptr);
197-
#endif
198189
worker.terminate();
199190
}
200191
PThread.unusedWorkers = [];
192+
PThread.runningWorkers = [];
193+
PThread.pthreads = [];
201194
},
202195
returnWorkerToPool: function(worker) {
203196
// We don't want to run main thread queued calls here, since we are doing

0 commit comments

Comments
 (0)