Skip to content

[Android] Add cancellation handling in image loading - #39948

Merged
kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
Transis-Felipe:patch-3
Nov 11, 2026
Merged

kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
Transis-Felipe:patch-3

Conversation

@Transis-Felipe

@Transis-Felipe Transis-Felipe commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Handle cancellation during image loading by treating it as a failure.

Description of Change

Issues Fixed

Fixes this error

image

Handle cancellation during image loading by treating it as a failure.
@github-actions

github-actions Bot commented Nov 5, 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 -- 39948

Or

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

@azure-pipelines

This comment has been minimized.

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Nov 5, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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.

@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 6, 2026
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@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 — 1 finding

Reviewed commit 9d9fdc4.

See inline comments for details.

// no-op
events?.LoadingCompleted(false);
}
catch (Exception) when (cancellationToken.IsCancellationRequested)

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 - 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.

@Transis-Felipe Transis-Felipe Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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);
    }
}

@MauiBot

This comment has been minimized.

@Transis-Felipe Transis-Felipe left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could this be covered by the test I left in the 'Files changed' review?

@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 6, 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 — 1 finding

Reviewed commit 9d9fdc4.

See inline comments for details.

// no-op
events?.LoadingCompleted(false);
}
catch (Exception) when (cancellationToken.IsCancellationRequested)

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 - 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.

@MauiBot

MauiBot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@Transis-Felipe — existing-test verification for commit 9d9fdc46.

Result SKIPPED Platform Android Commit 9d9fdc46


🧪 Gate analysis — click to expand

⚠ Skipped. No runnable PR tests were selected. The fix has not been verified by this gate.

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

PR head: 9d9fdc46.

Without-fix baseline: 334e5077.

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

@kubaflo kubaflo 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.

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>
@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 11, 2026
@kubaflo

kubaflo commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

/rebase

@github-actions

Copy link
Copy Markdown
Contributor

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.

@MauiBot

MauiBot commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@Transis-Felipe — 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 11, 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 — 0 findings

Reviewed commit 89b5e84.

@kubaflo
kubaflo changed the base branch from main to candidate/11.0.1xx-sr2 November 11, 2026 20:51
@kubaflo
kubaflo merged commit 4665469 into dotnet:candidate/11.0.1xx-sr2 Nov 11, 2026
4 of 14 checks passed
kubaflo added a commit that referenced this pull request Nov 11, 2026
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>
kubaflo added a commit that referenced this pull request Nov 13, 2026
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>
kubaflo added a commit that referenced this pull request Nov 16, 2026
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>
kubaflo added a commit that referenced this pull request Nov 16, 2026
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>
kubaflo added a commit that referenced this pull request Nov 17, 2026
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>
kubaflo added a commit that referenced this pull request Nov 18, 2026
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>
kubaflo added a commit that referenced this pull request Nov 18, 2026
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>
kubaflo added a commit that referenced this pull request Nov 18, 2026
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>
kubaflo added a commit that referenced this pull request Nov 19, 2026
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>
kubaflo added a commit that referenced this pull request Nov 22, 2026
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>
kubaflo added a commit that referenced this pull request Nov 23, 2026
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>
@kubaflo kubaflo added this to the .NET 11 SR2 milestone Nov 24, 2026
KarthikRajaKalaimani pushed a commit to KarthikRajaKalaimani/maui that referenced this pull request Nov 26, 2026
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>
jfversluis added a commit that referenced this pull request Nov 26, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants