@@ -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.
388401final 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}
0 commit comments