Skip to content

Fix Node.js exit for library builds with pthreads#26441

Open
brendandahl wants to merge 1 commit intoemscripten-core:mainfrom
brendandahl:pthread-library
Open

Fix Node.js exit for library builds with pthreads#26441
brendandahl wants to merge 1 commit intoemscripten-core:mainfrom
brendandahl:pthread-library

Conversation

@brendandahl
Copy link
Collaborator

When building without a main function (a library build), and using pthreads to do background work, the main Node.js thread has no event loop work to keep Node.js alive. The workers are also unreferenced when they start executing, so Node.js will exit prematurely before the background thread can finish its work.

To prevent this, change the worker reference logic so that we only unref() workers upon execution if HAS_MAIN is enabled. For library builds, we keep them referenced (worker.ref()) to keep the process alive while they are active.

We also ensure idle workers returned to the pool are unref()'d across all Node builds (not just with PROXY_TO_PTHREAD) so that an idle pool doesn't leak references preventing exit.

Fixes #23092

When building without a main function (a library build), and using pthreads
to do background work, the main Node.js thread has no event loop work to
keep Node.js alive. The workers are also unreferenced when they start
executing, so Node.js will exit prematurely before the background thread
can finish its work.

To prevent this, change the worker reference logic so that we only
`unref()` workers upon execution if `HAS_MAIN` is enabled. For library
builds, we keep them referenced (`worker.ref()`) to keep the process
alive while they are active.

We also ensure idle workers returned to the pool are `unref()`'d across
all Node builds (not just with `PROXY_TO_PTHREAD`) so that an idle
pool doesn't leak references preventing exit.

Fixes emscripten-core#23092
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

keeping node alive while workers are working

3 participants