Skip to content

worker: fix exit code for error thrown in uncaughtException handler - #38012

Closed
Linkgoron wants to merge 3 commits into
nodejs:masterfrom
Linkgoron:worker-throw-in-unhandled-exception
Closed

worker: fix exit code for error thrown in uncaughtException handler#38012
Linkgoron wants to merge 3 commits into
nodejs:masterfrom
Linkgoron:worker-throw-in-unhandled-exception

Conversation

@Linkgoron

@LinkgoronLinkgoron commented Mar 31, 2021

Copy link
Copy Markdown
Contributor

When an uncaughtException handler itself throws in a worker, the worker exits with an error code of 0 instead of 7 1, which happens because the worker thread global handler catches the error, and exitCode stays 0.

My fix only emits exit for "regular" unhandled exceptions (which shouldn't actually reach that code anyway, as it should set _exiting correctly in the "inner" handler) to emulate the same behaviour that happens in non-workers, where exit is not emitted when an error is thrown from a handler.

The process._exiting logic was essentially taken from here: https://github.com/nodejs/node/blob/master/lib/internal/process/execution.js#L167

Fixes: #37996

@nodejs-github-botnodejs-github-bot added the needs-ci PRs that need a full CI run. label Mar 31, 2021
@LinkgoronLinkgoron changed the title worker: fix exit code for error thrown in unhandled handlerworker: fix exit code for error thrown in uncaughtException handlerMar 31, 2021
Change worker exit code when the unhandled exception
handler throws from 0 to 7
fixes: nodejs#37996
@Linkgoron
Linkgoronforce-pushed the worker-throw-in-unhandled-exception branch from ddcff99 to 7cf8425CompareMarch 31, 2021 23:04
@nodejs-github-bot

This comment has been minimized.

@Prinzhorn

Copy link
Copy Markdown

Thanks for fixing this so quickly. I think the docs need an update as well if we're going with 7 and not 1

If the worker was terminated, the exitCode parameter is 1.

https://nodejs.org/api/worker_threads.html#worker_threads_event_exit

@Linkgoron

Copy link
Copy Markdown
ContributorAuthor

I've changed the exit code to 1 to conform with the documentation.

@nodejs-github-bot

This comment has been minimized.

@Linkgoron
Linkgoronforce-pushed the worker-throw-in-unhandled-exception branch from 287f22e to 7779d71CompareApril 1, 2021 11:23
@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@LinkgoronLinkgoron added the worker Issues and PRs related to Worker support. label Apr 3, 2021
@Linkgoron

Copy link
Copy Markdown
ContributorAuthor

@nodejs/workers

@nodejs-github-bot

This comment has been minimized.

@LinkgoronLinkgoron added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 5, 2021
@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

nodejs-github-bot commented Apr 6, 2021

Copy link
Copy Markdown
Collaborator

jasnell pushed a commit that referenced this pull request Apr 6, 2021
Change worker exit code when the unhandled exception
handler throws from 0 to 7
fixes: #37996
PR-URL: #38012Fixes: #37996
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@jasnell

Copy link
Copy Markdown
Member

Landed in 6986fa0

@jasnelljasnell closed this Apr 6, 2021
@Prinzhorn

Copy link
Copy Markdown

thanks!

Landed in 6986fa0

The 7 slipped in there again "from 0 to 7"

@Linkgoron

Linkgoron commented Apr 7, 2021

Copy link
Copy Markdown
ContributorAuthor

thanks!

Landed in 6986fa0

The 7 slipped in there again "from 0 to 7"

Yeah, it looks like I missed amending the commit message. The PR is good though, and correctly exits with 1.

targos pushed a commit that referenced this pull request May 1, 2021
Change worker exit code when the unhandled exception
handler throws from 0 to 7
fixes: #37996
PR-URL: #38012Fixes: #37996
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@danielleadamsdanielleadams mentioned this pull request May 3, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.needs-ciPRs that need a full CI run.workerIssues and PRs related to Worker support.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants

@Linkgoron@nodejs-github-bot@Prinzhorn@jasnell@benjamingr