diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index eabdc9e503a63..37e82174990a8 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -296,10 +296,8 @@ var LibraryPThread = { markAsFinished(d.thread); #endif } else if (cmd === 'loaded') { - worker.loaded = true; -#if ENVIRONMENT_MAY_BE_NODE && PTHREAD_POOL_SIZE - // Check that this worker doesn't have an associated pthread. - if (ENVIRONMENT_IS_NODE && !worker.pthread_ptr) { +#if ENVIRONMENT_MAY_BE_NODE + if (ENVIRONMENT_IS_NODE && !worker.strongref) { // Once worker is loaded & idle, mark it as weakly referenced, // so that mere existence of a Worker in the pool does not prevent // Node.js from exiting the app. @@ -604,7 +602,12 @@ var LibraryPThread = { // back to the main thread. #if ENVIRONMENT_MAY_BE_NODE if (ENVIRONMENT_IS_NODE) { - PThread.pthreads[thread].ref(); + var worker = PThread.pthreads[thread]; + worker.ref(); + // Also, record that we called strongref, in case this function is called + // bafore the 'loaded' callback from the thread (where we would normally + // `unref` it. + worker.strongref = 1; } #endif }, @@ -702,15 +705,6 @@ var LibraryPThread = { // in this file, and not from the external worker.js. msg.moduleCanvasId = threadParams.moduleCanvasId; msg.offscreenCanvases = threadParams.offscreenCanvases; -#endif -#if ENVIRONMENT_MAY_BE_NODE - if (ENVIRONMENT_IS_NODE) { - // Mark worker as weakly referenced once we start executing a pthread, - // so that its existence does not prevent Node.js from exiting. This - // has no effect if the worker is already weakly referenced (e.g. if - // this worker was previously idle/unused). - worker.unref(); - } #endif // Ask the worker to start executing its pthread entry point function. worker.postMessage(msg, threadParams.transferList); diff --git a/test/codesize/test_codesize_minimal_pthreads.json b/test/codesize/test_codesize_minimal_pthreads.json index 928806850fcd3..e454f0bb75e40 100644 --- a/test/codesize/test_codesize_minimal_pthreads.json +++ b/test/codesize/test_codesize_minimal_pthreads.json @@ -1,9 +1,9 @@ { - "a.out.js": 7367, + "a.out.js": 7365, "a.out.js.gz": 3587, "a.out.nodebug.wasm": 19037, "a.out.nodebug.wasm.gz": 8787, - "total": 26404, + "total": 26402, "total_gz": 12374, "sent": [ "a (memory)", diff --git a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json index 5477e9ba79e30..81eb5a88b9c87 100644 --- a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json +++ b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json @@ -1,10 +1,10 @@ { - "a.out.js": 7776, - "a.out.js.gz": 3791, + "a.out.js": 7774, + "a.out.js.gz": 3793, "a.out.nodebug.wasm": 19038, "a.out.nodebug.wasm.gz": 8788, - "total": 26814, - "total_gz": 12579, + "total": 26812, + "total_gz": 12581, "sent": [ "a (memory)", "b (exit)",