-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Ensure Node worker threads are exited gracefully #12963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8246,6 +8246,7 @@ def test_pthread_cxx_threads(self): | |
def test_pthread_create_pool(self): | ||
# with a pool, we can synchronously depend on workers being available | ||
self.set_setting('PTHREAD_POOL_SIZE', '2') | ||
self.set_setting('EXIT_RUNTIME') | ||
self.emcc_args += ['-DALLOW_SYNC'] | ||
self.do_run_in_out_file_test('core/pthread/create.cpp') | ||
|
||
|
@@ -8261,12 +8262,14 @@ def test_pthread_create_proxy(self): | |
def test_pthread_create_embind_stack_check(self): | ||
# embind should work with stack overflow checks (see #12356) | ||
self.set_setting('STACK_OVERFLOW_CHECK', 2) | ||
self.set_setting('EXIT_RUNTIME') | ||
self.emcc_args += ['--bind'] | ||
self.do_run_in_out_file_test('core/pthread/create.cpp') | ||
|
||
@node_pthreads | ||
def test_pthread_exceptions(self): | ||
self.set_setting('PTHREAD_POOL_SIZE', '2') | ||
self.set_setting('EXIT_RUNTIME') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if we need this in all our unit test I don't think that necessarily means that every program wants that behaviour. Basically its always desirable for our unittests to exit the process when they are done, but that isn't true for JS programs in general. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. I worry this might be confusing but I agree we can't simplify it as easily as I thought. |
||
self.emcc_args += ['-fexceptions'] | ||
self.do_run_in_out_file_test('core/pthread/exceptions.cpp') | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know why this was here in the first place? I don't really understand the comment or why process.exit would ever maskde sense for exiting a thread. (unless perhaps process.exit only applies to the current worker thread which seem unlikely).
In order works I agree this change looks good I just can't see why it was ever any different. Perhaps @kripken can lend some background. I believe this code dates back to the very first pthread node code: #9745
Also, can you mention this fix in the title and/or PR description so it describes what you are fixing exacly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was discovered by trial-and-error as a attempt to fix the failing Node posix tests. So, I'm not entirely sure why it was added (with commit 5a8c86d) and whether this change is correct.
I'll fix the title and PR description, after I've investigated it a bit further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is adding EXIT_RUNTIME enough to make this test keep working?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some further research, it looks like the
process.exit()
call in a Node worker will immediately cause the thread to exit, and not the whole program. Somehow this call also prevents from new threads to be created(?) and causes the program to hang indefinitely (see for example the addedtest_pthread_exit.c
test case in 38b4bde which hangs on master). The failing stress-test mentioned in #9763 now also seems to work correctly on Node.EXIT_RUNTIME
doesn't make thetest_pthread_create
test case pass after this change, but calling explicitlyexit(0);
after cancelling the main loop does (see PR #13211).Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I added this because indeed the throw on the next line would shut down the entire program (threads + main runtime), as the comment says. And
process.exit
seemed to just shut down the thread, but I can't find any docs to justify that, so it may have been a reliance on undefined behavior...But this testcase generally shows that:
Replace the process.exit with the throw to see the result (that is, the throw stops everything, while the process.exit just stops the worker, but the main thread can keep printing "time" every second).
But, it does look good to me to remove the process.exit here, assuming the throw does not halt the entire program. Do we have tests checking that, that is, that a node pthread can exit while things keep running?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(If things work then I assume we catch that throw higher up.)