-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Only a single console.log is logged from a worker when exiting #40961
Comments
Updated comment to link ongoing issue with Node: nodejs/node#40961
@nodejs/workers |
|
When #40813 lands, you will be able to wait for the async operations to be over before exiting like this: const {
Worker, isMainThread
} = require('worker_threads');
if (isMainThread) {
console.log('main');
const worker = new Worker(__filename, {});
} else {
console.log('worker1');
console.log('worker2');
console.log('worker3');
setInterval(() => process.getActiveResourcesInfo().length === 0 && process.exit(0));
} |
@RaisinTen Wouldn’t the interval also be a resource? 😉 I think in general the recommendation here should be to let workers finish naturally whenever possible instead of using |
@addaleax I don't think the console.log('before', process.getActiveResourcesInfo());
setImmediate(() => console.log('during', process.getActiveResourcesInfo()));
console.log('after', process.getActiveResourcesInfo()); $ ./node test.js
before []
after [
{ type: 'TTYWrap', asyncId: 2, triggerAsyncId: 1 },
{ type: 'Immediate', asyncId: 5, triggerAsyncId: 1 }
]
during [ { type: 'TTYWrap', asyncId: 2, triggerAsyncId: 1 } ]
👍 |
This is getting a bit off topic for this issue, but I'd consider that a bug. |
Thanks for the info @RaisinTen and @addaleax ! It's possible that with some work Emscripten can avoid calling |
Updated comment to link ongoing issue with Node: nodejs/node#40961
FWIW, we work around this in emscripten by using Without this, it makes the use of logging to debug of multi-threaded programs very hard, because normal program stdout/stderr doesn't interleave as expected with log messages. |
Hey, is it known if the |
That looks like the same issue yes. |
I'm aware it looks like the same issue, but is |
I think it stems from the use of This is very common in emscripten since workers that run pthreads normally never yield to the event loop. |
Thanks for the insight! |
Version
16.5.0
Platform
Linux64
Subsystem
No response
What steps will reproduce the bug?
Testcase:
Run with
node b.js
How often does it reproduce? Is there a required condition?
100% of the time.
What is the expected behavior?
I would expect to see
What do you see instead?
The main thread logged, but the worker only logged the first of three loggings.
Additional information
Somehow the first
console.log
is always logged out, but the other ones are stopped by the presence of theprocess.exit
after them. This does not seem to be a race in my testing: the first one is always shown, and none of the others.Returning to the main event loop after the first logging allows one more logging to show up. That is:
That prints out
worker2
. Once more a single logging appears of all those that are expected to happen, andworker3
is not logged.This was noticed in Emscripten here: emscripten-core/emscripten#14804 Emscripten will use
process.exit
when the program is done, and we were only getting the firstconsole.log
out of all those the program prints. (As a workaround we write synchonously to the fd for stdout, which does work.)The text was updated successfully, but these errors were encountered: