Fix weak reference creation#259
Merged
Merged
Conversation
acoates-ms
approved these changes
Nov 7, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fix Node-API weak reference creation for
HostObjectinstances.Issue Description
Recent hermes-windows releases trigger intermittent crashes in the RNW e2etest suite. A typical stack trace looks like this:
The underlying issues are:
We attempt to create a weak reference to a
HostObjectinstance.Weak reference creation adds an internal property to the
HostObjectso theNodeApiReferencecan receive finalization notifications.In failing cases, the
HostObject’s internal proxy is missing.Writing to a null proxy causes the crash.
The missing proxy might be due to GC or some other lifecycle quirk, but the weak-reference path has several problems regardless:
HostObject.HostObject.NodeApiEnvironment::getExternalPropertyValueusedgetNamedProperty, which walks the prototype chain even though internal properties must be own properties.napi_refobjects owned by user code, the finalizer does nothing; adding a finalizer is unnecessary.NodeApiReferencesubclasses ensured the referenced value stayed alive long enough.Solution
Refactored
NodeApiReference(and subclasses) wheninitialRefCount == 0, meaning we have to create a weak reference:needsFinalizerTracking()predicate that reports whether the weak reference needs finalizer notifications. (In the failing scenario, it now returnsfalse.)convertToWeakRootStorage()to honorneedsFinalizerTracking().create()helper now calls a newinitializeStorage()method after construction.initializeStorage()also handles the logic that keeps the value alive, so all three reference types now share the same protection.Hardened
NodeApiEnvironment::getExternalPropertyValue():HostObjectandJSProxyinstances.vm::JSObject::getOwnNamedDescriptorto access the internal slot without traversing the prototype chain.Testing
hermes.dll; no crashes observed.Microsoft Reviewers: Open in CodeFlow