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() }