Skip to content

[cronet_http/cupertino_http]: Fixes bugs where cancelling StreamedResponse.stream did not sever the connection #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 1, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update cronet_client.dart
  • Loading branch information
brianquinlan committed Apr 30, 2025
commit dd31d608101b5d3ff9e25eb5def7e1610cfc4efd
14 changes: 8 additions & 6 deletions pkgs/cronet_http/lib/src/cronet_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
StreamController<List<int>>? responseStream;
JByteBuffer? jByteBuffer;
var numRedirects = 0;
var cancelled = false;
var done = false;

// The order of callbacks generated by Cronet is documented here:
// https://developer.android.com/guide/topics/connectivity/cronet/lifecycle
Expand All @@ -164,7 +164,7 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
responseStream = StreamController(onCancel: () {
// The user did `response.stream.cancel()`. We can just pretend that
// the response completed normally.
cancelled = true;
done = true;
urlRequest.cancel();
responseStream!.sink.close();
jByteBuffer?.release();
Expand Down Expand Up @@ -213,7 +213,7 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
urlRequest.read(jByteBuffer!);
},
onRedirectReceived: (urlRequest, responseInfo, newLocationUrl) {
if (cancelled) return;
if (done) return;
final responseHeaders =
_cronetToClientHeaders(responseInfo.getAllHeaders());

Expand Down Expand Up @@ -258,7 +258,7 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
}
},
onReadCompleted: (urlRequest, responseInfo, byteBuffer) {
if (cancelled) return;
if (done) return;
byteBuffer.flip();
final data = jByteBuffer!.asUint8List().sublist(0, byteBuffer.remaining);
responseStream!.add(data);
Expand All @@ -268,13 +268,15 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
urlRequest.read(byteBuffer);
},
onSucceeded: (urlRequest, responseInfo) {
if (cancelled) return;
if (done) return;
done = true;
responseStream!.sink.close();
jByteBuffer?.release();
profile?.responseData.close();
},
onFailed: (urlRequest, responseInfo, cronetException) {
if (cancelled) return;
if (done) return;
done = true;
final error = ClientException(
'Cronet exception: ${cronetException.toString()}', request.url);
if (responseStream == null) {
Expand Down