Modern Apps git

← Modern-Apps.git

findfamily: fix ping pong

Commit
3f4d986734f237ca84ba04d358b1aee1f154e061
Author
Vayun Mathur <[email protected]>
When
2026-08-14 15:03 UTC (3 months ago)
Parents
337ad8b

Diff

findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.ktindex 0d41ba9935..19c70fa2f9 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt@@ -177,6 +177,9 @@ object Networking {      */     private const val LIVENESS_TIMEOUT_MS = 75_000L +    /** How often the watchdog checks liveness. Must be well under the timeout. */+    private const val LIVENESS_CHECK_MS = 15_000L+     @Volatile private var wsSession: WsSession? = null     private var liveJob: Job? = null @@ -237,27 +240,34 @@ object Networking {                         send(sub)                         backoff = 1000L                         Log.d(TAG, "live WS connected, subscribed+registered as ${userid.toULong()} bundleLen=${bundle.size}")-                        // Keepalive + half-open detection: ping on a timer, and if no-                        // frame (pong/push/anything) has arrived within LIVENESS_TIMEOUT_MS-                        // treat the socket as half-open and close it to force a reconnect.-                        // A half-open socket still accepts writes, so ping success alone-                        // proves nothing โ€” only inbound traffic proves the socket is live.-                        // Cancelled when the incoming loop ends (below), ending this scope.+                        // Keepalive + half-open recovery. Two independent children:+                        //  * watchdog: only ever suspends on delay + does non-blocking work.+                        //    If no inbound frame (pong/push/anything) arrives within+                        //    LIVENESS_TIMEOUT_MS the socket is half-open, so it calls abort()+                        //    โ€” a hard socket close that can't block โ€” to force a reconnect.+                        //  * pinger: sends pings so a healthy server keeps sending pongs,+                        //    refreshing liveness. A ping write may block on a half-open+                        //    socket; that's fine, the watchdog's abort() unblocks it.+                        // Never call the graceful close() here: its close-frame write can hang+                        // on a half-open socket, wedging teardown and blocking reconnect.                         coroutineScope {                             val lastInboundMs = AtomicLong(System.currentTimeMillis())-                            val pinger = launch {+                            val watchdog = launch {                                 while (isActive) {-                                    delay(PING_INTERVAL_MS)+                                    delay(LIVENESS_CHECK_MS)                                     val idle = System.currentTimeMillis() - lastInboundMs.get()                                     if (idle > LIVENESS_TIMEOUT_MS) {-                                        Log.w(TAG, "no inbound for ${idle}ms; socket half-open, closing to reconnect")-                                        runCatching { close() }+                                        Log.w(TAG, "no inbound for ${idle}ms; socket half-open, aborting to reconnect")+                                        runCatching { abort() }                                         break                                     }+                                }+                            }+                            val pinger = launch {+                                while (isActive) {+                                    delay(PING_INTERVAL_MS)                                     runCatching { ping() }.onFailure {                                         if (it is CancellationException) throw it-                                        Log.w(TAG, "keepalive ping failed; closing to reconnect", it)-                                        runCatching { close() }                                     }                                 }                             }@@ -271,7 +281,11 @@ object Networking {                                     }                                 }                             } finally {+                                watchdog.cancel()                                 pinger.cancel()+                                // Force the socket down so a ping write blocked on a half-open+                                // socket unblocks and the pinger can actually complete.+                                runCatching { abort() }                             }                         }                     }
library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt b/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.ktindex 4845046125..f029ef8f46 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt@@ -126,6 +126,19 @@ class WebSocketClient private constructor(         } catch (_: Exception) { }     } +    /**+     * Hard, non-blocking teardown: closes the underlying socket immediately WITHOUT+     * the graceful close-frame write. Unlike [close], this can never hang on a+     * half-open socket (a blocking close-frame write into a full send buffer would).+     * Closing the socket unblocks any read or write currently blocked on it, so a+     * keepalive watchdog can use this to break a wedged connection and force a+     * reconnect. Safe to call from any thread; idempotent.+     */+    fun abort() {+        closed = true+        runCatching { socket.close() }+    }+     private fun sendPong() {         writeFrame(opcode = 0xA, payload = ByteArray(0), mask = true)         output.flush()@@ -388,6 +401,9 @@ class WsSession internal constructor(     /** Sends a PING frame. Callers that need keepalive run their own timer loop over this. */     suspend fun ping() = client.ping()     suspend fun close() = client.close()++    /** Hard, non-blocking teardown (see [WebSocketClient.abort]); unblocks a wedged read/write. */+    fun abort() = client.abort() }  suspend fun webSocket(