Commit 8000b3e
fix(wall): don't remove the env cleanup hook from inside itself (#360)
WallProfiler registers an environment cleanup hook (CleanupHook) via
node::AddEnvironmentCleanupHook so it can stop profiling when a worker
isolate is terminated without a beforeExit notification. On termination,
CleanupHook -> Cleanup -> Dispose called node::RemoveEnvironmentCleanupHook
for that same hook.
Node's cleanup-hook trampoline (CleanupHookThunkRun) invokes the hook and
then dereferences its internal hook record again to remove the hook itself.
Removing the hook from within the hook frees that record early, so when our
callback returns Node reads freed memory — a use-after-free that segfaults
on worker termination (observed reliably on Node 26, exercised by the
"should not crash when worker is terminated" test).
Add a removeCleanupHook flag to Dispose (default true). Cleanup, which only
runs from inside CleanupHook, passes false and lets Node remove the hook
itself. The still-running call sites (StopCore, StartImpl failure) keep
removing it, since there the hook is live and must not fire later against a
destroyed profiler.
Reproduced and verified fixed in an Alpine/musl Node 26 container under gdb:
crashed within 1-2 runs before, 30/30 clean after.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent c82c2e7 commit 8000b3e
3 files changed
Lines changed: 19 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
467 | 467 | | |
468 | 468 | | |
469 | 469 | | |
470 | | - | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
471 | 474 | | |
472 | 475 | | |
473 | 476 | | |
| |||
689 | 692 | | |
690 | 693 | | |
691 | 694 | | |
692 | | - | |
| 695 | + | |
693 | 696 | | |
694 | 697 | | |
695 | 698 | | |
| |||
704 | 707 | | |
705 | 708 | | |
706 | 709 | | |
707 | | - | |
708 | | - | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
709 | 714 | | |
710 | 715 | | |
711 | 716 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
117 | 124 | | |
118 | 125 | | |
119 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
0 commit comments