@@ -11,6 +11,7 @@ private struct NodeInvokeRequestPayload: Codable, Sendable {
1111 var idempotencyKey : String ?
1212}
1313
14+
1415public actor GatewayNodeSession {
1516 private let logger = Logger ( subsystem : " ai.openclaw " , category : " node.gateway " )
1617 private let decoder = JSONDecoder ( )
@@ -23,6 +24,9 @@ public actor GatewayNodeSession {
2324 private var onConnected : ( @Sendable ( ) async -> Void ) ?
2425 private var onDisconnected : ( @Sendable ( String ) async -> Void ) ?
2526 private var onInvoke : ( @Sendable ( BridgeInvokeRequest ) async -> BridgeInvokeResponse ) ?
27+ private var hasNotifiedConnected = false
28+ private var snapshotReceived = false
29+ private var snapshotWaiters : [ CheckedContinuation < Bool , Never > ] = [ ]
2630
2731 static func invokeWithTimeout (
2832 request : BridgeInvokeRequest ,
@@ -78,6 +82,7 @@ public actor GatewayNodeSession {
7882 self . onInvoke = onInvoke
7983
8084 if shouldReconnect {
85+ self . resetConnectionState ( )
8186 if let existing = self . channel {
8287 await existing . shutdown ( )
8388 }
@@ -107,7 +112,8 @@ public actor GatewayNodeSession {
107112
108113 do {
109114 try await channel . connect ( )
110- await onConnected ( )
115+ _ = await self . waitForSnapshot ( timeoutMs : 500 )
116+ await self . notifyConnectedIfNeeded ( )
111117 } catch {
112118 await onDisconnected ( error . localizedDescription )
113119 throw error
@@ -120,6 +126,7 @@ public actor GatewayNodeSession {
120126 self . activeURL = nil
121127 self . activeToken = nil
122128 self . activePassword = nil
129+ self . resetConnectionState ( )
123130 }
124131
125132 public func currentCanvasHostUrl ( ) -> String ? {
@@ -179,14 +186,68 @@ public actor GatewayNodeSession {
179186 case let . snapshot ( ok ) :
180187 let raw = ok . canvashosturl ? . trimmingCharacters ( in : . whitespacesAndNewlines )
181188 self . canvasHostUrl = ( raw ? . isEmpty == false ) ? raw : nil
182- await self . onConnected ? ( )
189+ self . markSnapshotReceived ( )
190+ await self . notifyConnectedIfNeeded ( )
183191 case let . event ( evt ) :
184192 await self . handleEvent ( evt )
185193 default :
186194 break
187195 }
188196 }
189197
198+ private func resetConnectionState ( ) {
199+ self . hasNotifiedConnected = false
200+ self . snapshotReceived = false
201+ if ! self . snapshotWaiters . isEmpty {
202+ let waiters = self . snapshotWaiters
203+ self . snapshotWaiters . removeAll ( )
204+ for waiter in waiters {
205+ waiter . resume ( returning : false )
206+ }
207+ }
208+ }
209+
210+ private func markSnapshotReceived ( ) {
211+ self . snapshotReceived = true
212+ if ! self . snapshotWaiters . isEmpty {
213+ let waiters = self . snapshotWaiters
214+ self . snapshotWaiters . removeAll ( )
215+ for waiter in waiters {
216+ waiter . resume ( returning : true )
217+ }
218+ }
219+ }
220+
221+ private func waitForSnapshot ( timeoutMs : Int ) async -> Bool {
222+ if self . snapshotReceived { return true }
223+ let clamped = max ( 0 , timeoutMs )
224+ return await withCheckedContinuation { cont in
225+ self . snapshotWaiters . append ( cont )
226+ Task { [ weak self ] in
227+ guard let self else { return }
228+ try? await Task . sleep ( nanoseconds : UInt64 ( clamped ) * 1_000_000 )
229+ await self . timeoutSnapshotWaiters ( )
230+ }
231+ }
232+ }
233+
234+ private func timeoutSnapshotWaiters ( ) {
235+ guard ! self . snapshotReceived else { return }
236+ if ! self . snapshotWaiters . isEmpty {
237+ let waiters = self . snapshotWaiters
238+ self . snapshotWaiters . removeAll ( )
239+ for waiter in waiters {
240+ waiter . resume ( returning : false )
241+ }
242+ }
243+ }
244+
245+ private func notifyConnectedIfNeeded ( ) async {
246+ guard ! self . hasNotifiedConnected else { return }
247+ self . hasNotifiedConnected = true
248+ await self . onConnected ? ( )
249+ }
250+
190251 private func handleEvent ( _ evt : EventFrame ) async {
191252 self . broadcastServerEvent ( evt )
192253 guard evt . event == " node.invoke.request " else { return }
0 commit comments