diff --git a/.changeset/rpc-reject-pending-on-disconnect.md b/.changeset/rpc-reject-pending-on-disconnect.md new file mode 100644 index 000000000..cb8ecedbf --- /dev/null +++ b/.changeset/rpc-reject-pending-on-disconnect.md @@ -0,0 +1,9 @@ +--- +"client-sdk-android": patch +--- + +`performRpc` now fails as soon as the room disconnects, rather than staying suspended until its response timeout. + +Room cleanup reports every remote participant as disconnected, which already rejected calls addressed to a participant the room knew about. A call to an identity that never appeared in the participant list — an agent that has not joined yet, say — was in no such loop, so it stayed pending for the full `responseTimeout` after the room had gone. The caller's coroutine is now resumed with `RECIPIENT_DISCONNECTED` instead. + +This matches the Rust SDK's `fail_all_pending` and the Swift SDK's `handleAllPendingDisconnected` (livekit/client-sdk-js#2251). diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt index b4642f829..c240a8f19 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt @@ -1120,6 +1120,10 @@ constructor( remoteParticipants.keys.toMutableSet() // copy keys to avoid concurrent modifications. .forEach { sid -> handleParticipantDisconnect(sid) } + // The loop above only reaches calls addressed to a participant the room knows about. + // Sweep up anything else, so no caller is left waiting on a room that has gone. + localParticipant.failAllPendingRpcs() + sid = null metadata = null name = null diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt index 714f28d23..49b1746d5 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt @@ -1319,6 +1319,10 @@ internal constructor( rpcClientManager.handleParticipantDisconnect(identity) } + internal fun failAllPendingRpcs() { + rpcClientManager.failAllPending() + } + /** * @suppress */ diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/rpc/RpcClientManager.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/rpc/RpcClientManager.kt index db1e3b0d5..a75203304 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/rpc/RpcClientManager.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/rpc/RpcClientManager.kt @@ -322,6 +322,35 @@ class RpcClientManager @Inject constructor( } } + /** + * Rejects every in-flight call, whoever it was addressed to. Called when the room + * disconnects. + * + * Room cleanup already reports each remote participant as disconnected, which + * [handleParticipantDisconnect] turns into a rejection. That only covers calls addressed to + * a participant the room still knows about: a call to an identity that never appeared in the + * participant list -- an agent that has not joined yet, say -- is in neither loop, and would + * otherwise stay pending until its response timeout, long after the room has gone. + * + * No ack or response can arrive once the local participant has left, so there is nothing to + * wait for. + */ + fun failAllPending() { + // Resolve outside the locks: onResolve reaches back into pendingAcks. + val stranded = synchronized(pendingResponses) { + val responses = pendingResponses.values.toList() + pendingResponses.clear() + responses + } + synchronized(pendingAcks) { + pendingAcks.clear() + } + + for (response in stranded) { + response.onResolve(null, RpcError.BuiltinRpcError.RECIPIENT_DISCONNECTED.create()) + } + } + private data class PendingRpcAck( val participantIdentity: Identity, val onResolve: () -> Unit, diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/rpc/RpcMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/rpc/RpcMockE2ETest.kt index bb735b6a7..a2de175f5 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/rpc/RpcMockE2ETest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/rpc/RpcMockE2ETest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 LiveKit, Inc. + * Copyright 2023-2026 LiveKit, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import io.livekit.android.test.mock.MockPeerConnection import io.livekit.android.test.mock.TestData import io.livekit.android.test.mock.TestData.REMOTE_PARTICIPANT import io.livekit.android.test.util.toDataChannelBuffer +import io.livekit.android.util.toOkioByteString import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.async import kotlinx.coroutines.launch @@ -38,6 +39,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.minutes @ExperimentalCoroutinesApi @RunWith(RobolectricTestRunner::class) @@ -414,4 +416,80 @@ class RpcMockE2ETest : MockE2ETest() { ) assertTrue(methodCalled) } + + /** + * A call to a participant the room knows about is already covered: room cleanup reports + * every remote participant as disconnected. This guards that path. + */ + @Test + fun disconnectRejectsPendingRpcToAKnownParticipant() = runTest { + connect() + wsFactory.listener.onMessage(wsFactory.ws, TestData.PARTICIPANT_JOIN.toOkioByteString()) + val destination = Participant.Identity(REMOTE_PARTICIPANT.identity) + assertTrue( + "this test is only meaningful while the destination is a known participant", + room.remoteParticipants.containsKey(destination), + ) + + val rpcJob = async { performRpcCatchingError(destination) } + subDataChannel.simulateBufferReceived(createAck(sentRequestId())) + + room.disconnect() + + coroutineRule.dispatcher.scheduler.advanceUntilIdle() + assertEquals( + RpcError.BuiltinRpcError.RECIPIENT_DISCONNECTED.code, + (rpcJob.await() as RpcError).code, + ) + } + + /** + * A call to an identity that never appeared in the participant list -- an agent that has + * not joined yet, say -- is in no participant loop, so room cleanup never reached it. + * + * The ack here is what makes this the damaging case: it cancels the round-trip timer, so + * before the sweep in `cleanupRoom` nothing failed the call until `responseTimeout`, which + * defaults to minutes. Without it the caller only waited out `maxRoundTripLatency` and got + * the misleading `CONNECTION_TIMEOUT`. + */ + @Test + fun disconnectRejectsAnAckedRpcToAnIdentityTheRoomNeverSaw() = runTest { + connect() + val destination = Participant.Identity("never-joined") + assertFalse( + "this test is only meaningful while the destination is unknown to the room", + room.remoteParticipants.containsKey(destination), + ) + + val rpcJob = async { performRpcCatchingError(destination) } + subDataChannel.simulateBufferReceived(createAck(sentRequestId())) + + room.disconnect() + + coroutineRule.dispatcher.scheduler.advanceUntilIdle() + assertEquals( + RpcError.BuiltinRpcError.RECIPIENT_DISCONNECTED.code, + (rpcJob.await() as RpcError).code, + ) + } + + /** The id of the only RPC request published so far. */ + private fun sentRequestId(): String { + val buffers = pubDataChannel.sentBuffers + assertEquals(1, buffers.size) + return LivekitModels.DataPacket.parseFrom(ByteString.copyFrom(buffers[0].data)).rpcRequest.id + } + + private suspend fun performRpcCatchingError(destination: Participant.Identity): Exception? = + try { + room.localParticipant.performRpc( + destinationIdentity = destination, + method = "hello", + payload = "hello world", + responseTimeout = 5.minutes, + ) + null + } catch (e: Exception) { + e + } }