Skip to content

Fix MarshalMethods startup logcat race in device test - #12195

Merged
jonathanpeppers merged 4 commits into
mainfrom
jonathanpeppers-fix-marshal-startup-flake
Jul 22, 2026
Merged

Fix MarshalMethods startup logcat race in device test#12195
jonathanpeppers merged 4 commits into
mainfrom
jonathanpeppers-fix-marshal-startup-flake

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Pull Request

This test flake was caused by a timing race: MarshalMethodsAppRuns(CoreCLR) launched the app first and only then began logcat monitoring for XXX:OnStart done. On busy CI devices, that one-shot startup marker could be missed even when startup was successful.

This change starts logcat monitoring first, then launches MainActivity via an onMonitoringStarted callback so the listener is attached before startup logs are emitted. It also clears logcat immediately before this startup check to keep the buffer focused on current startup output.

  • Useful description of why the change is necessary.
  • Links to issues fixed
    • N/A
  • Unit tests
    • dotnet build src\Xamarin.Android.Build.Tasks\Xamarin.Android.Build.Tasks.csproj -v:minimal
    • dotnet build tests\MSBuildDeviceIntegration\MSBuildDeviceIntegration.csproj -v:minimal -p:BuildProjectReferences=false
    • dotnet test bin\TestDebug\MSBuildDeviceIntegration\net10.0\MSBuildDeviceIntegration.dll --filter Name~MarshalMethodsAppRuns (ran on device; failed earlier in app Build/Install before reaching the updated startup assertion path)

MarshalMethodsGCHangTests previously launched the app via RunProjectAndAssert and only then began logcat monitoring, which made the startup marker check racey under CI log volume. This change starts adb logcat monitoring first, then launches MainActivity via a callback once monitoring is attached, and clears logcat immediately before startup monitoring to keep the buffer focused on startup lines.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c491d761-523c-40ad-9374-4897fc12d51b
Copilot AI review requested due to automatic review settings July 21, 2026 14:04

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.

Pull request overview

Improves reliability of the MarshalMethodsAppRuns(CoreCLR) device test by eliminating a logcat timing race where the test could miss a one-shot startup marker on busy CI devices.

Changes:

  • Clear logcat before the startup assertion and start logcat monitoring before launching MainActivity.
  • Extend MonitorAdbLogcat to accept an onMonitoringStarted callback so callers can trigger actions only after monitoring begins.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/MSBuildDeviceIntegration/Tests/MarshalMethodsGCHangTests.cs Clears logcat and launches the app via a callback after logcat monitoring starts to avoid missing startup output.
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs Adds onMonitoringStarted callback support to MonitorAdbLogcat to allow “attach-before-run” monitoring patterns.

Ensure MonitorAdbLogcat always stops and waits for the adb logcat process even when onMonitoringStarted throws. Capture callback exceptions, run process cleanup in a finally path, then rethrow with original stack trace.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c491d761-523c-40ad-9374-4897fc12d51b
Use a single try/finally in MonitorAdbLogcat so onMonitoringStarted exceptions propagate naturally while adb logcat cleanup is always executed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c491d761-523c-40ad-9374-4897fc12d51b
@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Jul 21, 2026
@jonathanpeppers
jonathanpeppers merged commit b7bc99d into main Jul 22, 2026
42 of 44 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-fix-marshal-startup-flake branch July 22, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants