Repository navigation
fix(android): deliver GC notifications under a per-runtime lock - #91
Open
NathanWalker wants to merge 1 commit into
Open
NathanWalker wants to merge 1 commit into
NathanWalker wants to merge 1 commit into
Conversation
A GC batch could reach a worker runtime while it was being disposed: the GC thread took the runtime from the subscriber map, the worker thread unsubscribed and disposed its native Runtime, and the GC thread then entered notifyGc for it and crashed (SIGSEGV on the GC thread). Each subscriber now carries its own lock. A batch is delivered while holding that lock, and only if the subscriber is still subscribed; unsubscribe() removes the subscriber, then takes its lock to mark it unsubscribed. Once unsubscribe() returns, no GC thread is inside that runtime's notifyGc and none will enter it. The memory monitor's notifyGc() takes the same per-runtime lock instead of the listener-wide one, and unsubscribe() no longer takes the listener-wide lock, so subscribing or unsubscribing one runtime never waits on a GC delivery into another.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
This branch has not been deployed
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.
Split out of #89 at review request: GC delivery should not hold one lock shared by all runtimes. This fixes the delivery race with a lock per runtime.
Summary
A GC batch could reach a worker runtime while that runtime was being disposed:
Runtime.notifyGcinto the disposed runtime and crashed.unsubscribe(runtime)returns, no GC thread is inside that runtime'snotifyGc, and none will enter it.unsubscribe()must not be called from inside that runtime's JS scope, and the Javadoc says so. Every caller meets this:detachWorkerRuntimeafter the worker loop has exited and before dispose;detach()runs after React Native has stopped the JS thread.Evidence
Tested on an emulator (API 36, arm64, debug build) with V8.
TNS Workerstests.unsubscribeand the GC thread enteringnotifyGcfor the same runtime in the same millisecond, then the crash 8 ms later.ArgConverter::GetTypeLongCachewhen workers start at the same time, in both trees. It is out of scope here.Merge Danger
Door: two-way
Blast Radius: the GC notification path for every Android runtime (main and workers, both binding layers)
The change adds one rule:
unsubscribe()must not be called from inside the runtime's JS scope. Every current caller follows it. A future caller that breaks it would deadlock that runtime against the GC thread.