diff --git a/packages/kt-client/src/main/kotlin/dev/pyric/bridge/BridgeHeartbeat.kt b/packages/kt-client/src/main/kotlin/dev/pyric/bridge/BridgeHeartbeat.kt index 4a0fca02a4..8e6081d61b 100644 --- a/packages/kt-client/src/main/kotlin/dev/pyric/bridge/BridgeHeartbeat.kt +++ b/packages/kt-client/src/main/kotlin/dev/pyric/bridge/BridgeHeartbeat.kt @@ -36,8 +36,10 @@ internal class BridgeHeartbeat( /** * Replaces any running heartbeat. [ping] sends one frame on the socket and - * returns false when that socket is no longer the current one, which ends the - * heartbeat quietly. [onSilent] reports the drop; it also runs when [ping] throws. + * returns false when that socket is no longer the current one or cannot take + * the frame, which ends the heartbeat quietly. A socket that cannot take a + * frame is closing, and its own close reports the drop with the real close + * code. [onSilent] runs only for silence past the timeout. */ fun start(intervalMs: Long, timeoutMs: Long, ping: () -> Boolean, onSilent: () -> Unit) { synchronized(lock) { @@ -61,11 +63,13 @@ internal class BridgeHeartbeat( onSilent() return } + // A ping that cannot be sent is not a drop: reporting one here would + // replace the close code the socket is about to report, and turn a + // policy close into a reconnect. val sent = try { ping() } catch (_: Throwable) { - onSilent() - return + false } if (!sent) return // The next check falls at the timeout when no frame arrives before it. diff --git a/packages/kt-client/src/main/kotlin/dev/pyric/bridge/PyricBridgeClient.kt b/packages/kt-client/src/main/kotlin/dev/pyric/bridge/PyricBridgeClient.kt index c6f1420cd9..1ffa8b0b11 100644 --- a/packages/kt-client/src/main/kotlin/dev/pyric/bridge/PyricBridgeClient.kt +++ b/packages/kt-client/src/main/kotlin/dev/pyric/bridge/PyricBridgeClient.kt @@ -384,14 +384,8 @@ class PyricBridgeClient( if (isCurrentSocket) transport else null } ?: return false val frame = BridgeProtocol.createPingFrame("ping-${pingCounter.incrementAndGet()}") - val enqueued = socket.send(JsonCodec.encodeToString(frame)) - if (!enqueued) { - throw FirebaseFirestoreException( - "Failed to enqueue ping to bridge transport.", - FirebaseFirestoreException.Code.UNAVAILABLE - ) - } - return true + // False when the socket is closing; its close callback reports the drop. + return socket.send(JsonCodec.encodeToString(frame)) } private fun setState(next: BridgeConnectionState) { diff --git a/packages/kt-client/src/test/kotlin/dev/pyric/bridge/BridgeReconnectTest.kt b/packages/kt-client/src/test/kotlin/dev/pyric/bridge/BridgeReconnectTest.kt index 09175b5779..862efdd440 100644 --- a/packages/kt-client/src/test/kotlin/dev/pyric/bridge/BridgeReconnectTest.kt +++ b/packages/kt-client/src/test/kotlin/dev/pyric/bridge/BridgeReconnectTest.kt @@ -65,6 +65,18 @@ class FakeReconnectSocket( open = false listener.onClosed(code, "dropped") } + + /** + * The peer's close is under way but not yet reported: [send] returns false, + * as OkHttp's does on a closing socket, until [finishClosing] reports it. + */ + fun beginClosing() { + open = false + } + + fun finishClosing(code: Int) { + listener.onClosed(code, "closed by peer") + } } /** Hands out a new socket per connection and records every frame across all of them. */ @@ -602,6 +614,27 @@ class BridgeReconnectTest { assertEquals(1, bridge.ofType("attach").size) } + @Test + fun `a ping refused by a closing socket leaves the drop to its close, so a policy close is not retried`() = runBlocking { + val client = client(heartbeatIntervalMs = 20L) + client.connect() + val socket = bridge.current + until { socket.pings() >= 1 } + + socket.beginClosing() + val pingsAtClosing = socket.pings() + until { socket.pings() > pingsAtClosing } + delay(100) + assertEquals(BridgeConnectionState.ATTACHED, client.connectionState, "a refused ping is not a drop") + assertEquals(1, bridge.connectCalls.get(), "a refused ping must not start a reconnect") + + socket.finishClosing(1008) + until { client.connectionState == BridgeConnectionState.CLOSED } + delay(50) + assertEquals(1, bridge.ofType("attach").size) + assertEquals(1, bridge.connectCalls.get()) + } + @Test fun `a replaced host runs the Auth restore before any listen is re-sent`() = runBlocking { val client = client()