Forwards ignore bit to chained-to _Future.

If a target `_Future` is chained to another source `_Future`,
then it's because the target will complete with the same
result as the source future. Instead of keeping both
alive with a listener on the source which completes the target,
instead the target moves all its listeners to be directly on
the source, and keeps a link to the source in case more listeners
are added later.
The idea is that most futures are unreferenced after they have
had their first listener, so the target future has a chance
to be GC'ed.

If the target future has `.ignore()` called, and has no listener,
then the source completing with an error should not cause the
error to be uncaught. Without the optimization, the source would
have had a listener, and the target would ignore the error.

To simulate that, the source now gets a copy of the target's
`_ignoreUnhandledErrors` flag.

This is still not precisely the same as it would be without the
optimization. If *two* target futures are chained to the same source,
and only one of targes has `.ignore()` called, then this
implementation will make the uncaught error not be reported,
where it technically should.

(An alternative would be to *not* use chaining for futures
with `ignore()` called on them. But those are precisely futures
that are likely to be GC'able, because someone has already said
that they don't care if the future complete with errors.)

Fixes #54943

Bug: https://github.com/dart-lang/sdk/issues/54943
Change-Id: I0dbb4919ce2ea612d66539862fa0eb188aab8287
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352908
Reviewed-by: Slava Egorov <vegorov@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
Reviewed-by: Stephen Adams <sra@google.com>
This commit is contained in:
Lasse R.H. Nielsen 2024-02-21 13:05:56 +00:00 committed by Commit Queue
parent 8a6b48f596
commit e86b08e5bb
2 changed files with 18 additions and 2 deletions

View file

@ -356,7 +356,11 @@ class _Future<T> implements Future<T> {
}
void _ignore() {
_state |= _stateIgnoreError;
_Future<Object?> source = this;
while (source._isChained) {
source = source._chainSource;
}
source._state |= _stateIgnoreError;
}
Future<T> catchError(Function onError, {bool test(Object error)?}) {
@ -573,6 +577,7 @@ class _Future<T> implements Future<T> {
while (source._isChained) {
source = source._chainSource;
}
source._state |= target._state & _stateIgnoreError;
if (source._isComplete) {
_FutureListener? listeners = target._removeListeners();
target._cloneResult(source);

View file

@ -1138,7 +1138,16 @@ void testFutureOfFuture() async {
completer.complete(Future<int>.value(42));
}
main() {
void testIgnoreWhenCompleteError() {
// Regression test for https://github.com/dart-lang/sdk/issues/54943
asyncStart();
Future.error("Should be overridden by whenComplete error.").whenComplete(() {
return Future.error("From whenComplete. Should be ignored.");
}).ignore();
Future.delayed(Duration.zero, asyncEnd);
}
void main() {
asyncStart();
testValue();
@ -1212,6 +1221,8 @@ main() {
testFutureOfFuture();
testIgnoreWhenCompleteError();
asyncEnd();
}