fix(acp): engine backpressure cannot defeat deadlines or teardown
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
A hung engine that stops reading stdin fills the 64 KB pipe, and the client's synchronous send blocked its one serial queue — the same queue that carries request deadlines, request registration, and close()'s bookkeeping barrier. A blocked writer therefore defeated the request timeout, starved Stop/lifecycle calls, and made close() queue.sync behind the stuck write before SIGTERM was ever reachable; EngineHost's MainActor close callers turned that into an app-wide unbounded hang on restart or quit (argus probe: 2M-char request with 0.1 s timeout never timed out; a watchdog close never returned). Separate the writer from the state: stdin writes (and the stdin handle's own close, which a blocked write may still own) now ride a dedicated serial writerQueue, so the state queue is I/O-free — deadlines fire, Stop is accepted, follow-up requests register, and close() runs bookkeeping, SIGTERM -> grace -> SIGKILL -> reap without ever queueing behind the writer. Wire order is preserved (all send sites funnel through the one serial writer queue) and the healthy-engine contract is untouched. Focused serialized EngineSmokeTests: 6 executed / 6 passed at the fix (3 of them RED at base on exactly the three mechanisms).
This commit is contained in:
1 parent
33b7fe0f9f
commit
89cd377814
1 file changed
+34
-8
@@ -75,6 +75,9 @@ nonisolated extension JSONValue {
|
||||
/// matches responses to pending requests, and delivers notifications in wire
|
||||
/// order. Requests are expected to be strictly sequential (initialize →
|
||||
/// session/new → prompt), matching the pane's usage.
|
||||
/// brief-27: stdin writes ride a dedicated writer queue, so engine
|
||||
/// backpressure (a full pipe) can stall a send but never a deadline,
|
||||
/// Stop delivery, or teardown.
|
||||
///
|
||||
/// Wire facts baked in (verified against adapter 0.20.6 + the acp SDK):
|
||||
/// - `session/cancel` is a JSON-RPC *notification* (interfaces.py — CancelNotification).
|
||||
@@ -94,6 +97,12 @@ final nonisolated class ACPEngineClient: @unchecked Sendable {
|
||||
private let stderrPipe = Pipe()
|
||||
/// All mutable state below is confined to this serial queue.
|
||||
private let queue = DispatchQueue(label: "com.babylondreams.Museion.acp-client")
|
||||
/// brief-27: stdin writes (and the stdin handle's own close) ride this
|
||||
/// dedicated writer queue. The state queue NEVER touches the pipe — a
|
||||
/// full pipe (hung engine not reading stdin) blocks only this writer, so
|
||||
/// request deadlines, Stop delivery, and close()'s bookkeeping stay live
|
||||
/// behind it. Serial = the wire order every send site already had.
|
||||
private let writerQueue = DispatchQueue(label: "com.babylondreams.Museion.acp-client-writer")
|
||||
private var pending: [Int: CheckedContinuation<JSONValue?, any Error>] = [:]
|
||||
private var buffer = Data()
|
||||
private var currentID = 0
|
||||
@@ -174,7 +183,9 @@ final nonisolated class ACPEngineClient: @unchecked Sendable {
|
||||
continuation.resume(throwing: TransportError(message: "engine closed before \(method)"))
|
||||
return
|
||||
}
|
||||
queue.async { self.send(raw) }
|
||||
// brief-27: straight to the writer queue — never through the state
|
||||
// queue, so a blocked write can't pin the deadline armed below.
|
||||
send(raw)
|
||||
// ponytail: deadline on the same serial queue — fires only if the
|
||||
// response hasn't already resumed and removed the continuation.
|
||||
queue.asyncAfter(deadline: .now() + timeout) {
|
||||
@@ -194,11 +205,15 @@ final nonisolated class ACPEngineClient: @unchecked Sendable {
|
||||
"params": params?.any ?? [String: Any](),
|
||||
]
|
||||
guard let raw = try? JSONSerialization.data(withJSONObject: payload) else { return }
|
||||
queue.async { self.send(raw) }
|
||||
// brief-27: writer queue, not the state queue (see request).
|
||||
send(raw)
|
||||
}
|
||||
|
||||
/// Graceful teardown: SIGTERM → 1 s grace → SIGKILL → reap, so the child
|
||||
/// never outlives the app (same pattern as EngineProbe.terminateAndReap).
|
||||
/// Graceful teardown: SIGTERM → 1 s grace → SIGKILL → reap, so the
|
||||
/// child never outlives the app (same pattern as EngineProbe.terminateAndReap).
|
||||
/// brief-27: bounded even when a stdin write is blocked on a full pipe —
|
||||
/// bookkeeping syncs on the (I/O-free) state queue, and child
|
||||
/// termination never queues behind the writer.
|
||||
func close() {
|
||||
let alreadyClosed = queue.sync {
|
||||
if closed {
|
||||
@@ -227,7 +242,14 @@ final nonisolated class ACPEngineClient: @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
process.waitUntilExit()
|
||||
try? stdinPipe.fileHandleForWriting.close()
|
||||
// brief-27: the stdin WRITE handle closes on the writer queue — a
|
||||
// blocked write may still own it (it unblocks only once the child's
|
||||
// read end died, which the kill above just arranged); closing under
|
||||
// a live write would race Foundation. The reader handles have no
|
||||
// writers — their handlers are already off, so they close here.
|
||||
writerQueue.async {
|
||||
try? self.stdinPipe.fileHandleForWriting.close()
|
||||
}
|
||||
try? stdoutPipe.fileHandleForReading.close()
|
||||
try? stderrPipe.fileHandleForReading.close()
|
||||
}
|
||||
@@ -236,10 +258,14 @@ final nonisolated class ACPEngineClient: @unchecked Sendable {
|
||||
|
||||
/// Frame and write one NDJSON line. A failed write (child gone) is dropped:
|
||||
/// the crash path owns the UX, not the send.
|
||||
/// brief-27: the write runs on the WRITER queue, never the state queue —
|
||||
/// a blocked write here starves no deadline and no teardown.
|
||||
private func send(_ data: Data) {
|
||||
var framed = Data(data)
|
||||
framed.append(0x0A)
|
||||
try? stdinPipe.fileHandleForWriting.write(contentsOf: framed)
|
||||
writerQueue.async {
|
||||
var framed = Data(data)
|
||||
framed.append(0x0A)
|
||||
try? self.stdinPipe.fileHandleForWriting.write(contentsOf: framed)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fail every pending request and surface the crash — the one exit path
|
||||
|
||||
Reference in new issue
Block a user