Skip to content

Commit 5d92ea8

Browse files
committed
fix: store StreamSubscription refs so SIGINT/SIGTERM are not GC'd in server mode
Fix: In _waitForSignal(), ProcessSignal.sigint.watch().listen() and sigterm.watch().listen() returned StreamSubscriptions that were not stored. Dart GC collects unowned StreamSubscriptions, which automatically cancels them, causing Ctrl+C to silently stop working after an unpredictable interval. Store both subscriptions and cancel them explicitly after the completer fires so signal delivery is reliable for the entire lifetime of the headless server.
1 parent b0b0a3a commit 5d92ea8

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

lib/src/main.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ Future<void> main(List<String> args) async {
5656

5757
Future<void> _waitForSignal() async {
5858
final completer = Completer<void>();
59-
ProcessSignal.sigint.watch().listen((_) {
59+
final sigintSub = ProcessSignal.sigint.watch().listen((_) {
6060
if (!completer.isCompleted) completer.complete();
6161
});
62-
ProcessSignal.sigterm.watch().listen((_) {
62+
final sigtermSub = ProcessSignal.sigterm.watch().listen((_) {
6363
if (!completer.isCompleted) completer.complete();
6464
});
65-
return completer.future;
65+
await completer.future;
66+
await sigintSub.cancel();
67+
await sigtermSub.cancel();
6668
}

0 commit comments

Comments
 (0)