JSError.ThrowError(Exception) aborts the process when a Worker terminates during a .NET call
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 18/100
調査の方向性
Start with the static JSError.ThrowError(Exception) overload and its use in JSValue.InvokeCallback, then trace CreateErrorValueForException and JSError.Value to see where HasOwnProperty fails during termination. Run the minimal Worker repro to confirm the abort. Done means the hosted-CLR regression test exits cleanly. Linked PR #506 is already open against this issue, so check it before starting.
索引モデルが issue の本文から書いたものです。
説明
Summary
Calling worker.terminate() while a Node.js Worker is executing a .NET call through
node-api-dotnet reliably aborts the entire process with an unhandled
Microsoft.JavaScript.NodeApi.JSException.
The static JSError.ThrowError(Exception) callback error path is not safe when the Node-API
environment is terminating. It attempts to inspect or create JavaScript error values after V8 has
started terminating the Worker. Those Node-API operations return napi_pending_exception, causing
another JSException to escape the native callback boundary. CoreCLR then terminates the process
with SIGABRT (exit code 134).
This reproduces on node-api-dotnet 0.9.27 and is still present on the current main branch.
Environment
| Component | Version |
|---|---|
| node-api-dotnet | 0.9.27 |
| Node.js | 24.13.0 |
| .NET SDK/runtime | 10.0.302 / Microsoft.NETCore.App 10.0.10 |
| OS | Ubuntu 24.04.4 LTS, glibc 2.39, x64 |
| Container | mcr.microsoft.com/dotnet/sdk:10.0 |
The package includes the worker teardown fixes from #487, #492, and #495.
Minimal reproduction
minimal-worker.cjs:
'use strict';
const dotnet = require('node-api-dotnet/net10.0');
const { parentPort } = require('worker_threads');
parentPort.postMessage('ready');
for (;;) {
dotnet.System.String.Join(',', ['a', 'b', 'c']);
}
minimal.cjs:
'use strict';
const { Worker } = require('worker_threads');
(async () => {
for (let i = 0; i < 200; i++) {
const worker = new Worker(`${__dirname}/minimal-worker.cjs`);
await new Promise((resolve) => worker.once('message', resolve));
await new Promise((resolve) => setTimeout(resolve, 15));
await worker.terminate();
}
console.log('ALL_CLEAN');
})();
Run:
node minimal.cjs
Observed on the first iteration:
Cannot print exception string because Exception.ToString() failed.
Aborted (core dumped)
Exit code: 134 / SIGABRT.
The delay can be varied from 0 through 200 ms without changing the result.
Controls
| Worker state during teardown | Result |
|---|---|
| Pure JavaScript; node-api-dotnet never loaded | Clean exit |
| node-api-dotnet loaded, but no .NET call in flight | Clean exit |
| .NET call in flight | SIGABRT / exit 134 |
The same abort occurs with process.exit(0) while a Worker has a .NET call in flight, so this is
not specific to Worker.terminate().
Managed stack
An UnhandledException handler that avoids the overridden JSException.StackTrace property
captures:
Microsoft.JavaScript.NodeApi.JSException
An exception is pending: Error in HasOwnProperty at .../JSValue.cs:570
at Microsoft.JavaScript.NodeApi.JSValue.HasOwnProperty(JSValue key)
at Microsoft.JavaScript.NodeApi.JSError.get_Value()
at Microsoft.JavaScript.NodeApi.JSError.CreateErrorValueForException(Exception exception, ...)
at Microsoft.JavaScript.NodeApi.JSError.ThrowError(Exception exception)
at Microsoft.JavaScript.NodeApi.JSValue.InvokeCallback[TDescriptor](...)
at Microsoft.JavaScript.NodeApi.JSValue.InvokeJSMethod(...)
Under gdb, the process receives SIGABRT from CoreCLR's unhandled-managed-exception policy. This is
not a segmentation fault in the minimal reproduction.
Root cause
The callback path is:
- A JS-to-.NET call is executing in
JSValue.InvokeCallback. - Worker/environment termination begins.
- The managed callback fails and
InvokeCallbackcatches the exception. - The catch block calls static
JSError.ThrowError(Exception). ThrowError(Exception)callsCreateErrorValueForExceptionbefore checking whether the
environment already has a pending exception or is closing.- For a
JSException,CreateErrorValueForExceptionevaluatesError.Value. JSError.ValuecallsHasOwnProperty, which returnsnapi_pending_exceptionduring teardown.ThrowIfFailedcreates and throws anotherJSException.- That exception escapes the unmanaged callback boundary and the process aborts.
CreateErrorValueForException also performs additional Node-API operations for
Error.captureStackTrace and DefineProperties, which can fail if termination starts after an
initial state check.
The instance JSError.ThrowError() already handles the equivalent condition:
if (status == napi_status.napi_pending_exception)
{
// The environment must be terminating...
return;
}
The static overload does not provide the same protection before constructing the JavaScript error.
Proposed fix
Make static JSError.ThrowError(Exception) termination-safe:
- Obtain the current runtime and environment.
- Call the raw
IsExceptionPendingruntime API before constructing a JavaScript error. - Return if an exception is pending, or if the status is
napi_pending_exceptionor
napi_closing. - If termination starts while constructing or augmenting the JavaScript
Error, catch the
resultingJSExceptionand fall back to the direct runtimeThrowError(env, code, message)
API, which does not inspect existing JavaScript error objects. - Treat
napi_pending_exceptionandnapi_closingfrom the final throw as expected teardown
outcomes.
A regression test can make the race deterministic by:
- Calling a managed method in a Worker.
- Having the managed method call back into JS to signal that it started.
- Blocking briefly in managed code and then throwing.
- Terminating the Worker from the parent after receiving the started signal.
Without the fix, the child process aborts. With the proposed fix, the hosted-CLR test exits cleanly.
- 主要言語
- C#
- スター
- 787
- フォーク
- 80
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/node-api-dotnet のほかの issue
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
microsoft/node-api-dotnet#484 ·
-
Embedding args occasionally get trailing garbage on .NET (missing null terminator on a pooled buffer)対応中かも @tolmachev-pravo が 93 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/node-api-dotnet#481 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 70/100
microsoft/node-api-dotnet#504 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 82/100
microsoft/node-api-dotnet#502 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
microsoft/node-api-dotnet#486 · コメント 1 件 ·
microsoft/node-api-dotnet の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
ClickHouse/clickhouse-cs#689 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 7 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 65/100
vchelaru/FlatRedBall2#1481 ·
メンテナーはふだん 1 日以内に返信
-
ai-chat bug trivial
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
area-signalr
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
dotnet/aspnetcore#69842 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信