Repository navigation
Fail pending RPCs when the room disconnects - #1067
Conversation
Room cleanup reports every remote participant as disconnected, and handleParticipantDisconnect turns that into a rejection, so a call addressed to a participant the room knows about already failed at disconnect. A call to an identity that never appeared in the participant list -- an agent that has not joined yet, say -- is in no such loop. Nothing failed it, so the caller stayed suspended on a room that had gone. If the call had been acked, the round-trip timer was already cancelled and nothing was left to fire until responseTimeout, minutes away by default. Unacked, the caller waited out maxRoundTripLatency and got CONNECTION_TIMEOUT, which is not what happened. RpcClientManager.failAllPending rejects whatever is left with RECIPIENT_DISCONNECTED, and cleanupRoom calls it after the participant loop. This matches the Rust SDK's fail_all_pending and the Swift SDK's handleAllPendingDisconnected, and ports livekit/client-sdk-js#2251. One test pins the path that already worked; the other covers the acked call to an unknown identity and fails with RESPONSE_TIMEOUT without the fix. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 553b1a8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| val stranded = synchronized(pendingResponses) { | ||
| val responses = pendingResponses.values.toList() | ||
| pendingResponses.clear() | ||
| responses | ||
| } | ||
| synchronized(pendingAcks) { | ||
| pendingAcks.clear() | ||
| } |
There was a problem hiding this comment.
🟡 Disconnect can return a connection timeout
If an ack timer fires during failAllPending, an unacknowledged RPC can return CONNECTION_TIMEOUT after disconnect begins. The timer still sees its ack after the sweep removes its response.
Learn more
The RPC manager keeps separate maps for acknowledgments and responses. The ack timer removes an ack and completes the call with CONNECTION_TIMEOUT when it finds one. During failAllPending, the response map is cleared before the ack map, so the timer can win after the disconnect sweep has started. The later disconnect completion cannot replace the timeout error on the deferred result.
Example: An unacknowledged call has an ack deadline at 7 seconds. At that deadline, disconnect copies and clears its response; the timer then removes its ack and completes CONNECTION_TIMEOUT; the sweep's RECIPIENT_DISCONNECTED completion loses.
Recommended fix: Clear both maps under their locks in a consistent lock order before invoking any onResolve callbacks. Ensure the ack timer cannot observe an ack whose response was already taken by failAllPending.
Was this helpful? React with 👍 or 👎 to provide feedback.
The test file is touched by this branch, so spotless's ratchet rolls its copyright range to the current year; `spotlessCheck` failed the build on the stale `2023-2025`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Diffuse output: AARJAR |
Ports livekit/client-sdk-js#2251 to Android.
What was wrong
Room cleanup reports every remote participant as disconnected, and
handleParticipantDisconnectturns that into a rejection — so a call addressed to a participant the room knows about already failed at disconnect. Android was in better shape here than the JS SDK, which had no RPC hook on disconnect at all.The gap is a call to an identity that never appeared in the participant list — an agent that has not joined yet, say. It is in no participant loop, so nothing failed it and the caller stayed suspended on a room that had gone:
responseTimeout— minutes by defaultRECIPIENT_DISCONNECTEDCONNECTION_TIMEOUTaftermaxRoundTripLatencyRECIPIENT_DISCONNECTEDThe acked case is the damaging one: the ack cancels the round-trip timer, so nothing was left to fire until the response timeout. Unacked, the caller only waited out
maxRoundTripLatencyand gotCONNECTION_TIMEOUT, which is not what happened to it.Fix
RpcClientManager.failAllPending()rejects whatever is left withRECIPIENT_DISCONNECTED, andcleanupRoom()calls it after the existing participant loop. No ack or response can arrive once the local participant has left, so there is nothing to wait for.This matches the Rust SDK's
fail_all_pending(called fromRoom::close) and the Swift SDK'shandleAllPendingDisconnected, both of which use the same error code.Tests
RpcMockE2ETest, 14 pass:disconnectRejectsPendingRpcToAKnownParticipant— guards the path that already worked. It joins the participant first and assertsremoteParticipants.containsKey(destination), so it cannot quietly stop testing that path. Passes with and without the fix, by design.disconnectRejectsAnAckedRpcToAnIdentityTheRoomNeverSaw— the gap. Fails without the fix withRESPONSE_TIMEOUT.RpcV2MockE2ETesthas 15 pre-existing failures locally, identical with and without this change (same count, same test names). They need a host build ofliblivekit_ffi, which the test task warns about:Two things for the Android owner
onFullReconnectinghas the same shape — it loops participants but does not sweep, so an unknown-identity RPC survives a full reconnect and then times out, even though the data channel it was sent on is gone. Swift does fail all on full reconnect; JS does not. I scoped this PR to the disconnect path to match the JS change rather than widen it unasked. Happy to extend it if you want the Swift behaviour.connectionTimeout(1501) for this case where Rust, Swift, JS and now Android userecipientDisconnected(1503). Worth reconciling somewhere, not here.🤖 Generated with Claude Code