Skip to content

Conversation

@zzyzy
Copy link
Contributor

@zzyzy zzyzy commented Aug 8, 2025

New versions of http, cronet_http and cupertino_http supports request cancellation (just like the HttpClient from dart:io).

Minor change to use AbortableRequest and pass the cancelFuture to the abortTrigger argument.

New Pull Request Checklist

  • I have read the Documentation
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

Additional context and info (if any)

Testing with ASP.NET Core API

Test Flutter app (Android and iOS only):
https://github.com/zzyzy/dio_lzz_fork_flutter/blob/main/lib/test_dio_request_cancellation.dart

Test API endpoint:

    [HttpGet("request-cancellation")]
    public async Task<IActionResult> TestRequestCancellation(CancellationToken cancellationToken)
    {
        try
        {
            _logger.LogInformation("Request started {RequestID} {Platform}", HttpContext.TraceIdentifier,
                Request.Headers["X-App-Platform"]);
            
            await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken);
            
            _logger.LogInformation("Request end {RequestID} {Platform}", HttpContext.TraceIdentifier,
                Request.Headers["X-App-Platform"]);
        }
        catch (TaskCanceledException)
        {
            _logger.LogInformation("Request canceled {RequestID} {Platform}", HttpContext.TraceIdentifier,
                Request.Headers["X-App-Platform"]);
            // Do nothing            
        }

        return Ok();
    }

Android

Before:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://0.0.0.0:5292
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\ws\me\repos\my-code-vault\MyToolkit\TestAspNetCoreWebApi
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB51JJSG7:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB51JJSG8:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB51JJSG9:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB51JJSGA:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB51JJSGB:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB51JJSGC:00000001 Android

After:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://0.0.0.0:5292
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\ws\me\repos\my-code-vault\MyToolkit\TestAspNetCoreWebApi
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMAV9NHQ97:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMAV9NHQ97:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMAV9NHQ98:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMAV9NHQ98:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMAV9NHQ99:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMAV9NHQ99:00000001 Android
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMAV9NHQ9A:00000001 Android

IOS

Before:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://0.0.0.0:5292
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\ws\me\repos\my-code-vault\MyToolkit\TestAspNetCoreWebApi
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB42PERN4:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB42PERN5:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB42PERN6:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB42PERN7:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB42PERN8:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB42PERN9:00000001 IOS

After:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://0.0.0.0:5292
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\ws\me\repos\my-code-vault\MyToolkit\TestAspNetCoreWebApi
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB35NC5L0:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMB35NC5L0:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB35NC5L1:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMB35NC5L1:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB35NC5L2:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMB35NC5L2:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB35NC5L3:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMB35NC5L3:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB35NC5L4:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request canceled 0HNEMB35NC5L4:00000001 IOS
info: TestAspNetCoreWebApi.Controllers.TestController[0]
      Request started 0HNEMB35NC5L5:00000001 IOS

@zzyzy zzyzy requested a review from a team as a code owner August 8, 2025 08:59
zzyzy and others added 3 commits August 8, 2025 17:45
Co-authored-by: Alex Li <github@alexv525.com>
Signed-off-by: Zhen Zhi Lee <zzyzy@users.noreply.github.com>
Co-authored-by: Alex Li <github@alexv525.com>
Signed-off-by: Zhen Zhi Lee <zzyzy@users.noreply.github.com>
Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

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

Thanks for adding tests

zzyzy and others added 4 commits August 8, 2025 23:55
…dart

Co-authored-by: Alex Li <github@alexv525.com>
Signed-off-by: Zhen Zhi Lee <zzyzy@users.noreply.github.com>
Co-authored-by: Alex Li <github@alexv525.com>
Signed-off-by: Zhen Zhi Lee <zzyzy@users.noreply.github.com>
@zzyzy zzyzy requested a review from AlexV525 August 8, 2025 16:06
Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

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

LGTM with only one nit picking

@@ -1,3 +1,4 @@
import 'package:async/async.dart';
Copy link
Member

Choose a reason for hiding this comment

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

Could we only show classes we are using here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@github-actions
Copy link
Contributor

Code Coverage Report: Only Changed Files listed

Package Base Coverage New Coverage Difference
Overall Coverage 🟢 85.57% 🟢 85.57% ⚪ 0%

Minimum allowed coverage is 0%, this run produced 85.57%

@AlexV525 AlexV525 merged commit f5a38ae into cfug:main Aug 11, 2025
3 checks passed
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