Repository navigation
[Android] Add cancellation handling in image loading - #39948
Conversation
Handle cancellation during image loading by treating it as a failure.
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 39948Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 39948" |
This comment has been minimized.
This comment has been minimized.
|
Hey there @@Transis-Felipe! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
Hey there @Transis-Felipe! 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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| // no-op | ||
| events?.LoadingCompleted(false); | ||
| } | ||
| catch (Exception) when (cancellationToken.IsCancellationRequested) |
There was a problem hiding this comment.
[moderate] Regression Prevention and Test Coverage - The existing Android cancellation test cannot exercise this new branch: InterruptingLoadCancelsAndStartsOver uses ThrowIfCancellationRequested(), which reaches the existing OperationCanceledException catch and passes without this fix. The regression test CancelledLoadThatFailsDoesNotUpdateDisconnectedHandler and the stub's FailWhenCancelled implementation are iOS-only. Add deterministic Android coverage that cancels a paused load through disconnect or source replacement, then makes the service throw a non-cancellation exception; assert LoadingCompleted(false), no LoadingFailed, and no clearing of a replacement image. Include the same failure with an uncancelled token to preserve image clearing and LoadingFailed (the cleanup introduced by #13614). Otherwise reverting this fix leaves the Android regression undetected.
There was a problem hiding this comment.
Could be this test ?
[Theory]
[InlineData(true)] // Prior cancellation scenario
[InlineData(false)] // Control scenario (uncancelled)
async Task AndroidImageLoading_CancellationFollowedByNonCancellationException(bool cancelLoad)
{
// Arrange: Configure the image loading stub to simulate delay and potential cancellation
// https://github.com/dotnet/maui/issues/38845
var imageSource = new TStub();
var tcsLoaded = new TaskCompletionSource<bool>();
// Stub allowing controlled pause and firing non-cancellation exception post-cancellation
var imageServiceStub = new ImageLoadingServiceStub();
imageServiceStub.OnLoadRequested = async (token) =>
{
// Simulate in-flight async operation
await Task.Delay(50, token);
if (cancelLoad)
{
// Simulate cancellation token firing
token.ThrowIfCancellationRequested();
}
// Simulate generic I/O network failure (non-cancellation) occurring after cancellation
throw new IOException("Simulated network failure after cancellation");
};
var image = new Microsoft.Maui.Controls.Image { Source = imageSource };
// Instantiate and register the Android platform handler
var handler = CreateHandler<ImageViewHandler>(image);
await InvokeOnMainThreadAsync(() => image.Handler = handler);
// Act: Initiate loading and trigger cancellation via source replacement or handler disconnect
var loadTask = imageServiceStub.LoadImageAsync(imageSource, handler.PlatformView, CancellationToken.None);
if (cancelLoad)
{
// Trigger cancellation by disconnecting or replacing image source
image.Source = null; // or handler.DisconnectHandler()
}
// Assert: Verify expected behavior requested by code reviewer
try
{
await loadTask;
}
catch (Exception ex) when (cancelLoad && ex is IOException)
{
// Non-cancellation exception during canceled state should be caught without crashing
}
if (cancelLoad)
{
// Expectations for canceled flow:
// - LoadingCompleted must evaluate to false
// - LoadingFailed must NOT be triggered on a disconnected handler
// - Replacement/existing image must not be incorrectly cleared
Assert.False(imageServiceStub.WasLoadingCompletedSuccessfully);
Assert.False(imageServiceStub.WasLoadingFailedInvoked);
Assert.False(imageServiceStub.WasReplacementImageCleared);
}
else
{
// Expectations for uncancelled control flow:
// - Preserve cleanup logic and LoadingFailed invocation introduced in #13614
Assert.True(imageServiceStub.WasLoadingFailedInvoked);
}
}
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| // no-op | ||
| events?.LoadingCompleted(false); | ||
| } | ||
| catch (Exception) when (cancellationToken.IsCancellationRequested) |
There was a problem hiding this comment.
[moderate] Regression Prevention and Test Coverage - No committed Android test exercises this catch. InterruptingLoadCancelsAndStartsOver uses the Android stub's ThrowIfCancellationRequested(), reaching the existing OperationCanceledException catch even without this fix. FailWhenCancelled and CancelledLoadThatFailsDoesNotUpdateDisconnectedHandler exercise non-cancellation failures only on iOS. Add deterministic Android coverage that pauses a handler-driven load, cancels it through disconnect or source replacement, then throws a non-cancellation exception; assert LoadingCompleted(false), no LoadingFailed, and no clearing of a replacement image. Include the same failure with an uncancelled token to preserve image clearing and LoadingFailed from #13614. The proposed test in feedback is not committed and directly invokes a service with CancellationToken.None, bypassing this catch rather than exercising it.
PR Test Gate
|
kubaflo
left a comment
There was a problem hiding this comment.
Could you please add tests?
Cover non-cancellation service failures after handler disconnect and source replacement, with an uncancelled failure control. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
|
/rebase |
|
Unable to rebase this branch automatically. It may have conflicts with its base branch, or the PR author has not allowed maintainer edits. Please rebase manually. |
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 — 0 findings
Reviewed commit 89b5e84.
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure. <!-- !!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING MAIN. !!!!!!! --> ### Description of Change <!-- Enter description of the fix in this section --> ### 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 this error <img width="1292" height="995" alt="image" src="https://github.com/user-attachments/assets/ed1b0a9e-a1cd-4de0-88ce-6b61da262efa" /> --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Resolves conflicts with #39948 (Android image load cancellation) and #38111 (Android stream images buffered to bytes). Main's stream service is taken; the URI in-memory byte loader is renamed to loadImageFromDownloadedBytes so it no longer collides with main's loadImageFromBytes (which decodes at the original size). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Handle cancellation during image loading by treating it as a failure.
Description of Change
Issues Fixed
Fixes this error