Skip to content

Commit 7a7d27e

Browse files
author
hclsys
committed
fix(android): gate sendNodeEvent on handshake completion via isHandshakeComplete flag
The previous approach moved currentConnection assignment to after conn.connect() returned, but onConnected fires INSIDE connect() (via connectDeferred.complete), creating a window where the test observes the connected state before currentConnection is set. Add Connection.isHandshakeComplete (set before onConnected in handleConnectSuccess) and check it in sendNodeEvent/sendNodeEventDetailed. Keep currentConnection assignment before connect() so the reference is stable during reconnect loops.
1 parent 52a8602 commit 7a7d27e

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

apps/android/app/src/main/java/ai/openclaw/app/gateway/GatewaySession.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class GatewaySession(
217217
event: String,
218218
payloadJson: String?,
219219
): Boolean {
220-
val conn = currentConnection ?: return false
220+
val conn = currentConnection?.takeIf { it.isHandshakeComplete } ?: return false
221221
return try {
222222
conn.request(
223223
"node.event",
@@ -259,7 +259,7 @@ class GatewaySession(
259259
timeoutMs: Long = 8_000,
260260
): RpcResult {
261261
val conn =
262-
currentConnection
262+
currentConnection?.takeIf { it.isHandshakeComplete }
263263
?: return RpcResult(
264264
ok = false,
265265
payloadJson = null,
@@ -332,6 +332,7 @@ class GatewaySession(
332332
) {
333333
private val connectDeferred = CompletableDeferred<Unit>()
334334
private val closedDeferred = CompletableDeferred<Unit>()
335+
@Volatile var isHandshakeComplete = false
335336
private val isClosed = AtomicBoolean(false)
336337
private val connectNonceDeferred = CompletableDeferred<String>()
337338
private val client: OkHttpClient = buildClient()
@@ -617,6 +618,7 @@ class GatewaySession(
617618
?.get("sessionDefaults")
618619
.asObjectOrNull()
619620
mainSessionKey = sessionDefaults?.get("mainSessionKey").asStringOrNull()
621+
isHandshakeComplete = true
620622
onConnected(serverName, remoteAddress, mainSessionKey)
621623
}
622624

@@ -898,9 +900,9 @@ class GatewaySession(
898900
target.options,
899901
target.tls,
900902
)
903+
currentConnection = conn
901904
try {
902905
conn.connect()
903-
currentConnection = conn
904906
conn.awaitClose()
905907
} finally {
906908
currentConnection = null

0 commit comments

Comments
 (0)