fix(acp): engine backpressure cannot defeat deadlines or teardown [museion/acp-backpressure-defeats-timeout-and-shutdown] #199
No files matched your search
@@ -157,6 +157,10 @@ final nonisolated class ACPEngineClient: @unchecked Sendable {
|
|||||||
/// Send a JSON-RPC request and await its matching response result.
|
/// Send a JSON-RPC request and await its matching response result.
|
||||||
/// A request that gets no answer within `timeout` fails — a hung engine
|
/// A request that gets no answer within `timeout` fails — a hung engine
|
||||||
/// must surface as an error (→ restart prompt), never an eternal await.
|
/// must surface as an error (→ restart prompt), never an eternal await.
|
||||||
|
/// brief-27: a timed-out request's bytes may still reach the engine once
|
||||||
|
/// the child unblocks (the deadline fails the caller, never dequeues the
|
||||||
|
/// write) — phantom engine work only: ids are never reused and the
|
||||||
|
/// response path drops unknown ids, so a late reply can never misroute.
|
||||||
/// (No default arguments: the only caller is EngineHost, which rides the
|
/// (No default arguments: the only caller is EngineHost, which rides the
|
||||||
/// seam — `EngineTransport.request`'s convenience owns the default shape.)
|
/// seam — `EngineTransport.request`'s convenience owns the default shape.)
|
||||||
func request(_ method: String, params: JSONValue?, timeout: TimeInterval) async throws -> JSONValue? {
|
func request(_ method: String, params: JSONValue?, timeout: TimeInterval) async throws -> JSONValue? {
|
||||||
@@ -261,6 +265,13 @@ final nonisolated class ACPEngineClient: @unchecked Sendable {
|
|||||||
/// brief-27: the write runs on the WRITER queue, never the state queue —
|
/// brief-27: the write runs on the WRITER queue, never the state queue —
|
||||||
/// a blocked write here starves no deadline and no teardown.
|
/// a blocked write here starves no deadline and no teardown.
|
||||||
private func send(_ data: Data) {
|
private func send(_ data: Data) {
|
||||||
|
// ponytail: queued sends are unbounded, and a send racing close() writes
|
||||||
|
// onto a closed handle (benign — write throws a catchable CocoaError 512
|
||||||
|
// that try? swallows; probe-verified by two independent reviewers).
|
||||||
|
// Bounded today by MentorSessionModel's sequential-send discipline (a
|
||||||
|
// request is awaited before the next; worst case one prompt + one
|
||||||
|
// cancel). Add a closed/depth guard here if a looping or
|
||||||
|
// non-MainActor caller ever appears.
|
||||||
writerQueue.async {
|
writerQueue.async {
|
||||||
var framed = Data(data)
|
var framed = Data(data)
|
||||||
framed.append(0x0A)
|
framed.append(0x0A)
|
||||||
|
|||||||
@@ -222,11 +222,14 @@ struct EngineSmokeTests {
|
|||||||
|
|
||||||
// MARK: - brief-27 backpressure smokes (blocked-writer regression rig)
|
// MARK: - brief-27 backpressure smokes (blocked-writer regression rig)
|
||||||
|
|
||||||
/// brief-27: bounded await for operations that HANG on the unfixed
|
/// brief-27: await an operation that may HANG on the unfixed transport
|
||||||
/// transport (the RED shape) — nil when the task hasn't finished inside
|
/// (the RED shape) and report what it answered — nil when the window leg
|
||||||
/// `window` seconds. One deadline per call (the house rule). The losing
|
/// won. One deadline per call (the house rule). The bound is `window`
|
||||||
/// leg is cancelled; the task itself keeps running and settles at the
|
/// only in the green world: withTaskGroup awaits stragglers at scope
|
||||||
/// test's teardown (SIGKILL of the rig child unblocks the writer).
|
/// exit and `await task.value` is cancellation-blind, so a HUNG task
|
||||||
|
/// holds this helper until IT settles — the real RED-world watchdog is
|
||||||
|
/// the fake's `exec /bin/sleep 30` exit (~30 s, inside the rail's 60 s
|
||||||
|
/// per-test timeout). Keep the fake's sleep short.
|
||||||
@MainActor
|
@MainActor
|
||||||
private func awaitBounded<T: Sendable>(_ task: Task<T, Never>, window: TimeInterval) async -> T? {
|
private func awaitBounded<T: Sendable>(_ task: Task<T, Never>, window: TimeInterval) async -> T? {
|
||||||
await withTaskGroup(of: T?.self) { group in
|
await withTaskGroup(of: T?.self) { group in
|
||||||
@@ -241,6 +244,22 @@ struct EngineSmokeTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// brief-27: run one request on a task and string-encode its outcome
|
||||||
|
/// ("returned" or the error) — the observable the pins match on.
|
||||||
|
@MainActor
|
||||||
|
private func requestOutcome(
|
||||||
|
_ client: ACPEngineClient, method: String, params: JSONValue?, timeout: TimeInterval,
|
||||||
|
) -> Task<String, Never> {
|
||||||
|
Task<String, Never> {
|
||||||
|
do {
|
||||||
|
_ = try await client.request(method, params: params, timeout: timeout)
|
||||||
|
return "returned"
|
||||||
|
} catch {
|
||||||
|
return String(describing: error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// brief-27: the blocked-writer rig — a real ACPEngineClient over the
|
/// brief-27: the blocked-writer rig — a real ACPEngineClient over the
|
||||||
/// zsh fake in `neverRead` mode, which execs /bin/sleep without ever
|
/// zsh fake in `neverRead` mode, which execs /bin/sleep without ever
|
||||||
/// reading stdin (the finding's inert non-reading child; no real
|
/// reading stdin (the finding's inert non-reading child; no real
|
||||||
@@ -273,14 +292,7 @@ struct EngineSmokeTests {
|
|||||||
kill(pid, SIGKILL) // settle the blocked pipe FIRST — close's boundedness is the next smoke's pin, not this one's
|
kill(pid, SIGKILL) // settle the blocked pipe FIRST — close's boundedness is the next smoke's pin, not this one's
|
||||||
client.close()
|
client.close()
|
||||||
}
|
}
|
||||||
let outcome = Task<String, Never> {
|
let outcome = requestOutcome(client, method: "session/prompt", params: blockedWriterPayload, timeout: 0.5)
|
||||||
do {
|
|
||||||
_ = try await client.request("session/prompt", params: blockedWriterPayload, timeout: 0.5)
|
|
||||||
return "returned"
|
|
||||||
} catch {
|
|
||||||
return String(describing: error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let result = await awaitBounded(outcome, window: 5)
|
let result = await awaitBounded(outcome, window: 5)
|
||||||
#expect(
|
#expect(
|
||||||
result?.contains("timed out") == true,
|
result?.contains("timed out") == true,
|
||||||
@@ -303,29 +315,17 @@ struct EngineSmokeTests {
|
|||||||
kill(pid, SIGKILL)
|
kill(pid, SIGKILL)
|
||||||
client.close()
|
client.close()
|
||||||
}
|
}
|
||||||
_ = Task<String, Never> {
|
// Wedge the writer: the oversized prompt fills the pipe (outcome unobserved).
|
||||||
do {
|
Task { _ = try? await client.request("session/prompt", params: blockedWriterPayload, timeout: 8) }
|
||||||
_ = try await client.request("session/prompt", params: blockedWriterPayload, timeout: 8)
|
|
||||||
} catch {
|
|
||||||
return String(describing: error)
|
|
||||||
}
|
|
||||||
return "returned"
|
|
||||||
}
|
|
||||||
// Let the oversized write reach the pipe and wedge the writer first.
|
// Let the oversized write reach the pipe and wedge the writer first.
|
||||||
try await Task.sleep(nanoseconds: 200_000_000)
|
try await Task.sleep(nanoseconds: 200_000_000)
|
||||||
client.notify("session/cancel", params: .object(["sessionId": .string("sess-1")]))
|
client.notify("session/cancel", params: .object(["sessionId": .string("sess-1")]))
|
||||||
let second = Task<String, Never> {
|
let second = requestOutcome(
|
||||||
do {
|
client,
|
||||||
_ = try await client.request(
|
method: "session/new",
|
||||||
"session/new",
|
params: .object(["cwd": .string("/tmp"), "mcpServers": .array([])]),
|
||||||
params: .object(["cwd": .string("/tmp"), "mcpServers": .array([])]),
|
timeout: 0.5,
|
||||||
timeout: 0.5,
|
)
|
||||||
)
|
|
||||||
return "returned"
|
|
||||||
} catch {
|
|
||||||
return String(describing: error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let result = await awaitBounded(second, window: 5)
|
let result = await awaitBounded(second, window: 5)
|
||||||
#expect(
|
#expect(
|
||||||
result?.contains("timed out") == true,
|
result?.contains("timed out") == true,
|
||||||
@@ -345,14 +345,8 @@ struct EngineSmokeTests {
|
|||||||
kill(pid, SIGKILL)
|
kill(pid, SIGKILL)
|
||||||
client.close()
|
client.close()
|
||||||
}
|
}
|
||||||
_ = Task<String, Never> {
|
// Wedge the writer: the oversized prompt fills the pipe (outcome unobserved).
|
||||||
do {
|
Task { _ = try? await client.request("session/prompt", params: blockedWriterPayload, timeout: 8) }
|
||||||
_ = try await client.request("session/prompt", params: blockedWriterPayload, timeout: 8)
|
|
||||||
} catch {
|
|
||||||
return String(describing: error)
|
|
||||||
}
|
|
||||||
return "returned"
|
|
||||||
}
|
|
||||||
// The write is in the pipe and the writer wedged before close runs.
|
// The write is in the pipe and the writer wedged before close runs.
|
||||||
try await Task.sleep(nanoseconds: 200_000_000)
|
try await Task.sleep(nanoseconds: 200_000_000)
|
||||||
let closeTask = Task<Void, Never> { client.close() }
|
let closeTask = Task<Void, Never> { client.close() }
|
||||||
|
|||||||
Reference in new issue
Block a user