dart-lang / dart-lang/language

CancelableOperation and CancelableCompleter should cancel/kill delayed Futures

Open
#1,629 1 comment 2 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

When starting a Future which contains an `await` somewhere in the code, be it a delay or just a network request waiting for a response, it is impossible to stop that Future, not even with `CancelableOperation` or `CancelableCompleter`.

If the Future is running in the main thread without any `await`, it can't be stopped anyway, because the cancel operation is only invoked when the Future is done.

```dart
import 'dart:async';
import 'package:async/async.dart';

void main() {
Future foo() async {
print('running...');
await Future.delayed(Duration(seconds: 5));
print('done');
}

final CancelableCompleter completer = CancelableCompleter(onCancel: () => print('onCancel'));
completer.complete(foo());
completer.operation.cancel().then((dynamic _) => print('cancelled'));
}
```

Output:
```
running...
onCancel
cancelled
done // appears after 5 seconds
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the Dart example and the stated behavior of CancelableOperation and CancelableCompleter around Future.delayed and awaited network requests. The issue does not name files, tests, or an entry point, and completion criteria for changing Future cancellation semantics are not specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.