Repository navigation
[tests] Wait for a new GC bridge round in GC bridge tests - #13675
Merged
Merged
Conversation
The GC bridge tests inferred that their peers had been processed from "some bridge round finished" or from short polling timeouts. Bridge rounds run one at a time on a background thread, so a round that was already running when a test started could satisfy the wait without ever seeing the test's peers. Add GCBridgeTestHelpers.WaitForBridgeRoundsAsync(), which waits for three more completed rounds while forcing GCs with a throwaway peer. The third round must have started from a GC after the wait began. Tests then assert directly instead of polling, and use [Timeout] with cooperative cancellation for their deadline. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The GC bridge test stabilization changes have no active review issues.
No open issues 🎉
What changed in this PR
This PR stabilizes .NET for Android’s GC bridge tests by waiting for fresh bridge processing rather than relying on short polling windows or completion of an earlier round. It addresses the intermittent collection, finalization, and event-pair failures tracked in the linked issues.
Changes:
- Adds shared helpers that wait for three bridge rounds and collect unreachable peers.
- Updates affected tests to use cancellation-aware waits, 60-second cooperative timeouts, and direct assertions.
- Matches bridge Start/Stop events by event ID, allowing unrelated events between them.
| File | Description |
|---|---|
tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj |
Includes the shared bridge helper source. |
tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/TrimmableTypeMapTypeManagerTests.cs |
Uses the shared collection helper for registered-peer finalization. |
tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/ManagedObjectProxyTests.cs |
Waits for fresh rounds before checking Java-root retention and release. |
tests/Mono.Android-Tests/Mono.Android-Tests/Java.Interop/JavaMarshalGCBridgeTests.cs |
Updates cycle-lifetime waits and extracts reference-event assertions. |
tests/Mono.Android-Tests/Mono.Android-Tests/Android.Runtime/RuntimeEventSourceTests.cs |
Waits for fresh bridge events and tolerates interleaved event types. |
tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop/JavaObjectTest.cs |
Replaces collection and finalization polling with shared waits. |
tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop/GCBridgeTestHelpers.cs |
Adds cancellation-aware bridge-round and finalization helpers. |
Review effort: Balanced
Member
|
I checked build 1691928. The failures appear to predate this PR:
I don't see evidence that this PR introduced either failure. The allocation test can still make CI red after #13666. |
jonathanpeppers
approved these changes
Nov 20, 2026
9 tasks
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.
Fixes: #13637
Fixes: #13645
Fixes: #13638
Fixes: #13639
Fixes: #13275
Context: #13646
Several GC bridge tests fail intermittently on CI:
Cause
The bridge itself works as designed; the tests' waits are wrong. Bridge rounds run one at a time on a background thread. While a round is running, a GC does not start another one, and
JNIEnv.BridgeProcessingGenerationis incremented when a round ends. Each test made one of these assumptions:ManagedPeerCycle...,JavaObjectArrayRetainsManagedValueUntilReleased).ProcessBridgechecks whether the events are enabled once, when the round starts. A round already running when the listener is created writes neither event, but still bumps the generation, after which the test stops forcing GCs and waits 5 s for events that never come (GCBridgeRoundEmission).Dispose_Finalized,RegisteredPeer_Dispose_Finalized,UnreferencedInstanceIsCollected).Fix
GCBridgeTestHelpers.WaitForBridgeRoundsAsync()reads the generation and waits for three more rounds to complete, forcing a GC with a throwaway peer every 10 ms so a new round can always start. When the wait begins, one round may still be running and the next may already be queued with objects gathered by an earlier GC, so the third round to complete must have started from a GC after the wait began. That round has processed the test's peers. The second round's callback also started after the wait began, so its Start/Stop events have been written.CollectUnreachablePeersAsync()follows the wait with a GC,WaitForPendingFinalizers(), and a second GC.The tests now assert directly after the wait instead of polling a predicate. Their deadline is
[Timeout (60_000, CooperativeCancellation = true)], which only fires if something is actually broken.GCBridgeRoundEmissionnow matches Start and Stop by event ID, because TypeMap events from other threads can be written between them.Validation
Release builds on an arm64 API 35 emulator (
MAUI_android_35). Each harness called the test methods directly, in categoryBridgeStress, and was removed before committing:NativeAOT added
-p:PublishAot=true -p:Optimize=true -p:TestsFlavor=BridgeNativeAOT.An earlier round held open. An event listener blocks an unrelated bridge round on the bridge thread, then releases it after 100, 2,500, or 6,000 ms. This stands in for a slow round or one left over from earlier tests:
On NativeAOT,
GCBridgeRoundEmissionis Inconclusive by design, so 15 runs count.Background noise. A thread keeps creating unreachable Java peers in batches of 10 or 1,000 and forcing GCs, so rounds run back to back at about 4–11 ms each. Each test ran 100 times per batch size:
ManagedPeerCycle...The other originals don't fail under noise: their failures need a single round longer than their timeout, and nonstop rounds hide the
GCBridgeRoundEmissionrace.Plain repetition. Each test ran 200 times: 0 failures for both original and fixed on CoreCLR, and for fixed on NativeAOT. A fast local emulator doesn't produce the overlapping or slow rounds seen on CI. Fixed tests take about 40–80 ms each.
[Timeout]was checked on the device runner: a probe test with a 2 s timeout was cancelled after 2.003 s.1-hour parallel stress. The original and fixed builds ran side by side on two
MAUI_android_35emulators for 60 minutes each. Each cycle ran every test 10 times under noise with batches of 10, 10 times under noise with batches of 1,000, 10 times with no noise, and once each with an earlier round held for 100, 2,500, and 6,000 ms. A test still running after 90 s would have been recorded as a hang. Neither app crashed, so neither needed a restart.GCBridgeRoundEmissionManagedPeerCycleSurvivesJavaRootAndIsCollectedAfterReleaseDispose_FinalizedRegisteredPeer_Dispose_FinalizedUnreferencedInstanceIsCollectedJavaObjectArrayRetainsManagedValueUntilReleasedEvery original failure matched the CI message for that test. The slowest fixed run took 6.4 s, during a 6,000 ms held round.