Skip to content

Commit 3e8b91a

Browse files
GalaxiasKyklosCopilotjasongin
authored
Fix worker_threads teardown crash (SIGSEGV) and hang when node-api-dotnet is loaded in a Worker (#487)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Jason Ginchereau <jasongin@github.com> Copilot-Session: 144c87db-8d78-474f-bff9-21031a23e3e7
1 parent 04a65d2 commit 3e8b91a

4 files changed

Lines changed: 250 additions & 1 deletion

File tree

‎src/NodeApi/DotNetHost/NativeHost.cs‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,155 @@ public static void Trace(string msg)
4343
}
4444
}
4545

46+
private static bool s_moduleUnloadPrevented;
47+
48+
/// <summary>
49+
/// Pins this native host module in memory so the OS never unloads it.
50+
/// </summary>
51+
/// <remarks>
52+
/// This native host is compiled with NativeAOT, so it embeds a .NET runtime whose
53+
/// per-thread cleanup is registered with the OS via a <c>pthread_key</c> destructor that
54+
/// points into this module's own code. Node.js unloads (<c>dlclose</c>) an addon when the
55+
/// environment that loaded it is torn down. When a <c>worker_threads</c> Worker loads this
56+
/// module and is then terminated, Node unloads the module while the worker's OS thread is
57+
/// still alive; the still-registered destructor then points at unmapped memory and the
58+
/// process crashes with SIGSEGV as the thread exits (glibc <c>__nptl_deallocate_tsd</c>).
59+
/// Keeping the module mapped for the lifetime of the process keeps that destructor valid.
60+
/// <para/>
61+
/// This is scoped to Linux (glibc) and macOS, where the native host can be unloaded before
62+
/// the worker thread exits; on Windows module/thread teardown does not hit this issue. The
63+
/// pin is best-effort: any failure is traced but does not block init.
64+
/// </remarks>
65+
private static unsafe void PreventModuleUnload()
66+
{
67+
if (s_moduleUnloadPrevented)
68+
{
69+
return;
70+
}
71+
72+
s_moduleUnloadPrevented = true;
73+
74+
bool isMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
75+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !isMacOS)
76+
{
77+
return;
78+
}
79+
80+
try
81+
{
82+
// Resolve the file path of this shared library from the address of one of its
83+
// own functions, then re-open it with RTLD_NODELETE so it is never unmapped.
84+
nint moduleFunction =
85+
(nint)(delegate* unmanaged[Cdecl]<napi_env, napi_value, napi_value>)
86+
&InitializeModule;
87+
88+
nint fileName = default;
89+
if (DlAddr(moduleFunction, out Dl_info info) != 0)
90+
{
91+
fileName = info.dli_fname;
92+
}
93+
94+
if (fileName != default)
95+
{
96+
// RTLD_NOLOAD resolves the already-loaded module without loading a new copy;
97+
// RTLD_NODELETE keeps it mapped for the process lifetime. The extra (never
98+
// released) reference also prevents Node's dlclose from unmapping it.
99+
const int RTLD_LAZY = 0x0001;
100+
const int RTLD_NOLOAD_LINUX = 0x0004;
101+
const int RTLD_NODELETE_LINUX = 0x1000;
102+
const int RTLD_NOLOAD_MACOS = 0x0010;
103+
const int RTLD_NODELETE_MACOS = 0x0080;
104+
int flags = RTLD_LAZY | (isMacOS ?
105+
RTLD_NOLOAD_MACOS | RTLD_NODELETE_MACOS :
106+
RTLD_NOLOAD_LINUX | RTLD_NODELETE_LINUX);
107+
nint handle = DlOpen(fileName, flags);
108+
Trace($" Pinned native host module ({(handle != default ? "ok" : "no-op")}).");
109+
}
110+
else
111+
{
112+
Trace(" Could not resolve native host module path to pin it.");
113+
}
114+
}
115+
catch (Exception ex)
116+
{
117+
Trace(" Failed to pin native host module: " + ex);
118+
}
119+
}
120+
121+
// dladdr and dlopen are exported by libSystem on macOS. On Linux they are exported by
122+
// libc.so.6 on glibc >= 2.34, but by libdl.so.2 on older glibc versions.
123+
private static int DlAddr(nint addr, out Dl_info info)
124+
{
125+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
126+
{
127+
return DlAddrLibSystem(addr, out info);
128+
}
129+
130+
try
131+
{
132+
return DlAddrLibc(addr, out info);
133+
}
134+
catch (Exception ex) when (ex is EntryPointNotFoundException or DllNotFoundException)
135+
{
136+
return DlAddrLibdl(addr, out info);
137+
}
138+
}
139+
140+
private static nint DlOpen(nint fileName, int flags)
141+
{
142+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
143+
{
144+
return DlOpenLibSystem(fileName, flags);
145+
}
146+
147+
try
148+
{
149+
return DlOpenLibc(fileName, flags);
150+
}
151+
catch (Exception ex) when (ex is EntryPointNotFoundException or DllNotFoundException)
152+
{
153+
return DlOpenLibdl(fileName, flags);
154+
}
155+
}
156+
157+
[StructLayout(LayoutKind.Sequential)]
158+
private struct Dl_info
159+
{
160+
public nint dli_fname;
161+
public nint dli_fbase;
162+
public nint dli_sname;
163+
public nint dli_saddr;
164+
}
165+
166+
[LibraryImport("libc.so.6", EntryPoint = "dladdr")]
167+
private static partial int DlAddrLibc(nint addr, out Dl_info info);
168+
169+
[LibraryImport("libdl.so.2", EntryPoint = "dladdr")]
170+
private static partial int DlAddrLibdl(nint addr, out Dl_info info);
171+
172+
[LibraryImport("libc.so.6", EntryPoint = "dlopen")]
173+
private static partial nint DlOpenLibc(nint filename, int flags);
174+
175+
[LibraryImport("libdl.so.2", EntryPoint = "dlopen")]
176+
private static partial nint DlOpenLibdl(nint filename, int flags);
177+
178+
[LibraryImport("/usr/lib/libSystem.B.dylib", EntryPoint = "dladdr")]
179+
private static partial int DlAddrLibSystem(nint addr, out Dl_info info);
180+
181+
[LibraryImport("/usr/lib/libSystem.B.dylib", EntryPoint = "dlopen")]
182+
private static partial nint DlOpenLibSystem(nint filename, int flags);
183+
46184
[UnmanagedCallersOnly(
47185
EntryPoint = nameof(napi_register_module_v1),
48186
CallConvs = new[] { typeof(CallConvCdecl) })]
49187
public static napi_value InitializeModule(napi_env env, napi_value exports)
50188
{
51189
Trace($"> NativeHost.InitializeModule({env.Handle:X8}, {exports.Handle:X8})");
52190

191+
// Ensure this native module stays loaded for the lifetime of the process. See
192+
// PreventModuleUnload() for details on the worker-thread teardown crash this avoids.
193+
PreventModuleUnload();
194+
53195
s_jsRuntime ??= new NodejsRuntime();
54196

55197
// The native host JSValueScope is not disposed after a successful initialization. It

‎src/NodeApi/Interop/JSSynchronizationContext.cs‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Runtime.CompilerServices;
6+
using System.Runtime.InteropServices;
57
using System.Threading;
68
using System.Threading.Tasks;
9+
using Microsoft.JavaScript.NodeApi.Runtime;
10+
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
11+
using static Microsoft.JavaScript.NodeApi.Runtime.NodejsRuntime;
712

813
namespace Microsoft.JavaScript.NodeApi.Interop;
914

@@ -244,32 +249,85 @@ public Task<T> RunAsync<T>(Func<Task<T>> asyncAction)
244249
}
245250
}
246251

247-
internal sealed class JSTsfnSynchronizationContext : JSSynchronizationContext
252+
internal sealed unsafe class JSTsfnSynchronizationContext : JSSynchronizationContext
248253
{
254+
private readonly JSRuntime _runtime;
255+
private readonly napi_env _env;
249256
private readonly JSThreadSafeFunction _tsfn;
257+
private GCHandle _cleanupHandle;
250258

251259
public JSTsfnSynchronizationContext()
252260
{
261+
_runtime = JSValueScope.Current.Runtime;
262+
_env = (napi_env)JSValueScope.Current;
253263
_tsfn = new JSThreadSafeFunction(
254264
maxQueueSize: 0,
255265
initialThreadCount: 1,
256266
asyncResourceName: (JSValue)nameof(JSSynchronizationContext));
257267

258268
// Unref TSFN to indicate that this TSFN is not preventing Node.JS shutdown.
259269
_tsfn.Unref();
270+
271+
// Node runs environment cleanup hooks in reverse registration order. Registering this
272+
// after the TSFN ensures it is released before Node closes the TSFN's libuv handle.
273+
_cleanupHandle = GCHandle.Alloc(this);
274+
napi_status status = _runtime.AddEnvCleanupHook(
275+
_env,
276+
new napi_cleanup_hook(s_cleanup),
277+
(nint)_cleanupHandle);
278+
if (status != napi_status.napi_ok)
279+
{
280+
_cleanupHandle.Free();
281+
_tsfn.Release();
282+
status.ThrowIfFailed();
283+
}
260284
}
261285

262286
public override void Dispose()
263287
{
264288
if (IsDisposed) return;
265289

290+
if (_cleanupHandle.IsAllocated)
291+
{
292+
_runtime.RemoveEnvCleanupHook(
293+
_env,
294+
new napi_cleanup_hook(s_cleanup),
295+
(nint)_cleanupHandle).ThrowIfFailed();
296+
_cleanupHandle.Free();
297+
}
298+
266299
base.Dispose();
267300

268301
// Destroy TSFN by releasing last thread use count.
269302
// TSFN is deleted after this point and must not be used.
270303
_tsfn.Release();
271304
}
272305

306+
#if !UNMANAGED_DELEGATES
307+
private static readonly napi_cleanup_hook.Delegate s_cleanup = Cleanup;
308+
#else
309+
private static readonly unsafe delegate* unmanaged[Cdecl]<nint, void> s_cleanup = &Cleanup;
310+
#endif
311+
312+
#if UNMANAGED_DELEGATES
313+
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
314+
#endif
315+
private static unsafe void Cleanup(nint data)
316+
{
317+
GCHandle cleanupHandle = GCHandle.FromIntPtr(data);
318+
JSTsfnSynchronizationContext context =
319+
(JSTsfnSynchronizationContext)cleanupHandle.Target!;
320+
context._cleanupHandle = default;
321+
try
322+
{
323+
context.Dispose();
324+
}
325+
finally
326+
{
327+
cleanupHandle.Free();
328+
}
329+
}
330+
273331
/// <summary>
274332
/// Increment reference count for the main loop async resource.
275333
/// Non-zero count prevents Node.JS process from exiting.

‎test/NativeAotTests.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public class NativeAotTests
1818

1919
public static IEnumerable<object[]> TestCases { get; } = ListTestCases((testCaseName) =>
2020
!testCaseName.Contains("/dynamic_") &&
21+
22+
// The worker_teardown case validates NativeHost.PreventModuleUnload(), which only runs
23+
// when the hosted host module (Microsoft.JavaScript.NodeApi.node) is loaded. A generated
24+
// NativeAOT module has its own entry point that never calls it, so this case is only
25+
// meaningful under the hosted CLR host. See HostedClrTests.
26+
!testCaseName.Contains("/worker_teardown") &&
2127
!testCaseName.StartsWith("projects/", StringComparison.Ordinal));
2228

2329
[Theory]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
// Regression test for a worker_threads teardown crash.
5+
//
6+
// When the native host is loaded ONLY inside a Worker (so no other reference keeps the
7+
// module mapped) and the Worker is then terminated, Node.js unloads (dlclose) the addon
8+
// while the worker's OS thread is still exiting. For a NativeAOT host that leaves a
9+
// dangling pthread-key destructor, so the process crashes with SIGSEGV as the thread exits.
10+
// NativeHost.PreventModuleUnload() pins the module to prevent that. This test fails (the
11+
// child node process exits non-zero) if the crash regresses.
12+
//
13+
// This validates the hosted host module (Microsoft.JavaScript.NodeApi.node), which is what
14+
// PreventModuleUnload() pins, so it runs under HostedClrTests only (excluded from
15+
// NativeAotTests, whose generated module has a separate entry point).
16+
//
17+
// The binding is intentionally NOT loaded on the main thread: doing so would keep another
18+
// module reference alive and mask the unload crash (which is why multi_instance.js cannot
19+
// cover this case).
20+
21+
const assert = require('assert');
22+
const { Worker, isMainThread, parentPort } = require('worker_threads');
23+
24+
if (isMainThread) {
25+
const worker = new Worker(__filename);
26+
worker.on('error', (err) => { throw err; });
27+
worker.once('message', (message) => {
28+
assert.strictEqual(message, 'ready');
29+
// Let the worker settle, then tear it down. An unfixed host crashes during the
30+
// worker thread's teardown after the module is unloaded.
31+
setTimeout(async () => {
32+
await worker.terminate();
33+
// Keep the process alive briefly so any teardown crash surfaces as a non-zero
34+
// exit code instead of being skipped by an immediate process exit.
35+
setTimeout(() => process.exit(0), 300);
36+
}, 300);
37+
});
38+
} else {
39+
// Load the native host ONLY in the worker.
40+
const binding = require('../common').binding;
41+
assert.ok(binding);
42+
parentPort.postMessage('ready');
43+
}

0 commit comments

Comments
 (0)