Skip to content

Worker exits with code 0 (and not 7) when throwing inside uncaughtException #37996

Closed
@Prinzhorn

Description

@Prinzhorn

This is the underlying bug that kept my workaround of #37988 from working.

What steps will reproduce the bug?

index.js

const path = require('path');
const { Worker } = require('worker_threads');
const worker = new Worker(path.join(__dirname, 'worker.js'));

worker
  .on('online', () => {
    console.log('online');
  })
  .on('error', (err) => {
    console.log('error: %s', err);
  })
  .on('exit', (code) => {
    console.log('exit: %s', code);
    process.exit();
  });

worker.js

process.on('uncaughtException', function () {
  throw new Error('uncaughtException');
});

throw new Error('oopsie');
node index.js

online
error: Error: uncaughtException
    at process.<anonymous> (/home/alex/src/issues/worker-exit-code/worker.js:2:9)
    at process.emit (events.js:315:20)
    at internal/process/execution.js:163:25
    at process.workerOnGlobalUncaughtException [as _fatalException] (internal/main/worker_thread.js:194:15)

exit: 0    <================

node worker.js
echo $?
7           <================

How often does it reproduce? Is there a required condition?

10/10 would reproduce again

What is the expected behavior?

Exit code 7 for the worker

Exit code >0 for the worker. Exit codes of workers don't seem to be aligned with the main process, I'm fine with anything other than 0.

What do you see instead?

Exit code 0, which is why my error handling didn't catch it

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions