Repository navigation
🐛 Fix WebSocket reconnect upgrade, pending request fast-fail, send cancellation, and heartbeat probe - #5345
Conversation
…ncellation, and heartbeat probe
There was a problem hiding this comment.
NEEDS FIX
Round 1 review covers 6452b2ed9cb1d3c55b03e89ddddfc750b150d1f8. Found 4 P1 issues covering underlying channel cancellation, half-open detection on standard devices, concurrent handshakes, and pending request cleanup on session close. See inline comments for details.
Ran 108 targeted desktop tests on a temporary copy of this commit (covering WebSocket session management, message handling, auth integration, and sync resolution) — all passed. Verified all 4 findings with dedicated temporary repro tests. git diff --check passed, and GitHub app-build and ci checks passed.
Real-device mobile background/foreground transitions and cross-version reconnection were not live-tested.
There was a problem hiding this comment.
NEEDS FIX
Round 2 incremental review covers 6452b2ed..9a3b4c99. All 4 findings from round 1 have been verified as fixed and marked resolved.
Found 2 new P1 issues in this increment: application-level heartbeat timeouts can interrupt active sync transfers, and handshake deduplication can drop updated peer addresses/ports. See inline comments for details.
113 targeted desktop tests passed, along with the 2 previous repro tests on this commit. Verified both new findings with real Ktor loopback and MockEngine repro tests. git diff --check passed. Real-device mobile and cross-version reconnection remain untested.
…e dials on endpoint change
guiyanakuang
left a comment
There was a problem hiding this comment.
NEEDS FIX
I reviewed the changes from 9a3b4c99 to e638e9a1. Endpoint changes now cancel the stale handshake and dial the new address immediately. I resolved that thread.
The slow-ingestion heartbeat case is fixed. The existing P1 remains open because the probe can still close a healthy connection while an incoming chunked paste is making progress. I posted the reproduction on the existing thread.
All 117 targeted desktop tests passed. The two previous reproductions now pass, and a focused frame-reader test reproduces the remaining receive-side case. git diff --check passed. Mobile lifecycle and mixed-version reconnects were not tested.
guiyanakuang
left a comment
There was a problem hiding this comment.
PASS / LGTM
I reviewed e638e9a1..0265b204 and found no high-confidence issues. Both the client and server now record inbound frame progress during message assembly. The previous reproduction confirms that the session stays connected when chunks arrive during the heartbeat timeout window. I marked the remaining P1 thread as resolved.
All 127 targeted desktop tests and the temporary reproduction passed. Coverage included WebSocket authentication, legacy frame decoding, malformed chunk sequences, and idle heartbeat timeout handling. git diff --check passed. Mobile lifecycle and mixed-version reconnects were not tested on devices.
…eartbeat_ack support (#5346) Follow-up to #5345 improving WebSocket lifecycle and backpressure stability: - Add pause/resume gating to WsSessionManager, WsClientConnector, and WsRouting so pausing atomically rejects new sessions/handshakes, cancels in-flight dials, and closes active sessions. - Enforce per-session inbound business queue limits (128 MiB payload bytes / 256 messages) in dispatchWsSessionMessages, closing overflowing sessions with TRY_AGAIN_LATER (1013) so peers fall back to retry/HTTP without unbounded memory growth. - Advertise heartbeat_ack capability in the browser extension and echo requestId in HEARTBEAT_ACK responses while remaining backwards-compatible with legacy heartbeats.
Summary
Fixes 4 WebSocket reconnection and transport stability issues in shared
commonMaincode (tracked downstream in CrossPaste/crosspaste-mobile#2710):Rebuild WebSocket in
SyncResolver.verifyConnection()(and trust paths)attemptWebSocketUpgrade(host, port)was only invoked inauthenticate(). When a peer remainedSyncState.CONNECTEDin DB while its WebSocket session was absent (e.g., iOS resuming afterpause()calledcloseAll(), or any transient WS drop where HTTP heartbeat still succeeds),verifyConnection()returnedconnectStatewithout ever re-upgrading to WebSocket, permanently degrading to 60s HTTP polling.verifyConnection()invokesattemptWebSocketUpgrade(host, port)when HTTP heartbeat returnsSyncState.CONNECTED(guarded by existingisConnectedandWsConnectionPolicy.shouldInitiatechecks), and initial trust paths also trigger opportunistic upgrade immediately.Fast-fail in-flight
WsPendingRequestson session close / replacement /closeAll()WsPendingRequeststo(appInstanceId, session)using aSynchronizedObject-guarded map, and addedfailForSession,failForPeer, andfailAll(completing waiters withWsSessionClosedException).registerSession(on replacement for the old session),unregisterSession,notifySessionClosed,closeSession, andcloseAll, and handled non-cancellation failures inWsSessionManager.sendPastePushasWsPayloadSendResult.Failedso in-flight pushes/pulls unwind immediately instead of hanging for 30s.Do not swallow
CancellationExceptioninWsSessionManager.send()runCatchinginsend()(viasendToSession()) withtry / catch (e: CancellationException) { throw e } catch (e: Exception)so cancelling a caller coroutine no longer tears down a healthy WebSocket session.Correlated application-level heartbeat probe for half-open detection
WsCapability.HEARTBEAT_ACKto handshake capability negotiation and echoedrequestIdinWsMessageHandlerforHEARTBEAT/HEARTBEAT_ACK.WsSessionManager.probe()now sends a correlatedHEARTBEATand awaitsHEARTBEAT_ACKwithintimeoutMs(defaultWsSession.PING_TIMEOUT_MS) for peers advertisingWsCapability.HEARTBEAT_ACK, eliminating half-open TCP false positives while preservingsession.ping()fallback for legacy peers.Verification
./gradlew ktlintFormat && ./gradlew app:desktopTest ktlintCheck— all 2,358 fast-tier tests + ktlint passed../gradlew app:desktopTest -PintegrationTests --tests "com.crosspaste.net.ws.WsAuthenticationIntegrationTest"— all 5 integration tests passed.