Skip to content

fix(cronet_http): UrlRequest leak - #2129

Open
brianquinlan wants to merge 5 commits into
dart-lang:masterfrom
brianquinlan:cronet_mem_fix
Open

brianquinlan wants to merge 5 commits into
dart-lang:masterfrom
brianquinlan:cronet_mem_fix

Conversation

@brianquinlan

@brianquinlan brianquinlan commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2124
Fixes #1919

There were two issues before:

  1. The way abort triggers were handled:
final cronetRequest = builder.build()!;
if (request case Abortable(:final abortTrigger?)) {
  unawaited(abortTrigger.whenComplete(cronetRequest.cancel));
}

This would keep cronetRequest alive beyond the lifetime of the send method if the abort was never triggered.

  1. onCancel in onResponseStarted closed over urlRequest:
      onResponseStarted: (urlRequest, responseInfo) {
        using((arena) {
          // `urlRequest` is captured by the `onCancel` callback of the
          // `StreamController` below. So it must not be registered to be later
          // released here.
          responseInfo?.releasedBy(arena);
          responseStream = StreamController(
            onCancel: () {
              // The user did `response.stream.cancel()`. We can just pretend
              // that the response completed normally.
              if (responseStreamCancelled) return;
              responseStreamCancelled = true;
              urlRequest!

This mean that it could not be cleaned-up by the arena.

I fixed this by creating a class (_ActiveUrlRequest) that owns the UrlRequest and releases it as soon as any finishing code path indicates that we are done with it, i.e. request succeeded, request failed, request cancelled, request aborted. So now we only close over _ActiveUrlRequest which, if the request has already finished, will just contain a null reference to a UrlRequest.

I added a conformance test for this sort of leak. They fail for package:cronet_http without this fix but they aren't very convincing for desktop implementations, which can allocate 10s of GiB of memory without any issue. I expect to improve those tests in the future. They are also slow - maybe I'll make an independent workflow for them.


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Note: The Dart team is trialing Gemini Code Assist. Don't take its comments as final Dart team feedback. Use the suggestions if they're helpful; otherwise, wait for a human reviewer.

@brianquinlan
brianquinlan marked this pull request as draft November 20, 2026 05:24

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request fixes memory and JNI global reference leaks across request lifecycles and abortable uploads in cronet_http. It introduces the _ActiveUrlRequest class to manage the lifecycle of jb.UrlRequest global references, preventing leaks and double-releases. Additionally, it adds validation to throw a ClientException when a response Content-Length header overflows a 64-bit integer. Comprehensive integration tests have been added to verify these memory leak fixes and header overflow handling. There are no review comments, so I have no feedback to provide.

@github-actions github-actions Bot added package:cupertino_http Issues related to package:cupertino_http package:http labels Nov 20, 2026
@brianquinlan brianquinlan changed the title Cronet mem fix fix(cronet_http): UrlRequest leak Nov 20, 2026
@brianquinlan brianquinlan added the run-ios-tests Trigger iOS tests label Nov 20, 2026
@brianquinlan
brianquinlan marked this pull request as ready for review November 20, 2026 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants