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,7 +128,10 @@ class LocalSignals implements Signals { ...@@ -128,7 +128,10 @@ 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];
if (handlers != null) {
final List<SignalHandler> handlersCopy = handlers.toList();
for (final SignalHandler handler in handlersCopy) {
try { try {
await asyncGuard<void>(() async => handler(s)); await asyncGuard<void>(() async => handler(s));
} on Exception catch (e) { } on Exception catch (e) {
...@@ -137,6 +140,7 @@ class LocalSignals implements Signals { ...@@ -137,6 +140,7 @@ class LocalSignals implements Signals {
} }
} }
} }
}
// If this was a signal that should cause the process to go down, then // If this was a signal that should cause the process to go down, then
// call exit(); // call exit();
if (_shouldExitFor(s)) { if (_shouldExitFor(s)) {
......
...@@ -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