Make nullFuture be per-zone.
We introduced a `nullFuture` during the null-safety migration where
we changed some methods to no longer allow returning `null`,
and they therefore had to return a `Future`.
That affected timing, because returning `null` was processed
synchronously, and that change in timing made some tests fail.
Rather that fix the fragile tests, we made the function return
a recognizable future, a canonical `Future<Null>.value(null)`,
and then recognized it and took a synchronous path for it.
That caused other issues, because the future was created in the
root zone. (Well, originally, it was created in the first zone
which needed one, that was worse. Now it's created in the root zone.)
Some code tries to contain asynchrony inside a custom zone, and
then the get a `nullFuture` and calls `then` on it, and that
schedules a microtask in the root zone.
(It should probably have used the listener's zone, and not store
a zone in the future at all, but that's how it was first done,
and now people rely on that behavior too.)
This change creates a `null` future *per zone* (lazily initialized
when asked for). That should be sufficient because the code recognizing
a returned `null` future is generally running in the same zone,
but if any other code gets the `nullFuture`, it will be in the
expected zone for where it was requested.
This is a reland of commit a247b158d6
Change-Id: Ia113756de1f6d50af4b1abfec219d6b4dcd5d59b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249488
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
This commit is contained in:
committed by
Commit Bot
parent
391540c889
commit
4d750a862d
@@ -248,13 +248,13 @@ class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> {
|
||||
}
|
||||
|
||||
Future cancel() {
|
||||
if (_canceled) return nullFuture;
|
||||
|
||||
_unlisten();
|
||||
// Clear out the target to indicate this is complete.
|
||||
_target = null;
|
||||
_onData = null;
|
||||
return nullFuture;
|
||||
if (!_canceled) {
|
||||
_unlisten();
|
||||
// Clear out the target to indicate this is complete.
|
||||
_target = null;
|
||||
_onData = null;
|
||||
}
|
||||
return Future<void>.value(null);
|
||||
}
|
||||
|
||||
bool get _canceled => _target == null;
|
||||
|
||||
Reference in New Issue
Block a user