Skip to content

[Tests] Avoid Java heap OOM in upload-reset test - #13270

Closed
simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-upload-test-oom-fix
Closed

simonrozsival wants to merge 1 commit into
mainfrom
simonrozsival-upload-test-oom-fix

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

UploadConnectionResetThrowsHttpRequestException can abort before reaching its connection-reset assertions on CI devices with a 16 MiB Java application heap. System.IO.MemoryStream.CopyToAsync() bypasses the requested copy-buffer size and writes its full 16 MiB backing array. OutputStreamInvoker marshals that entire array through JNI, requiring a 16,777,232-byte Java allocation and triggering OutOfMemoryError followed by JNI SIGABRT.

Use a test-only BoundedCopyMemoryStream whose CopyToAsync() reads and writes through an actual byte[bufferSize]. Keep the full 16 MiB payload, seekability, both HttpCompletionOption cases, [Repeat (3)], and all existing connection-reset, exception-chain, caller-cancellation, and rewind assertions.

This is the exact single-file OOM repair extracted from commit b2ff9d7 in #13110. It deliberately remains in that PR until this standalone fix merges. The test is already on main via #13125, so this repair has no dependency on the signing/resource-designer changes. No product changes, test disabling, or InstallAndRunTests.cs timeout changes are included.

Validation

The following build and device-test results are prior validation of the identical patch in the source worktree, not newly executed tests in this extraction worktree. All 22 AndroidMessageHandlerUpload tests passed in each configuration on a temporary API 35 arm64 emulator with dalvik.vm.heapgrowthlimit=16m and dalvik.vm.heapsize=576m. The larger absolute heap maximum kept Android system processes operational while ordinary application heaps retained the 16 MiB growth limit. Both connection-reset completion options retained [Repeat (3)].

Fresh extraction checks: git diff --check passed; the single-file diff and complete file content match the source commit byte-for-byte. The change is exactly 13 additions and 1 deletion in one file. No build/test suite was rerun for this extraction, and no CI rerun was requested.

Exact prior local commands and results
make prepare
make all

Result: both passed. The prior build required generating missing API 37.1/37.2 reference/documentation outputs and temporarily withholding an old stock Android workload registration during dependency installation; the registration was restored afterward, without repository build-configuration changes.

export ADB_TARGET='-s emulator-5580' ANDROID_SERIAL=emulator-5580

./dotnet-local.sh build tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj \
  -t:Install -c Debug -p:AdbTarget='-s emulator-5580' -p:Device=emulator-5580 \
  -p:AndroidSdkDirectory=/Users/simon/Library/Android/sdk \
  -p:JavaSdkDirectory=/Users/simon/android-toolchain/jdk-21 \
  -p:IncludeCategories=AndroidMessageHandlerUpload -v:quiet
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Debug \
    -p:AdbTarget='-s emulator-5580' -p:Device=emulator-5580 \
    -p:AndroidSdkDirectory=/Users/simon/Library/Android/sdk \
    -p:JavaSdkDirectory=/Users/simon/android-toolchain/jdk-21 \
    -p:IncludeCategories=AndroidMessageHandlerUpload \
    --report-trx --results-directory ../../../bin/TestDebug/TestResults
)

Result: Debug build/install passed; 22 passed, 0 failed, 0 skipped.

./dotnet-local.sh build tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj \
  -t:Install -c Release -p:AdbTarget='-s emulator-5580' -p:Device=emulator-5580 \
  -p:AndroidSdkDirectory=/Users/simon/Library/Android/sdk \
  -p:JavaSdkDirectory=/Users/simon/android-toolchain/jdk-21 \
  -p:IncludeCategories=AndroidMessageHandlerUpload -v:quiet
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release \
    -p:AdbTarget='-s emulator-5580' -p:Device=emulator-5580 \
    -p:AndroidSdkDirectory=/Users/simon/Library/Android/sdk \
    -p:JavaSdkDirectory=/Users/simon/android-toolchain/jdk-21 \
    -p:IncludeCategories=AndroidMessageHandlerUpload \
    --report-trx --results-directory ../../../bin/TestRelease/TestResults
)

Result: Release AAB build/install passed; 22 passed, 0 failed, 0 skipped.

./dotnet-local.sh build tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj \
  -t:Install -c Release -p:AndroidPackageFormats=apk \
  -p:AdbTarget='-s emulator-5580' -p:Device=emulator-5580 \
  -p:AndroidSdkDirectory=/Users/simon/Library/Android/sdk \
  -p:JavaSdkDirectory=/Users/simon/android-toolchain/jdk-21 \
  -p:IncludeCategories=AndroidMessageHandlerUpload -v:quiet
(
  cd tests/Mono.Android-Tests/Mono.Android-Tests
  ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release \
    -p:AndroidPackageFormats=apk \
    -p:AdbTarget='-s emulator-5580' -p:Device=emulator-5580 \
    -p:AndroidSdkDirectory=/Users/simon/Library/Android/sdk \
    -p:JavaSdkDirectory=/Users/simon/android-toolchain/jdk-21 \
    -p:IncludeCategories=AndroidMessageHandlerUpload \
    --report-trx --results-directory ../../../bin/TestRelease/TestResults
)

Result: APK-only Release build/install passed; 22 passed, 0 failed, 0 skipped. Release builds emitted existing trim-analysis warnings but no errors.


Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

No separate issue is associated with this extraction; related PRs are linked above.

MemoryStream.CopyToAsync writes the entire 16 MiB backing array at
once.  JNI marshals that array into Java, exceeding the small app heap
on CI devices before the intended connection reset can be asserted.

Use a test-only stream that copies through the requested buffer size.
Retain the full payload, seekability, repeated completion-option cases,
and the existing reset, exception-chain, and rewind assertions.

Extract only the upload-test repair from b2ff9d7 in PR #13110.
Leave that PR unchanged and exclude its device-launch timeout changes.
Prior validation of this identical patch passed make prepare, make all,
and all 22 upload tests in Debug, Release AAB, and APK-only Release on
an API 35 arm64 emulator with a 16 MiB Java app heap.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: b454c3dd-5c23-44f1-9492-868b00d4021a
Copilot AI balanced review requested due to automatic review settings October 27, 2026 08:56

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 narrow test-only change preserves regression coverage, has reported prior device validation of the identical patch, and has no unresolved findings.

No open issues 🎉

What changed in this PR

This test-only fix prevents Java heap exhaustion during the upload connection-reset regression test introduced in #13125, without changing production behavior.

Changes:

  • Adds BoundedCopyMemoryStream to copy through a buffer of the requested size rather than writing the entire backing array.
  • Preserves the 16 MiB payload, seekability, cancellation propagation, repeated completion-option cases, and existing assertions.
File Description
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Xamarin.Android.Net/​AndroidMessageHandlerUploadTests.cs Uses bounded upload buffers to avoid Java heap exhaustion during reset testing.

Review effort: Balanced

@simonrozsival

Copy link
Copy Markdown
Member Author

#13110 is closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants