Skip to content

[Android] Fix ObjectDisposedException when Glide restarts stream images - #38111

Merged
kubaflo merged 9 commits into
dotnet:candidate/11.0.1xx-sr2from
HarishwaranVijayakumar:fix-37834
Nov 19, 2026
Merged

kubaflo merged 9 commits into
dotnet:candidate/11.0.1xx-sr2from
HarishwaranVijayakumar:fix-37834

Conversation

@HarishwaranVijayakumar

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Details:

  • On Android, stream-backed images can throw ObjectDisposedException and remain blank when the Activity stops and resumes while Glide is still loading the image.

Root Cause of the issue:

  • MAUI passed Glide a single managed stream instance. When an Activity stopped, the custom Glide loader's cleanup closed that stream. Glide then restarted the request after resume using the same closed stream, causing  ObjectDisposedException  and leaving the image blank.

Description of Change

Image loading API changes:

  • Added PlatformInterop.loadImageFromBytes methods for both ImageView and Context to allow loading images directly from byte arrays instead of streams, leveraging Glide for image rendering.
  • Updated StreamImageSourceService to read stream contents into a byte array before passing them to the new PlatformInterop.loadImageFromBytes methods, and removed manual stream disposal and related resource management code.

Testing improvements:

  • Added a new test (LoadDrawableAsyncSurvivesGlideRestart) to verify that image loading is robust even when Glide is stopped and restarted mid-operation, using a custom BlockingReadStream to simulate delayed reads.
  • Introduced the BlockingReadStream helper class in the test suite to enable fine-grained control over stream read operations during testing.
  • Included necessary test imports for stream and threading support.

Issues Fixed

Fixes #37834

Tested the behavior in the following platforms

  • Windows
  • Android
  • iOS
  • Mac

Output

Before After
Beforefix-37834.mov
Afterfix-37834.mov

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 38111

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 38111"

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Sep 2, 2026
@azure-pipelines

This comment has been minimized.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@HarishwaranVijayakumar! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Sep 2, 2026

@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Expert Review — 1 findings

See inline comments for details.

MauiBot

This comment was marked as outdated.

@MauiBot MauiBot added s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-fix-win AI found a better alternative fix than the PR s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review) and removed s/agent-review-in-progress AI review is currently running for this PR labels Sep 2, 2026
@Transis-Felipe

Copy link
Copy Markdown
Contributor

this is The fix I've been waiting for.
Thank you very much

@vishnumenon2684 vishnumenon2684 added the do-not-merge Don't merge this PR label Sep 7, 2026
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Sep 10, 2026

@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Expert Review — 3 findings

See inline comments for details.

MauiBot

This comment was marked as outdated.

@MauiBot MauiBot added s/agent-gate-failed AI could not verify tests catch the bug and removed s/agent-review-in-progress AI review is currently running for this PR labels Sep 10, 2026
@vishnumenon2684

This comment has been minimized.

@kubaflo
kubaflo force-pushed the net11 branch 2 times, most recently from 99996d6 to 0b71c35 Compare November 18, 2026 10:52
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026
@MauiBot

MauiBot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@HarishwaranVijayakumar — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026

@github-actions github-actions Bot 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.

Expert Review — 2 findings

Reviewed commit b255f90.

Source assessment incomplete (8/9 dimensions); finding count is partial.

See inline comments for details.

Previously reported findings were not duplicated.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026
@MauiBot

MauiBot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@HarishwaranVijayakumar — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026

@github-actions github-actions Bot 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.

Expert Review — 2 findings

Reviewed commit b255f90.

Source assessment incomplete (8/9 dimensions); finding count is partial.

See inline comments for details.

Previously reported findings were not duplicated.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026
@MauiBot

MauiBot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@HarishwaranVijayakumar — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026

@github-actions github-actions Bot 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.

Expert Review — 2 findings

Reviewed commit b255f90.

See inline comments for details.

{
if (stream is not null)
{
bytes = await Task.Run(() => GetStreamBytesAsync(stream, cancellationToken), cancellationToken);

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.

[major] Logic and Correctness Verification - Pre-submission buffering still exposes a stale-image failure with three successive sources A -> B -> C. Start A and B with synchronously acquired streams and pending reads, then let canceled A finish while B is buffering. UpdateSourceAsync catches A's cancellation and returns null; ImageSourcePartLoader calls CompleteLoad(null), which disposes and clears B's current cancellation source without canceling it. Selecting C can therefore no longer cancel B. If C loads first and B finishes buffering afterward, B passes the cancellation check and replaces C's Glide request and drawable. Although the shared manager's ownership defect predates this PR and is tracked in #40421, it remains present in the pinned source; previously, synchronously acquired streams were submitted before reading, so the newer Glide request superseded them. Preserve successor cancellation ownership when completing an older load and cover this sequence through ImageSourcePartLoader; the direct-service cancellation test bypasses that manager.


await WaitForStage(trackingStream.ReadStarted.Task, "the source stream read to start");

requestManager.OnStop();

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.

[moderate] Regression Prevention and Test Coverage - This exercises submission while Glide is stopped, not the in-flight decode restart reported in #37834. ReadStarted fires during managed buffering, which cannot finish until AllowRead below, so this load has no Glide request when OnStop runs. RequestSubmitted and IsRunning == false then establish a pending request; OnStart begins its first decode. A model that loads once but fails when reused after cleanup is not distinguished, even though SourceDisposed can make this test fail against the old implementation. Keep this pending-load case and add deterministic synchronization inside Glide decoding so the request begins before OnStop, undergoes cleanup, and completes successfully after OnStart.

@sheiksyedm
sheiksyedm marked this pull request as ready for review November 19, 2026 11:59
@azure-pipelines

This comment has been minimized.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026
@MauiBot

MauiBot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@HarishwaranVijayakumar — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 19, 2026
@kubaflo
kubaflo changed the base branch from net11 to candidate/11.0.1xx-sr2 November 19, 2026 18:40
@kubaflo
kubaflo merged commit da07454 into dotnet:candidate/11.0.1xx-sr2 Nov 19, 2026
142 of 185 checks passed
@kubaflo kubaflo mentioned this pull request Nov 19, 2026
1 task
kubaflo added a commit that referenced this pull request Nov 22, 2026
…es (#38111)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Issue Details:

- On Android, stream-backed images can throw ObjectDisposedException and
remain blank when the Activity stops and resumes while Glide is still
loading the image.

### Root Cause of the issue:

- MAUI passed Glide a single managed stream instance. When an Activity
stopped, the custom Glide loader's cleanup closed that stream. Glide
then restarted the request after resume using the same closed stream,
causing  ObjectDisposedException  and leaving the image blank.

### Description of Change

**Image loading API changes:**

* Added `PlatformInterop.loadImageFromBytes` methods for both
`ImageView` and `Context` to allow loading images directly from byte
arrays instead of streams, leveraging Glide for image rendering.
* Updated `StreamImageSourceService` to read stream contents into a byte
array before passing them to the new
`PlatformInterop.loadImageFromBytes` methods, and removed manual stream
disposal and related resource management code.

**Testing improvements:**

* Added a new test (`LoadDrawableAsyncSurvivesGlideRestart`) to verify
that image loading is robust even when Glide is stopped and restarted
mid-operation, using a custom `BlockingReadStream` to simulate delayed
reads.
* Introduced the `BlockingReadStream` helper class in the test suite to
enable fine-grained control over stream read operations during testing.
* Included necessary test imports for stream and threading support.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #37834

### Tested the behavior in the following platforms
 
- [ ] Windows
- [x] Android
- [ ] iOS
- [ ] Mac

### Output
 
| Before | After |
|----------|----------|
| <video
src="https://github.com/user-attachments/assets/4a6b07f9-762a-4343-b24f-58890d9a927a">
| <video
src="https://github.com/user-attachments/assets/4e057627-16b0-4b91-9068-63654a4b43c0">
|







<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 23, 2026
…es (#38111)

<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!
<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Issue Details:

- On Android, stream-backed images can throw ObjectDisposedException and
remain blank when the Activity stops and resumes while Glide is still
loading the image.

### Root Cause of the issue:

- MAUI passed Glide a single managed stream instance. When an Activity
stopped, the custom Glide loader's cleanup closed that stream. Glide
then restarted the request after resume using the same closed stream,
causing  ObjectDisposedException  and leaving the image blank.

### Description of Change

**Image loading API changes:**

* Added `PlatformInterop.loadImageFromBytes` methods for both
`ImageView` and `Context` to allow loading images directly from byte
arrays instead of streams, leveraging Glide for image rendering.
* Updated `StreamImageSourceService` to read stream contents into a byte
array before passing them to the new
`PlatformInterop.loadImageFromBytes` methods, and removed manual stream
disposal and related resource management code.

**Testing improvements:**

* Added a new test (`LoadDrawableAsyncSurvivesGlideRestart`) to verify
that image loading is robust even when Glide is stopped and restarted
mid-operation, using a custom `BlockingReadStream` to simulate delayed
reads.
* Introduced the `BlockingReadStream` helper class in the test suite to
enable fine-grained control over stream read operations during testing.
* Included necessary test imports for stream and threading support.

### Issues Fixed

<!-- Please make sure that there is a bug logged for the issue being
fixed. The bug should describe the problem and how to reproduce it. -->

Fixes #37834

### Tested the behavior in the following platforms
 
- [ ] Windows
- [x] Android
- [ ] iOS
- [ ] Mac

### Output
 
| Before | After |
|----------|----------|
| <video
src="https://github.com/user-attachments/assets/4a6b07f9-762a-4343-b24f-58890d9a927a">
| <video
src="https://github.com/user-attachments/assets/4e057627-16b0-4b91-9068-63654a4b43c0">
|







<!--
Are you targeting main? All PRs should target the main branch unless
otherwise noted.
-->

Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-image Image control community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/android s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review)

Projects

None yet

5 participants