Skip to content

[tests] Wait for a new GC bridge round in GC bridge tests - #13675

Merged
jonathanpeppers merged 1 commit into
mainfrom
simonrozsival-gc-bridge-flakiness
Nov 20, 2026
Merged

jonathanpeppers merged 1 commit into
mainfrom
simonrozsival-gc-bridge-flakiness

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes: #13637
Fixes: #13645
Fixes: #13638
Fixes: #13639
Fixes: #13275
Context: #13646

Several GC bridge tests fail intermittently on CI:

ManagedPeerCycleSurvivesJavaRootAndIsCollectedAfterRelease
  Expected collection to not be empty. The Java-rooted cycle must participate in global-to-weak bridge processing.
GCBridgeRoundEmission
  GC bridge Start and Stop events should be emitted as matched pairs.
Dispose_Finalized / RegisteredPeer_Dispose_Finalized
  Expected ...Dispose(disposing: false) to run.
UnreferencedInstanceIsCollected
  Expected the unreferenced instance to be collected.

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.BridgeProcessingGeneration is incremented when a round ends. Each test made one of these assumptions:

  • "The generation changed, so my peers were processed." A round that started before the test's peers existed can finish during the wait (ManagedPeerCycle..., JavaObjectArrayRetainsManagedValueUntilReleased).
  • "The next round writes Start/Stop events." ProcessBridge checks 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).
  • "The object will be finalized within 2 s." A slow round, or one left over from an earlier test, uses up the whole budget (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. GCBridgeRoundEmission now 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 category BridgeStress, and was removed before committing:

./dotnet-local.sh build -t:Install -c Release tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -p:Device=emulator-5554 -p:RuntimeIdentifier=android-arm64 -p:IncludeCategories=BridgeStress
../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:Device=emulator-5554 -p:RuntimeIdentifier=android-arm64 -p:IncludeCategories=BridgeStress --report-trx

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:

Original Fixed, CoreCLR Fixed, NativeAOT
Failures 11 of 18 0 of 18 0 of 15

On NativeAOT, GCBridgeRoundEmission is 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:

Original Fixed
ManagedPeerCycle... 17 of 200 failed, with the CI message 0 of 200
Other five tests 0 of 1,000 0 of 1,000

The other originals don't fail under noise: their failures need a single round longer than their timeout, and nonstop rounds hide the GCBridgeRoundEmission race.

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_35 emulators 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.

Test Original Fixed
GCBridgeRoundEmission 224 / 2,000 (44 in the no-noise phase) 0 / 1,672
ManagedPeerCycleSurvivesJavaRootAndIsCollectedAfterRelease 105 / 1,996 (91 under noise) 0 / 1,670
Dispose_Finalized 120 / 2,000 0 / 1,670
RegisteredPeer_Dispose_Finalized 120 / 2,000 0 / 1,670
UnreferencedInstanceIsCollected 120 / 2,000 0 / 1,670
JavaObjectArrayRetainsManagedValueUntilReleased 60 / 2,000 0 / 1,670
Total 749 / 11,996 0 / 10,022, no hangs

Every original failure matched the CI message for that test. The slowest fixed run took 6.4 s, during a 6,000 ms held round.

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>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 11:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@jonathanpeppers

Copy link
Copy Markdown
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
jonathanpeppers merged commit 3d8e787 into main Nov 20, 2026
32 of 39 checks passed
@jonathanpeppers
jonathanpeppers deleted the simonrozsival-gc-bridge-flakiness branch November 20, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants