Skip to content

Fix error reporting during worker termination - #506

Open
Saúl Ponce (GalaxiasKyklos) wants to merge 2 commits into
microsoft:mainfrom
GalaxiasKyklos:fix-jserror-worker-termination
Open

Saúl Ponce (GalaxiasKyklos) wants to merge 2 commits into
microsoft:mainfrom
GalaxiasKyklos:fix-jserror-worker-termination

Conversation

@GalaxiasKyklos

Copy link
Copy Markdown
Contributor

Fixes #505.

Summary

  • avoid constructing or inspecting JavaScript error values when the Node-API environment already has a pending exception or is closing
  • fall back to the direct runtime ThrowError API when worker termination races JavaScript error construction
  • add unit coverage and a deterministic hosted-CLR worker termination regression test

Testing

  • dotnet test test\NodeApi.Test.csproj -f net10.0 -c Debug --filter "FullyQualifiedName~JSErrorTests"
  • dotnet test test\NodeApi.Test.csproj -f net10.0 -c Debug --filter "FullyQualifiedName~HostedClrTests&DisplayName~napi-dotnet/errors|FullyQualifiedName~HostedClrTests&DisplayName~worker_termination_during_call"

Copilot AI balanced review requested due to automatic review settings October 13, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The newly introduced fallback path lacks deterministic test coverage.

1 open finding
What changed in this PR

Improves worker-termination error handling so pending or closing Node-API environments do not abort the process.

Changes:

  • Adds termination-safe exception checks and direct-runtime fallback handling.
  • Adds unit and hosted-CLR regression coverage.
  • Excludes the hosted-specific regression from NativeAOT tests.
File Description
src/​NodeApi/​JSError.cs Implements termination-safe error reporting.
test/​JSErrorTests.cs Tests preflight termination states.
test/​TestCases/​napi-dotnet/​Errors.cs Adds the managed regression helper.
test/​TestCases/​napi-dotnet/​worker_termination_during_call.js Exercises worker termination during a managed call.
test/​NativeAotTests.cs Excludes the hosted-CLR-specific scenario.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/NodeApi/JSError.cs Resolved
@vmoroz
Vladimir Morozov (vmoroz) dismissed their stale review November 18, 2026 05:27

Approved the wrong PR by mistake.

@vmoroz

Copy link
Copy Markdown
Member

Saúl Ponce (@GalaxiasKyklos) , thank you for your contribution!
Could you please address the Copilot PR feedback and rebase the PR.
Then, it should be good to merge.

Avoid constructing JavaScript error values after a Node-API environment starts terminating, and add a deterministic hosted-worker regression test.\n\nFixes microsoft#505.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Cover the race where error construction throws after the environment preflight succeeds, including expected and unexpected direct ThrowError statuses.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 17:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Node-API failures after a successful preflight can trigger recursive JSError construction and abort the process before fallback handling is reached.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/NodeApi/JSError.cs
try
{
// Do not construct a JSError object here, because that would require a runtime context.
error = CreateErrorValueForException(exception, message);

This branch has not been deployed

No deployments
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.

JSError.ThrowError(Exception) aborts the process when a Worker terminates during a .NET call

3 participants