Repository navigation
[Android] Fix ObjectDisposedException when Glide restarts stream images - #38111
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 38111Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 38111" |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
MauiBot
left a comment
There was a problem hiding this comment.
Expert Review — 1 findings
See inline comments for details.
|
this is The fix I've been waiting for. |
This comment has been minimized.
This comment has been minimized.
MauiBot
left a comment
There was a problem hiding this comment.
Expert Review — 3 findings
See inline comments for details.
This comment has been minimized.
This comment has been minimized.
99996d6 to
0b71c35
Compare
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 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. |
There was a problem hiding this comment.
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.
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 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. |
There was a problem hiding this comment.
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.
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 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. |
| { | ||
| if (stream is not null) | ||
| { | ||
| bytes = await Task.Run(() => GetStreamBytesAsync(stream, cancellationToken), cancellationToken); |
There was a problem hiding this comment.
[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(); |
There was a problem hiding this comment.
[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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
PR Test Gate
🧪 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. |
da07454
into
dotnet:candidate/11.0.1xx-sr2
…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>
…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>
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:
Root Cause of the issue:
Description of Change
Image loading API changes:
PlatformInterop.loadImageFromBytesmethods for bothImageViewandContextto allow loading images directly from byte arrays instead of streams, leveraging Glide for image rendering.StreamImageSourceServiceto read stream contents into a byte array before passing them to the newPlatformInterop.loadImageFromBytesmethods, and removed manual stream disposal and related resource management code.Testing improvements:
LoadDrawableAsyncSurvivesGlideRestart) to verify that image loading is robust even when Glide is stopped and restarted mid-operation, using a customBlockingReadStreamto simulate delayed reads.BlockingReadStreamhelper class in the test suite to enable fine-grained control over stream read operations during testing.Issues Fixed
Fixes #37834
Tested the behavior in the following platforms
Output
Beforefix-37834.mov
Afterfix-37834.mov