Skip to content

Fail pending RPCs when the room disconnects - #1067

Merged
xianshijing-lk merged 2 commits into
mainfrom
sxian/rpc-reject-pending-on-disconnect
Nov 14, 2026
Merged

xianshijing-lk merged 2 commits into
mainfrom
sxian/rpc-reject-pending-on-disconnect

Conversation

@xianshijing-lk

Copy link
Copy Markdown
Contributor

Ports livekit/client-sdk-js#2251 to Android.

What was wrong

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. 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:

before after
acked, then disconnect pending until responseTimeout — minutes by default RECIPIENT_DISCONNECTED
not acked, then disconnect CONNECTION_TIMEOUT after maxRoundTripLatency RECIPIENT_DISCONNECTED

The 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 maxRoundTripLatency and got CONNECTION_TIMEOUT, which is not what happened to it.

Fix

RpcClientManager.failAllPending() rejects whatever is left with RECIPIENT_DISCONNECTED, and cleanupRoom() 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 from Room::close) and the Swift SDK's handleAllPendingDisconnected, 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 asserts remoteParticipants.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 with RESPONSE_TIMEOUT.

RpcV2MockE2ETest has 15 pre-existing failures locally, identical with and without this change (same count, same test names). They need a host build of liblivekit_ffi, which the test task warns about:

[livekit] No host build of liblivekit_ffi found.
Tests that exercise data streams will fail to load the native library.

Two things for the Android owner

  1. onFullReconnecting has 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.
  2. Flutter uses connectionTimeout (1501) for this case where Rust, Swift, JS and now Android use recipientDisconnected (1503). Worth reconciling somewhere, not here.

🤖 Generated with Claude Code

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-bot

changeset-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 553b1a8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
client-sdk-android Patch

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

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Devin Review

Comment on lines +340 to +347
val stranded = synchronized(pendingResponses) {
val responses = pendingResponses.values.toList()
pendingResponses.clear()
responses
}
synchronized(pendingAcks) {
pendingAcks.clear()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Devin Review


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>
@github-actions

Copy link
Copy Markdown
Contributor

Diffuse output:

OLD: diffuse-source-file
NEW: livekit-android-sdk-release.aar

 AAR      │ old      │ new      │ diff    
──────────┼──────────┼──────────┼─────────
      jar │  3.1 MiB │  3.1 MiB │ +36 KiB 
 manifest │  1.5 KiB │  1.5 KiB │     0 B 
 lint-jar │ 12.7 KiB │ 12.7 KiB │     0 B 
    other │  2.4 KiB │  2.4 KiB │     0 B 
──────────┼──────────┼──────────┼─────────
    total │  3.1 MiB │  3.1 MiB │ +36 KiB 

 JAR     │ old   │ new   │ diff            
─────────┼───────┼───────┼─────────────────
 classes │  1780 │  1794 │  +14 (+19 -5)   
 methods │ 22545 │ 22661 │ +116 (+151 -35) 
  fields │  5938 │  5984 │  +46 (+61 -15)
AAR
 size    │ diff    │ path          
─────────┼─────────┼───────────────
 3.1 MiB │ +36 KiB │ ∆ classes.jar 
─────────┼─────────┼───────────────
 3.1 MiB │ +36 KiB │ (total)
JAR
CLASSES:

   old  │ new  │ diff         
  ──────┼──────┼──────────────
   1780 │ 1794 │ +14 (+19 -5) 
  
  + io.livekit.android.room.DuplexTransportObserver_WhenMappings
  + io.livekit.android.room.DuplexTransportObserver_onAddTrack_1
  + io.livekit.android.room.DuplexTransportObserver
  + io.livekit.android.room.RTCEngine_attachBoundTracks_1
  + io.livekit.android.room.RTCEngine_configure_2_2_4_1_1_1
  + io.livekit.android.room.RTCEngine_configure_2_2_4_1_1_2
  + io.livekit.android.room.RTCEngine_configure_2_2_4_1_1
  + io.livekit.android.room.RTCEngine_configure_2_2_5
  + io.livekit.android.room.SignalClient_connect_4_1_1
  + io.livekit.android.room.SignalClient_connect_4
  + io.livekit.android.room.SinglePeerConnectionTransport_applyServerAnswer_1
  + io.livekit.android.room.SinglePeerConnectionTransport_attachBoundTracks_2
  + io.livekit.android.room.SinglePeerConnectionTransport_handleMediaSectionsRequirement_2
  + io.livekit.android.room.SinglePeerConnectionTransport_syncStateDescriptions_1
  + io.livekit.android.room.SinglePeerConnectionTransport_syncStateDescriptions_2
  + io.livekit.android.room.SinglePeerConnectionTransport_trackIdForReceiver_mid_1_1
  + io.livekit.android.room.SinglePeerConnectionTransport_trackIdForReceiver_mid_1
  + io.livekit.android.room.SinglePeerConnectionTransport
  + io.livekit.android.room.util.SignalUrlBuilder
  
  - io.livekit.android.room.RTCEngine_configure_2_2_3_1_1_1
  - io.livekit.android.room.RTCEngine_configure_2_2_3_1_1_2
  - io.livekit.android.room.RTCEngine_configure_2_2_3_1_1
  - io.livekit.android.room.SignalClient_connect_3_1_1
  - io.livekit.android.room.SignalClient_connect_3
  

METHODS:

   old   │ new   │ diff            
  ───────┼───────┼─────────────────
   22545 │ 22661 │ +116 (+151 -35) 
  
  + io.livekit.android.ConnectOptions <init>(boolean, List, PeerConnection_RTCConfiguration, boolean, boolean, ProtocolVersion, ClientProtocolVersion, boolean)
  + io.livekit.android.ConnectOptions <init>(boolean, List, PeerConnection_RTCConfiguration, boolean, boolean, ProtocolVersion, ClientProtocolVersion, boolean, int, DefaultConstructorMarker)
  + io.livekit.android.ConnectOptions component8() → boolean
  + io.livekit.android.ConnectOptions copy(boolean, List, PeerConnection_RTCConfiguration, boolean, boolean, ProtocolVersion, ClientProtocolVersion, boolean) → ConnectOptions
  + io.livekit.android.ConnectOptions copy_default(ConnectOptions, boolean, List, PeerConnection_RTCConfiguration, boolean, boolean, ProtocolVersion, ClientProtocolVersion, boolean, int, Object) → ConnectOptions
  + io.livekit.android.ConnectOptions getSinglePeerConnection() → boolean
  + io.livekit.android.room.DualPeerConnectionTransport attachBoundTracks(Continuation) → Object
  + io.livekit.android.room.DuplexTransportObserver <init>(RTCEngine, SignalClient, RTCThreadToken)
  + io.livekit.android.room.DuplexTransportObserver onAddTrack(RtpReceiver, MediaStream[])
  + io.livekit.android.room.DuplexTransportObserver onTrack(RtpTransceiver)
  + io.livekit.android.room.DuplexTransportObserver_WhenMappings <clinit>()
  + io.livekit.android.room.DuplexTransportObserver_onAddTrack_1 <init>(RtpReceiver, MediaStream[], DuplexTransportObserver)
  + io.livekit.android.room.DuplexTransportObserver_onAddTrack_1 invoke() → Object
  + io.livekit.android.room.DuplexTransportObserver_onAddTrack_1 invoke()
  + io.livekit.android.room.PeerConnectionTransport isStaleAnswer(int) → boolean
  + io.livekit.android.room.PeerConnectionTransport_setRemoteDescription_result_1 <init>(SessionDescription, PeerConnectionTransport, int, Continuation)
  + io.livekit.android.room.PublisherTransportObserver getClient() → SignalClient
  + io.livekit.android.room.PublisherTransportObserver getEngine() → RTCEngine
  + io.livekit.android.room.PublisherTransportObserver getRtcThreadToken() → RTCThreadToken
  + io.livekit.android.room.RTCEngine access_isSubscriberPrimary_p(RTCEngine) → boolean
  + io.livekit.android.room.RTCEngine access_setConnectOptions_p(RTCEngine, ConnectOptions)
  + io.livekit.android.room.RTCEngine access_setPublisherObserver_p(RTCEngine, PublisherTransportObserver)
  + io.livekit.android.room.RTCEngine attachBoundTracks_livekit_android_sdk_release()
  + io.livekit.android.room.RTCEngine_Listener onTrackBound(RtpReceiver, MediaStreamTrack, String) → boolean
  + io.livekit.android.room.RTCEngine_Listener_DefaultImpls onTrackBound(RTCEngine_Listener, RtpReceiver, MediaStreamTrack, String) → boolean
  + io.live
...✂

@xianshijing-lk
xianshijing-lk merged commit fe7acd4 into main Nov 14, 2026
6 checks passed
@xianshijing-lk
xianshijing-lk deleted the sxian/rpc-reject-pending-on-disconnect branch November 14, 2026 04:31
@davidliu davidliu mentioned this pull request Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants