@@ -114,6 +114,8 @@ final actor NewLambdaRuntimeClient: LambdaRuntimeClientProtocol {
114114 }
115115
116116 private func close( ) async {
117+ self . logger. trace ( " Close lambda runtime client " )
118+
117119 guard case . notClosing = self . closingState else {
118120 return
119121 }
@@ -229,10 +231,24 @@ final actor NewLambdaRuntimeClient: LambdaRuntimeClientProtocol {
229231
230232 private func channelClosed( _ channel: any Channel ) {
231233 switch ( self . connectionState, self . closingState) {
232- case ( . disconnected, _) ,
233- ( _, . closed) :
234+ case ( _, . closed) :
234235 fatalError ( " Invalid state: \( self . connectionState) , \( self . closingState) " )
235236
237+ case ( . disconnected, . notClosing) :
238+ if let index = self . closingConnections. firstIndex ( where: { $0 === channel } ) {
239+ self . closingConnections. remove ( at: index)
240+ }
241+
242+ case ( . disconnected, . closing( let continuation) ) :
243+ if let index = self . closingConnections. firstIndex ( where: { $0 === channel } ) {
244+ self . closingConnections. remove ( at: index)
245+ }
246+
247+ if self . closingConnections. isEmpty {
248+ self . closingState = . closed
249+ continuation. resume ( )
250+ }
251+
236252 case ( . connecting( let array) , . notClosing) :
237253 self . connectionState = . disconnected
238254 for continuation in array {
@@ -303,8 +319,14 @@ final actor NewLambdaRuntimeClient: LambdaRuntimeClientProtocol {
303319 let handler = try channel. pipeline. syncOperations. handler (
304320 type: LambdaChannelHandler< NewLambdaRuntimeClient> . self
305321 )
322+ self . logger. trace (
323+ " Connection to control plane created " ,
324+ metadata: [
325+ " lambda_port " : " \( self . configuration. port) " ,
326+ " lambda_ip " : " \( self . configuration. ip) " ,
327+ ]
328+ )
306329 channel. closeFuture. whenComplete { result in
307- self . eventLoop. preconditionInEventLoop ( )
308330 self . assumeIsolated { runtimeClient in
309331 runtimeClient. channelClosed ( channel)
310332 }
@@ -754,6 +776,12 @@ extension LambdaChannelHandler: ChannelInboundHandler {
754776 }
755777
756778 func errorCaught( context: ChannelHandlerContext , error: Error ) {
779+ self . logger. trace (
780+ " Channel error caught " ,
781+ metadata: [
782+ " error " : " \( error) "
783+ ]
784+ )
757785 // pending responses will fail with lastError in channelInactive since we are calling context.close
758786 self . delegate. connectionErrorHappened ( error, channel: context. channel)
759787
0 commit comments