Skip to content

Ignore received data after the response stream has been closed #1766

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 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,7 @@ void main() {
expect(profile.requestData.contentLength, isNull);
expect(profile.requestData.startTime, isNotNull);
expect(profile.requestData.endTime, isNotNull);
// Extra data could be received before the cancel event is dispatched
// by the url loading framework so check that
// `profile.responseData.bodyBytes` starts with `receivedData`.
expect(profile.responseData.bodyBytes.sublist(0, receivedData.length),
receivedData);
expect(profile.responseData.bodyBytes, receivedData);
});
});

Expand Down
8 changes: 8 additions & 0 deletions pkgs/cupertino_http/lib/src/cupertino_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class _TaskTracker {
final responseCompleter = Completer<URLResponse>();
final BaseRequest request;
final StreamController<Uint8List> responseController;

/// Whether the response stream subscription has been cancelled.
bool responseListenerCancelled = false;
final HttpClientRequestProfile? profile;
int numRedirects = 0;
Uri? lastUrl; // The last URL redirected to.
Expand Down Expand Up @@ -211,6 +214,7 @@ class CupertinoClient extends BaseClient {
taskTracker.responseCompleter.completeError(exception);
}
} else {
assert(error == null || taskTracker.responseListenerCancelled);
assert(taskTracker.profile == null ||
taskTracker.profile!.requestData.endTime != null);

Expand All @@ -226,6 +230,7 @@ class CupertinoClient extends BaseClient {

static void _onData(URLSession session, URLSessionTask task, NSData data) {
final taskTracker = _tracker(task);
if (taskTracker.responseListenerCancelled) return;
taskTracker.responseController.add(data.toList());
taskTracker.profile?.responseData.bodySink.add(data.toList());
}
Expand Down Expand Up @@ -366,6 +371,9 @@ class CupertinoClient extends BaseClient {
request.headers.forEach(urlRequest.setValueForHttpHeaderField);
final task = urlSession.dataTaskWithRequest(urlRequest);
final subscription = StreamController<Uint8List>(onCancel: () {
final taskTracker = _tasks[task];
if (taskTracker == null) return;

Choose a reason for hiding this comment

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

This will skip task.cancel() if there's no tracker. Is that intended? If not, I think you can simplify this to _tasks[task]?.responseListenerCancelled = true;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The tracker is removed when the request is completed, in which case cancelling the response stream is a no-op (because no more data will arrive).

taskTracker.responseListenerCancelled = true;
task.cancel();
});
final taskTracker = _TaskTracker(request, subscription, profile);
Expand Down
Loading