From 02df633a0184e634d01de04a9ab3765dec64db01 Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 03:56:14 -0700 Subject: [PATCH] app-engine: say ByeBye on SIGTERM/SIGINT/SIGHUP too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QA finding 3. contracts C2's last row promises the sockets send `ByeBye` however the process ends, and the signal path only released the keys and the pointer. `ControlChannel` is `Sendable` and each of its methods hops to its own serial queue, so the handler can hold the channel and queue the goodbye itself without waking the Engine actor — which the header comment on `EmergencyRelease` forbids, and rightly. The 150 ms wait afterwards is the same grace `Engine.stop()` gives the packet (`InitAndCleanup.cs:192-215`); it is best effort, and it runs after the keys are already back. Co-Authored-By: Claude Opus 5 (1M context) --- Sources/mwb/AppLifecycle.swift | 39 ++++++++++++++++++++++++++++++++++ Sources/mwb/Engine.swift | 4 ++++ 2 files changed, 43 insertions(+) diff --git a/Sources/mwb/AppLifecycle.swift b/Sources/mwb/AppLifecycle.swift index e3d9312..314439a 100644 --- a/Sources/mwb/AppLifecycle.swift +++ b/Sources/mwb/AppLifecycle.swift @@ -85,9 +85,15 @@ enum SingleInstance { final class EmergencyRelease: @unchecked Sendable { static let shared = EmergencyRelease() + /// How long the signal path gives a queued `ByeBye` to reach the wire — the + /// same wait `Engine.stop()` makes on the graceful path + /// (`InitAndCleanup.cs:192-215`). + static let goodbyeGrace: TimeInterval = 0.15 + private let lock = NSLock() private var injector: Injector? private var cursor: Cursor? + private var channel: ControlChannel? private var done = false /// Hands over the two objects the handlers may touch. Called once, at start. @@ -98,6 +104,35 @@ final class EmergencyRelease: @unchecked Sendable { lock.unlock() } + /// Hands over the channel, so the signal path can keep contracts C2's last + /// row ("sockets send `ByeBye`") without waking the Engine actor. + /// + /// `ControlChannel` is `Sendable` and every one of its methods hops to its + /// own serial queue, so this is a reference the handlers may hold and call + /// from any thread — unlike everything else the Engine owns. + func arm(channel: ControlChannel?) { + lock.lock() + self.channel = channel + lock.unlock() + } + + /// Best-effort `ByeBye` on the way out: queue it on every trusted + /// connection, then give it `goodbyeGrace` to be written. + /// + /// Best effort in the literal sense — the write happens on the channel's + /// queue, so a stuck queue loses the packet and the peer falls back to its + /// own socket-level detection (`SocketStuff.cs:1561-1563`). Blocking the + /// caller for 150 ms is only acceptable because the caller is about to + /// `exit(0)`; the keys and the pointer are already back by then. + func sayGoodbye() { + lock.lock() + let channel = self.channel + lock.unlock() + guard let channel else { return } + channel.sayGoodbye() + Thread.sleep(forTimeInterval: EmergencyRelease.goodbyeGrace) + } + /// Releases every held key and gives the pointer back. Idempotent. func run() { lock.lock() @@ -204,7 +239,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate { signal(number, SIG_IGN) let source = DispatchSource.makeSignalSource(signal: number, queue: .main) source.setEventHandler { + // Keys and pointer first (CLAUDE.md invariant 3), then contracts + // C2's last row: the peers are told we are gone. Neither call + // touches the Engine actor — the channel is its own lock. EmergencyRelease.shared.run() + EmergencyRelease.shared.sayGoodbye() exit(0) } source.resume() diff --git a/Sources/mwb/Engine.swift b/Sources/mwb/Engine.swift index ff622b3..42d3164 100644 --- a/Sources/mwb/Engine.swift +++ b/Sources/mwb/Engine.swift @@ -264,6 +264,7 @@ public actor Engine { try? await Task.sleep(for: .milliseconds(150)) channel?.stop() channel = nil + EmergencyRelease.shared.arm(channel: nil) tap?.stop() tap = nil health?.cancel() @@ -350,6 +351,9 @@ public actor Engine { let channel = ControlChannel( config: channelConfig(), sequencer: sequencer, replay: replay, sink: sink) self.channel = channel + // The signal/atexit path may say `ByeBye` without waking this actor + // (contracts C2, last row). + EmergencyRelease.shared.arm(channel: channel) channel.start() }