Skip to content

Commit 39f7f65

Browse files
committed
fix(apple): reuse runner HTTP connections
1 parent f7879cf commit 39f7f65

6 files changed

Lines changed: 386 additions & 77 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerHttpRequestFraming.swift‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum RunnerHttpRequestRejection: Equatable {
5151
struct RunnerHttpAdmittedHead: Equatable {
5252
let bodyOffset: Int
5353
let contentLength: Int
54+
let keepsConnectionAlive: Bool
5455
}
5556

5657
enum RunnerHttpHeadVerdict: Equatable {
@@ -121,10 +122,16 @@ enum RunnerHttpRequestFraming {
121122
else {
122123
return .rejected(.bodyTooLarge)
123124
}
125+
let connectionOptions = (headers["connection"] ?? []).flatMap {
126+
$0.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces).lowercased() }
127+
}
128+
let keepsConnectionAlive = !connectionOptions.contains("close")
129+
&& (requestLine[2] == "HTTP/1.1" || connectionOptions.contains("keep-alive"))
124130
return .admitted(
125131
RunnerHttpAdmittedHead(
126132
bodyOffset: terminator.upperBound - buffer.startIndex,
127-
contentLength: contentLength
133+
contentLength: contentLength,
134+
keepsConnectionAlive: keepsConnectionAlive
128135
)
129136
)
130137
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Transport.swift‎

Lines changed: 101 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ extension RunnerTests {
88
/// loopback peer may idle, so its clock starts at its first byte.
99
static let requestTimeout: TimeInterval = 30
1010

11-
/// Starts one connection and reads one request from it once its path is known. Every refusal
12-
/// answers and closes that connection only; the listener keeps serving.
11+
/// An unused loopback connection watches listener lifetime. Used connections have bounded idle time.
12+
static let connectionIdleTimeout: TimeInterval = 30
13+
14+
/// Every request is authenticated, including subsequent requests on an admitted connection.
1315
func handle(connection: NWConnection) {
1416
guard let token = listenerToken else {
1517
connection.cancel()
1618
return
1719
}
20+
transportConnections[ObjectIdentifier(connection)] = connection
1821
connection.stateUpdateHandler = { [weak self] state in
1922
switch state {
2023
case .ready:
21-
connection.stateUpdateHandler = nil
2224
self?.admit(connection, token: token)
2325
case .failed:
2426
connection.cancel()
27+
case .cancelled:
28+
connection.stateUpdateHandler = nil
29+
self?.transportConnections.removeValue(forKey: ObjectIdentifier(connection))
2530
default:
2631
break
2732
}
@@ -60,6 +65,8 @@ extension RunnerTests {
6065
}
6166

6267
private func consume(_ data: Data, for inbound: RunnerInboundRequest, over connection: NWConnection) {
68+
inbound.idleDeadline?.cancel()
69+
inbound.idleDeadline = nil
6370
if inbound.deadline == nil {
6471
armDeadline(for: inbound, over: connection)
6572
}
@@ -89,8 +96,9 @@ extension RunnerTests {
8996
}
9097
inbound.close()
9198
let body = inbound.buffer.subdata(in: head.bodyOffset..<bodyEnd)
92-
handleRequestBody(body) { [weak self] result in
93-
self?.sendResult(result, over: connection)
99+
let remainder = Data(inbound.buffer.suffix(from: bodyEnd))
100+
handleRequestBody(body) { [weak self, token = inbound.token] result in
101+
self?.sendResult(result, over: connection, request: head, token: token, remainder: remainder)
94102
}
95103
}
96104

@@ -114,37 +122,64 @@ extension RunnerTests {
114122
status: rejection.status,
115123
response: errorResponse(code: rejection.code, message: rejection.message)
116124
)
117-
sendResponse(response, over: connection)
125+
sendResponse(response, over: connection, closeConnection: true)
118126
}
119127

120128
private func sendResult(
121-
_ result: (data: Data, shouldFinish: Bool),
122-
over connection: NWConnection
129+
_ result: (response: RunnerHttpResponse, shouldFinish: Bool),
130+
over connection: NWConnection,
131+
request: RunnerHttpAdmittedHead,
132+
token: RunnerListenerToken,
133+
remainder: Data
123134
) {
124-
sendResponse(result.data, over: connection) { [weak self] in
135+
let closeConnection = !request.keepsConnectionAlive || result.shouldFinish
136+
sendResponse(result.response, over: connection, closeConnection: closeConnection) { [weak self] sent in
137+
guard let self else { return }
125138
if result.shouldFinish {
126-
self?.finish()
139+
self.finish()
140+
} else if sent && !closeConnection {
141+
self.readNextRequest(over: connection, token: token, buffered: remainder)
127142
}
128143
}
129144
}
130145

146+
private func readNextRequest(over connection: NWConnection, token: RunnerListenerToken, buffered: Data) {
147+
guard transportConnections[ObjectIdentifier(connection)] != nil else { return }
148+
let inbound = RunnerInboundRequest(token: token)
149+
if !buffered.isEmpty {
150+
consume(buffered, for: inbound, over: connection)
151+
return
152+
}
153+
// Idle expiry is silent; a partial request gets its own full arrival deadline and a 408.
154+
let idleDeadline = DispatchWorkItem {
155+
guard !inbound.closed else { return }
156+
inbound.close()
157+
connection.cancel()
158+
}
159+
inbound.idleDeadline = idleDeadline
160+
transportQueue.asyncAfter(deadline: .now() + Self.connectionIdleTimeout, execute: idleDeadline)
161+
receiveRequest(inbound, over: connection)
162+
}
163+
131164
private func sendResponse(
132-
_ response: Data,
165+
_ response: RunnerHttpResponse,
133166
over connection: NWConnection,
134-
afterSend: @escaping @Sendable () -> Void = {}
167+
closeConnection: Bool,
168+
afterSend: @escaping @Sendable (Bool) -> Void = { _ in }
135169
) {
136-
connection.send(content: response, isComplete: true, completion: .contentProcessed { error in
170+
// Network delivers send completions on the connection's transport queue.
171+
connection.send(content: response.encoded(closeConnection: closeConnection), completion: .contentProcessed { error in
137172
if let error {
138173
NSLog("AGENT_DEVICE_RUNNER_SEND_FAILED=%@", String(describing: error))
139174
}
140-
connection.cancel()
141-
afterSend()
175+
if closeConnection || error != nil { connection.cancel() }
176+
afterSend(error == nil)
142177
})
143178
}
144179

145180
private func handleRequestBody(
146181
_ body: Data,
147-
completion: @escaping @Sendable ((data: Data, shouldFinish: Bool)) -> Void
182+
completion: @escaping @Sendable ((response: RunnerHttpResponse, shouldFinish: Bool)) -> Void
148183
) {
149184
guard String(data: body, encoding: .utf8) != nil else {
150185
completion((
@@ -257,7 +292,7 @@ extension RunnerTests {
257292
/// false so the caller enqueues the (single) execution.
258293
func attachToInFlightCommandIfNeeded(
259294
command: Command,
260-
completion: @escaping @Sendable ((data: Data, shouldFinish: Bool)) -> Void
295+
completion: @escaping @Sendable ((response: RunnerHttpResponse, shouldFinish: Bool)) -> Void
261296
) -> Bool {
262297
guard let commandId = command.commandId?.trimmedNonEmpty else { return false }
263298
inFlightCommandLock.lock()
@@ -278,10 +313,10 @@ extension RunnerTests {
278313

279314
func deliverCommandResult(
280315
command: Command,
281-
result: (data: Data, shouldFinish: Bool),
282-
completion: ((data: Data, shouldFinish: Bool)) -> Void
316+
result: (response: RunnerHttpResponse, shouldFinish: Bool),
317+
completion: ((response: RunnerHttpResponse, shouldFinish: Bool)) -> Void
283318
) {
284-
var waiters: [@Sendable ((data: Data, shouldFinish: Bool)) -> Void] = []
319+
var waiters: [@Sendable ((response: RunnerHttpResponse, shouldFinish: Bool)) -> Void] = []
285320
if let commandId = command.commandId?.trimmedNonEmpty {
286321
inFlightCommandLock.lock()
287322
inFlightCommandIds.remove(commandId)
@@ -296,7 +331,7 @@ extension RunnerTests {
296331

297332
// MARK: - Response Encoding
298333

299-
private func jsonResponse(status: Int, response: Response) -> Data {
334+
private func jsonResponse(status: Int, response: Response) -> RunnerHttpResponse {
300335
// Stamp the gesture-clock uptime at the END of command handling, just before the HTTP
301336
// write, so the warm snapshot and recordStart responses carry the anchor for free. This
302337
// runs AFTER commandJournal.finish, so journal-stored lifecycleResponseJson stays
@@ -310,7 +345,7 @@ extension RunnerTests {
310345
: response
311346
let encoder = JSONEncoder()
312347
let body = (try? encoder.encode(stamped)).flatMap { String(data: $0, encoding: .utf8) } ?? "{}"
313-
return httpResponse(status: status, body: body)
348+
return RunnerHttpResponse(status: status, body: body)
314349
}
315350

316351
private func errorResponse(code: String, message: String, hint: String? = nil) -> Response {
@@ -343,53 +378,32 @@ extension RunnerTests {
343378
)
344379
}
345380

346-
private func httpResponse(status: Int, body: String) -> Data {
347-
let headers = [
348-
"HTTP/1.1 \(status) \(Self.reasonPhrase(status))",
349-
"Content-Type: application/json",
350-
"Content-Length: \(body.utf8.count)",
351-
"\(Self.authEnforcedResponseHeader): required",
352-
"Connection: close",
353-
"",
354-
body
355-
].joined(separator: "\r\n")
356-
return Data(headers.utf8)
357-
}
358-
359381
/// Present on every response so the daemon can refuse a runner build that predates the token.
360382
static let authEnforcedResponseHeader = "X-Agent-Device-Runner-Auth"
361383

362-
private static func reasonPhrase(_ status: Int) -> String {
363-
switch status {
364-
case 200: return "OK"
365-
case 400: return "Bad Request"
366-
case 401: return "Unauthorized"
367-
case 404: return "Not Found"
368-
case 405: return "Method Not Allowed"
369-
case 408: return "Request Timeout"
370-
case 411: return "Length Required"
371-
case 413: return "Content Too Large"
372-
case 431: return "Request Header Fields Too Large"
373-
default: return "Internal Server Error"
374-
}
384+
func closeTransport() {
385+
listener?.cancel()
386+
listener = nil
387+
for connection in transportConnections.values { connection.cancel() }
388+
transportConnections.removeAll()
375389
}
376390

377391
private func finish() {
378-
listener?.cancel()
379-
listener = nil
392+
closeTransport()
380393
// Guard against double-fulfill: coalesced shutdown sends deliver one result to
381394
// multiple waiters, each of which may ask to finish.
382395
doneExpectation?.fulfill()
383396
doneExpectation = nil
384397
}
385398
}
386399

387-
/// One connection's request, read on the transport queue.
400+
/// One request on a connection, read on the transport queue.
388401
final class RunnerInboundRequest {
389402
let token: RunnerListenerToken
390403
var buffer = Data()
391404
var head: RunnerHttpAdmittedHead?
392405
var deadline: DispatchWorkItem?
406+
var idleDeadline: DispatchWorkItem?
393407
private(set) var closed = false
394408

395409
init(token: RunnerListenerToken) {
@@ -402,5 +416,41 @@ final class RunnerInboundRequest {
402416
closed = true
403417
deadline?.cancel()
404418
deadline = nil
419+
idleDeadline?.cancel()
420+
idleDeadline = nil
421+
}
422+
}
423+
424+
/// Coalesced commands share their response, but each connection owns its persistence policy.
425+
struct RunnerHttpResponse: Equatable, Sendable {
426+
let status: Int
427+
let body: String
428+
429+
func encoded(closeConnection: Bool) -> Data {
430+
let headers = [
431+
"HTTP/1.1 \(status) \(Self.reasonPhrase(status))",
432+
"Content-Type: application/json",
433+
"Content-Length: \(body.utf8.count)",
434+
"\(RunnerTests.authEnforcedResponseHeader): required",
435+
"Connection: \(closeConnection ? "close" : "keep-alive")",
436+
"",
437+
body
438+
].joined(separator: "\r\n")
439+
return Data(headers.utf8)
440+
}
441+
442+
private static func reasonPhrase(_ status: Int) -> String {
443+
switch status {
444+
case 200: return "OK"
445+
case 400: return "Bad Request"
446+
case 401: return "Unauthorized"
447+
case 404: return "Not Found"
448+
case 405: return "Method Not Allowed"
449+
case 408: return "Request Timeout"
450+
case 411: return "Length Required"
451+
case 413: return "Content Too Large"
452+
case 431: return "Request Header Fields Too Large"
453+
default: return "Internal Server Error"
454+
}
405455
}
406456
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ final class RunnerTests: XCTestCase {
6060
var listener: NWListener?
6161
var listenerToken: RunnerListenerToken?
6262
var doneExpectation: XCTestExpectation?
63+
var transportConnections: [ObjectIdentifier: NWConnection] = [:]
6364
let transportQueue = DispatchQueue(label: "agent-device.runner.transport")
6465
let commandExecutionQueue = DispatchQueue(label: "agent-device.runner.commands")
6566
let app = XCUIApplication()
@@ -106,7 +107,7 @@ final class RunnerTests: XCTestCase {
106107
// execution instead of enqueueing them again behind it (#1105 capture pileup).
107108
let inFlightCommandLock = NSLock()
108109
var inFlightCommandIds: Set<String> = []
109-
var inFlightCommandWaiters: [String: [@Sendable ((data: Data, shouldFinish: Bool)) -> Void]] = [:]
110+
var inFlightCommandWaiters: [String: [@Sendable ((response: RunnerHttpResponse, shouldFinish: Bool)) -> Void]] = [:]
110111
// Tracks main-queue work abandoned by the execution watchdog (runMainThreadWork). While any is
111112
// outstanding the main thread is occupied: new main-thread commands fail fast as busy instead
112113
// of queueing behind work that cannot be cancelled, capture plans skip XCTest-backed tiers,
@@ -247,6 +248,11 @@ final class RunnerTests: XCTestCase {
247248
continueAfterFailure = true
248249
}
249250

251+
override func tearDown() {
252+
transportQueue.sync { closeTransport() }
253+
super.tearDown()
254+
}
255+
250256
/// The XCTest fetch wordings whose recorded issue carries the AX server's own error text: the
251257
/// element snapshot fetch and query resolution (`allElementsBoundByIndex`, recorded once per
252258
/// element type by the query-sweep tier). Both read the target's tree through testmanagerd and

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+HttpRequestFramingTests.swift‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,30 @@ extension RunnerTests {
2424
let head = framingHead(authenticatedPost + ["Content-Length: 2"], body: "{}")
2525
XCTAssertEqual(
2626
admit(authenticatedPost + ["Content-Length: 2"], body: "{}"),
27-
.admitted(RunnerHttpAdmittedHead(bodyOffset: head.count - 2, contentLength: 2))
27+
.admitted(RunnerHttpAdmittedHead(bodyOffset: head.count - 2, contentLength: 2, keepsConnectionAlive: true))
2828
)
2929
}
3030

31+
func testHttpFramingResolvesConnectionPersistence() {
32+
let cases: [(String, [String], Bool)] = [
33+
("HTTP/1.1", [], true),
34+
("HTTP/1.1", ["Connection: close"], false),
35+
("HTTP/1.1", ["Connection: upgrade, CLOSE"], false),
36+
("HTTP/1.1", ["Connection: keep-alive", "connection: Close"], false),
37+
("HTTP/1.0", [], false),
38+
("HTTP/1.0", ["Connection: Keep-Alive"], true),
39+
("HTTP/1.0", ["Connection: keep-alive, close"], false),
40+
]
41+
for (version, connection, expected) in cases {
42+
let verdict = admit(["POST /command \(version)", authenticatedPost[1], "Content-Length: 0"] + connection)
43+
guard case .admitted(let head) = verdict else {
44+
XCTFail("unexpected refusal: \(verdict)")
45+
continue
46+
}
47+
XCTAssertEqual(head.keepsConnectionAlive, expected, "\(version) \(connection)")
48+
}
49+
}
50+
3151
func testHttpFramingWaitsForTheWholeHead() {
3252
let partial = Data("POST /command HTTP/1.1\r\nContent-Length: 2\r\n".utf8)
3353
XCTAssertEqual(
@@ -87,7 +107,7 @@ extension RunnerTests {
87107
guard firstVerdict == .incomplete else { continue }
88108
XCTAssertEqual(
89109
RunnerHttpRequestFraming.admitHead(request, token: framingListenerToken, alreadyScanned: split),
90-
.admitted(RunnerHttpAdmittedHead(bodyOffset: request.count, contentLength: 0)),
110+
.admitted(RunnerHttpAdmittedHead(bodyOffset: request.count, contentLength: 0, keepsConnectionAlive: true)),
91111
"split at \(split)"
92112
)
93113
}

0 commit comments

Comments
 (0)