Unverified Commit 9cc61b0f authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] handle concurrent modification in signal callback (#78913)

parent d165ea5c
......@@ -128,12 +128,16 @@ class LocalSignals implements Signals {
}
Future<void> _handleSignal(ProcessSignal s) async {
for (final SignalHandler handler in _handlersList[s] ?? <SignalHandler>[]) {
try {
await asyncGuard<void>(() async => handler(s));
} on Exception catch (e) {
if (_errorStreamController.hasListener) {
_errorStreamController.add(e);
final List<SignalHandler>? handlers = _handlersList[s];
if (handlers != null) {
final List<SignalHandler> handlersCopy = handlers.toList();
for (final SignalHandler handler in handlersCopy) {
try {
await asyncGuard<void>(() async => handler(s));
} on Exception catch (e) {
if (_errorStreamController.hasListener) {
_errorStreamController.add(e);
}
}
}
}
......
......@@ -56,6 +56,21 @@ void main() {
await completer.future;
});
testWithoutContext('signal handlers do not cause concurrent modification errors when removing handlers in a signal callback', () async {
final Completer<void> completer = Completer<void>();
Object token;
Future<void> handle(ProcessSignal s) async {
expect(s, signalUnderTest);
expect(await signals.removeHandler(signalUnderTest, token), true);
completer.complete();
}
token = signals.addHandler(signalUnderTest, handle);
fakeSignal.controller.add(fakeSignal);
await completer.future;
});
testWithoutContext('signal handler error goes on error stream', () async {
final Exception exn = Exception('Error');
signals.addHandler(signalUnderTest, (ProcessSignal s) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment