Skip to content

Fix error reporting during worker termination - #506

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

Saúl Ponce (GalaxiasKyklos) wants to merge 1 commit 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"

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>
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
Comment on lines +293 to +298
catch (JSException)
{
// Termination may begin after the check above, causing any of the N-API operations used
// to construct or augment the JS Error object to fail. Fall back to the direct runtime
// error API, which does not require creating or inspecting any JS values.
napi_status fallbackStatus = runtime.ThrowError(env, code: null, 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

2 participants