GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Finalize what the GC collects at deterministic points when recording/replaying by Andarist · Pull Request #171 · replayio/node

Conversation

@Andarist

@Andarist Andarist commented Oct 6, 2026

Copy link
Copy Markdown
Member

Fourth PR of the old-space-GC stack, on top of #161.

Old space GCs now run while recording, at points the replay does not reproduce, and which embedder objects die in one differs between the two. Cleanup that ran from a weak callback was therefore dropped or leaked until teardown: async_hooks destroy hooks never fired for collected resources, an open FileHandle/DirHandle the GC collected kept its fd, a collected N-API reference stayed unfinalized until teardown (never on process.exit()), external Buffer data with a free callback leaked, and every closed HandleWrap leaked its wrap and JS object.

This replaces the per-site "leak it under record/replay" gates with one mechanism, recordreplay::DeferredFinalization (src/node_deferred_finalization.h), modelled on the WeakRef/FinalizationRegistry port in deps/v8/src/replay/: the GC's callback only notes what was collected, and a microtasks-completed callback records the notes and runs the cleanup from there on both sides. Tracked objects get an id at creation when events are recorded; when replaying they are kept alive until the recorded release. Objects created where no id could be assigned keep the previous handling and its LeakMemory diagnostic. The deferred-finalization feature switches it off, with a subfeature per user.

Commits, each with a case on backend branch claude/node-recording-gc-tests:

  • GC-destroyed async ids are recorded and emitted from the poll. gc/async-hooks-destroy-collected: 593 AsyncResource and 321 promise destroys delivered on the recording, 0 with the feature off.
  • BaseObjects, with FileHandle and DirHandle first: fd close and warning happen at the same point on both sides. gc/open-file-handles-collected checks the warnings against the fds still open.
  • N-API references and Buffer::CallbackInfo via a Finalizable interface. Module finalizers now run inside a callback region so the N-API calls they make are replayed; a stub stands in where the driver passes the finalizer as null on replay, which the recording notes. native-modules/napi-add-finalizer-collected, and gc/backing-stores-collected now expects finalizations.
  • HandleWrap: closed wraps are deleted on their deterministic close path; a collected open weak wrap (IntervalHistogram, now tracked) is closed from the poll. gc/handle-wraps-collected.

Not changed: ContextifyScript keeps its leak. gc/vm-script-import-after-collection relies on import() working after the script object was collected, which deleting the wrapper at any point would break; that needs upstream's later design where the dynamic-import callback is not reached through the wrapper.

Verified locally against a local recorder build: the gc, native-modules and async-hooks groups pass (37 files), with record/replay agreement on every logged result. native-modules/napi-wrap still aborts on local builds because the pinned driver predates Op_napi_wrap; napi_add_finalizer covers the same reference path.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Andarist
Andarist added this pull request to stack #165 October 6, 2026 08:28
@Andarist
Andarist force-pushed the claude/gc-deferred-finalization branch from 62331c0 to 213e143 Compare October 6, 2026 10:33
@Andarist
Andarist force-pushed the claude/gc-deferred-finalization branch from 213e143 to 25bf666 Compare October 6, 2026 10:51
@Andarist
Andarist requested a review from Domiii October 7, 2026 09:11
@Andarist
Andarist force-pushed the claude/gc-deferred-finalization branch from 25bf666 to 1ed5a7f Compare October 7, 2026 09:52
Domiii
Domiii approved these changes Oct 7, 2026
View reviewed changes
Andarist and others added 6 commits October 7, 2026 10:22
@Andarist @claude
Record/replay the async ids the GC destroys
d65ce29
When recording/replaying, a destroy which comes from the GC (a weak
callback, or a wrap deleted by one) was dropped, since it happens at a
point the replay does not reproduce. async_hooks destroy hooks never
fired for collected resources, so domain's pairing map and userland
per-async-id maps grew for the life of the process.

Add recordreplay::DeferredFinalization, owned by the Environment: the
weak callback only notes the id, and a microtasks-completed callback
records the noted ids and emits the destroys from there, like V8's
WeakRef/FinalizationRegistry handling (deps/v8/src/replay/gc-poll.cc)
and the N-API reference poll. When replaying, the recorded ids are
emitted at the same checkpoint and the replay's own GC destroys are
dropped. The "deferred-finalization" feature switches this off, which
leaves the destroys dropped as before.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Andarist @claude
Close FileHandles and DirHandles the GC collects at a deterministic p…
40dc911
…oint

An open FileHandle or DirHandle the GC collected was leaked while
recording/replaying (EnterLeakMemory), since closing it schedules a
process warning, JS the replay could not run at the same point. Its fd
stayed open until the environment was torn down.

Extend recordreplay::DeferredFinalization to BaseObjects: a subclass
calls RecordReplayTrack() before MakeWeak(), which gives the object an
id when it is created at a deterministic point. When recording, the
weak callback then only notes the id and the next poll runs
OnGCCollect(); when replaying, MakeWeak() keeps the JS object alive and
the poll runs OnGCCollect() where the recording did, so the fd is
closed and the warning scheduled at the same point on both sides. An
object created where no id could be assigned keeps the previous
handling, with its LeakMemory diagnostic.

FileHandle and DirHandle are the first users.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Andarist @claude
Finalize N-API references and external Buffers the GC collects at a d…
c630557
…eterministic point

A collected N-API reference was left unfinalized until environment
teardown when recording/replaying (never on process.exit()), so the
native object behind every napi_wrap and napi_create_external leaked,
and external Buffer data with a free callback (Buffer::New with a
callback, napi_create_external_buffer) was leaked outright.

Generalize recordreplay::DeferredFinalization from BaseObjects to a
Finalizable interface, and make v8impl::Reference and
Buffer::CallbackInfo users: the second pass callback, or the backing
store's deleter (which runs on the sweeper's thread), only notes the id,
and the poll finalizes the reference or runs the free callback at the
next microtask checkpoint on both sides. When replaying, a tracked
reference's value and a tracked Buffer's ArrayBuffer are kept alive
until then. A reference or buffer created where no id could be assigned
keeps the previous handling, with a LeakMemory diagnostic.

Module finalizers run from an immediate scheduled at that point, so
node_napi_env__::CallFinalizer and the external Buffer finalizer now
call them inside a callback region, like a module's init: the N-API
calls a finalizer makes are recorded and replayed at the same point, and
the finalizer itself does not run when replaying. The replayed call that
created the reference or buffer may have no finalizer (the driver passes
it as null), so whether the recording had one is recorded and a stub
stands in for it when replaying.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Andarist @claude
Delete closed HandleWraps when recording/replaying, and close collect…
adc8a63
…ed IntervalHistograms at a deterministic point

HandleWrap::OnGCCollect leaked every wrap when recording/replaying,
including the closed wraps it is reached for from OnClose(), a path
which replays: each closed TCP, UDP, pipe or timer handle kept its C++
wrap and, through the wrap's strong handle, its JS object for the life
of the process. Delete closed wraps as upstream does; destroying them
has no effect beyond freeing memory.

An open wrap the GC collects (a weak one, e.g. IntervalHistogram) is
closed from DeferredFinalization::Poll when tracked, and still leaked
when it was created where no id could be assigned. IntervalHistogram
tracks itself, which replaces holding it strongly when replaying: a
tracked histogram is held until the recording shows the GC collected
it, and an untracked one as before.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Andarist @claude
Take the API interrupt ordered lock once per drain
f64fc30
RecordReplayInvokeApiInterruptCallbacksAtProgress took the ordered lock once
per queued entry, so the number of recorded lock rounds depended on the
queue's contents. An entry queued by code which only runs when recording,
a binary module calling v8::Isolate::RequestInterrupt (profilers, APM
agents, the @replayio/v8-test-hooks test module), exists only in the
recording's queue: the recording drained one more round than the replay,
the recorded lock order diverged, and the replay failed with a linker
mismatch.

Swap the queue out under one lock round and run the entries outside it.
The lock still orders pushes from other threads against the drain, and an
entry a callback queues waits for the next trigger on both sides, which
resetting record_replay_api_interrupt_triggered_ before the drain already
arranges.

Backed by the node-recording case v8/api-interrupts-at-progress, now on the
test hooks instead of %ScheduleGCInStackCheck(). Same change to be made in
the Chromium V8 fork, where the drain is identical.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Andarist @claude
Say "deterministic point", not "point which replays"
efd9451
What the record/replay bookkeeping needs is a point whose behaviour is
semantically deterministic between recording and replay, where the same
state is reached and the same decisions are made, so that a value recorded
or cleanup run there reproduces. "A point which replays" named it badly:
GC points replay too, at other places. Comments and diagnostics only.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Andarist Andarist changed the title Run the cleanup for what the GC collects at a point which replays Finalize what the GC collects at deterministic points when recording/replaying Oct 7, 2026
@Andarist
Andarist force-pushed the claude/gc-deferred-finalization branch from 1ed5a7f to efd9451 Compare October 7, 2026 10:43
@Andarist
Andarist merged commit 6ca972c into master Oct 7, 2026
2 of 3 checks passed

Uh oh!

There was an error while loading. Please reload this page.

bhackett1024 added a commit that referenced this pull request Nov 5, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
c92c470
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
bhackett1024 pushed a commit that referenced this pull request Nov 5, 2026
@Andarist
Finalize what the GC collects at deterministic points
0f67773
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Add
recordreplay::DeferredFinalization, owned by the Environment and added
to V8's GC poll as a participant: the GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@Andarist @bhackett1024 @claude
Add deferred finalization for what the GC collects
2d5cdfd
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@bhackett1024 @Andarist @claude
Add NAPI and native addon recording support
2446e79
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@Andarist @bhackett1024 @claude
Finalize what the GC collects at deterministic points
cac4bc9
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@bhackett1024 @Andarist @claude
Add NAPI and native addon recording support
6903662
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@Andarist @bhackett1024 @claude
Finalize what the GC collects at deterministic points
6f80406
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Add
recordreplay::DeferredFinalization, owned by the Environment and added
to V8's GC poll as a participant: the GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@bhackett1024 @Andarist @claude
Add NAPI and native addon recording support
bc8ca58
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@Andarist @bhackett1024 @claude
Finalize what the GC collects at deterministic points
983b9bb
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Add
recordreplay::DeferredFinalization, owned by the Environment and added
to V8's GC poll as a participant: the GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 6, 2026
@Andarist
Add deferred finalization for what the GC collects
64c3164
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 6, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
5b1420b
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
4b99cd0
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist @claude
Add NAPI and native addon recording support
e1b1832
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Node 24 splits the reference classes further, so the record/replay tracking
follows its factories rather than a single New().

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
16bc07a
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist @bhackett1024 @claude
Finalize what the GC collects at deterministic points
2c2eb55
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
e9b1f11
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist @claude
Add NAPI and native addon recording support
ab28e14
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Node 24 splits the reference classes further, so the record/replay tracking
follows its factories rather than a single New().

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist @bhackett1024 @claude
Finalize what the GC collects at deterministic points
b1bdd9e
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
286902a
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
dae8f42
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
56e7ef5
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
5757063
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
5d2e12c
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides. A module which
registered with NAPI_VERSION_EXPERIMENTAL still gets its finalizer inline,
which is what node does for it as well.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
f0bbf51
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
40b2194
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
f50164b
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
de34183
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
bc32eea
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
02d9913
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Node 24 splits the reference classes further, so the record/replay tracking
follows its factories rather than a single New().

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
449fc30
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
9d5bb64
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
8eeac5d
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Node 24 splits the reference classes further, so the record/replay tracking
follows its factories rather than a single New().

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
0989aa0
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
22c9870
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
af93ec9
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
4c326d7
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
be64421
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Add deferred finalization for what the GC collects
fa51411
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown.
recordreplay::DeferredFinalization, owned by the Environment, is added
to V8's GC poll as a participant: a GC's callback only notes what it
collected, and the poll writes the notes into the recording and runs
the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

This commit adds the mechanism, its ReplayGCPollParticipant contract
with V8 and the Environment's instance of it. The finalizers that track
objects with it follow, starting with the N-API references in the next
commit.

Rebase note: IsActive() and the number of values the poll records on a
participant's behalf have to be the same when recording and replaying,
so nothing here may depend on the replay's own heap.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
5443bca
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist added a commit that referenced this pull request Nov 10, 2026
@Andarist
Finalize what the GC collects at deterministic points
252cf10
Old space GCs run at points the replay does not reproduce, and which
objects die in one differs between the two, so cleanup from a weak
callback was dropped or leaked until teardown. Track what the GC
collects with the Environment's recordreplay::DeferredFinalization: the
GC's callback only notes what it collected, and the poll writes the
notes into the recording and runs the cleanup from there. When replaying, what the GC would collect is
kept alive until the poll names its id, so the cleanup runs in the same
state on both sides.

The notes are the async ids of resources the GC destroyed, so
async_hooks destroy hooks fire again, and the ids of tracked
Finalizables: BaseObjects (FileHandle, DirHandle, open HandleWraps,
IntervalHistogram), N-API references and external Buffers' free
callbacks. An object created where no id could be assigned keeps the
earlier handling and its LeakMemory diagnostic.

Where liveness is JS-visible it is recorded rather than read from the
replay's own heap: WeakReference, an active handle's owner, an async
context's resource. A legacy addon initializer reached through its
well-known symbol invalidates the recording, as the self-registering
ones already did.

An external Buffer's free callback is module code too, so
CallbackInfo::RecordReplayFinalize schedules an immediate for it instead of
running it where the poll was reached, for the same reason the deferred N-API
finalizers are handed to the environment: node's own deleter schedules one
(see OnBackingStoreFree), and the poll is reached at the same point when
recording and replaying, so the immediate it schedules is too.

Rebase note: nothing a weak callback, a backing store deleter or
another GC-driven path does may be observable; it either notes an id
for the GC poll or leaks through EnterLeakMemory, and only objects
created where events are recorded get an id. Check new upstream
finalizers, and keep every JS-visible answer about what is still alive
coming from the recording.

From #164, #171 and #197.

Co-authored-by: Brian Hackett <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Andarist pushed a commit that referenced this pull request Nov 10, 2026
@bhackett1024 @Andarist
Add NAPI and native addon recording support
c5cb3f0
Patch native addon registration for recording/replaying, add pointer
registration for addon handles, and serialize module content for
deterministic addon loading. Mark an addon's dlopen() for the driver
with the addon's name, so the driver attaches the addon wherever it is
loaded from instead of only under node_modules.

Record/replay whether a weak N-API reference's value is still alive. A
module reading one with napi_get_reference_value gets its value or,
once the GC collected it, nothing, and the GC runs at other points
when replaying, so the module could see a value the recording didn't
or the other way around. References the module creates in calls the
driver replays (RecordReplayIsInsideInterceptedCallback says which
those are) get an id and have their values held strongly when
replaying; the ids of references whose values the GC collected are
recorded whenever the module reads a reference, and the replay clears
exactly those references then. A collected reference's finalizer, and
its deletion when it deletes itself, are deferred to the next GC poll
on both sides through node's deferred finalization, with a stub
standing in for a finalizer the replayed call was not given, since
module finalizers never run when replaying.

A module's finalizer is module code, which node does not run from the GC: it
hands it to the environment's second pass, an immediate
(napi_env__::InvokeFinalizerFromGC). The deferred finalization hands it there
too rather than calling Finalize() itself, so that a finalizer never runs
nested inside whatever the module was doing when the poll was reached -- its
own napi_make_callback, say -- which never happens without the recorder.
Reaching that deferral from the GC is what a tracked reference cannot do,
since the GC runs at points which differ between recording and replaying;
reaching it from the poll is the same point on both sides.

Node 20 splits Reference::New into Reference, ReferenceWithData and
ReferenceWithFinalizer factories, and the references with finalizers are
exactly the latter two, so each factory tracks its reference rather than the
base one alone; the record/replay bookkeeping carries its own env and asks the
subclass that owns the finalizer about it, since node 20 keeps both off
Reference. Nothing tracked means nothing deferred to a GC poll, and
napi-add-finalizer-collected and napi-external-finalizers-collected then see
no finalizations at all, because without the deferral they only run at
environment teardown.

Node 24 splits the reference classes further, so the record/replay tracking
follows its factories rather than a single New().

Rebase note: native addon code does not run when replaying. The addon's
node_module struct is restored from the recording, and the exports
value returned by its init function is mapped back through
RegisterPointer/PointerId. Any new upstream path that calls an addon's
register/init function needs the same treatment or must invalidate the
recording, as the legacy nm_register_func path does. Everything a
module can observe about the GC's timing (whether a weak reference
still has a value, when a finalizer runs) has to be recorded and
replayed at fixed points instead of being left to the replay's own GC.

Includes later changes from #167, #168, #171 and #197.

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Domiii Domiii Domiii approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Andarist @Domiii
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.