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 { ...@@ -128,12 +128,16 @@ class LocalSignals implements Signals {
} }
Future<void> _handleSignal(ProcessSignal s) async { Future<void> _handleSignal(ProcessSignal s) async {
for (final SignalHandler handler in _handlersList[s] ?? <SignalHandler>[]) { final List<SignalHandler>? handlers = _handlersList[s];
try { if (handlers != null) {
await asyncGuard<void>(() async => handler(s)); final List<SignalHandler> handlersCopy = handlers.toList();
} on Exception catch (e) { for (final SignalHandler handler in handlersCopy) {
if (_errorStreamController.hasListener) { try {
_errorStreamController.add(e); await asyncGuard<void>(() async => handler(s));
} on Exception catch (e) {
if (_errorStreamController.hasListener) {
_errorStreamController.add(e);
}
} }
} }
} }
......
...@@ -56,6 +56,21 @@ void main() { ...@@ -56,6 +56,21 @@ void main() {
await completer.future; 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 { testWithoutContext('signal handler error goes on error stream', () async {
final Exception exn = Exception('Error'); final Exception exn = Exception('Error');
signals.addHandler(signalUnderTest, (ProcessSignal s) { 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