Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/reliable-buffer-trim-on-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"client-sdk-android": patch
---

Fix memory usage growing steadily for as long as a room stays connected.
19 changes: 19 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ in tests.
- Add short code examples for new APIs to the entry point (e.g., `Room` class)
- Use `LKLog` for logging

## Changesets

- Add a changeset in `.changeset/` for every change SDK users will notice (fixes, new APIs,
behavior changes). Tests, CI and internal refactors don't need one.
- Write the file by hand, for example `.changeset/my-change.md`:

```md
---
"client-sdk-android": patch
---

Fix memory usage growing steadily for as long as a room stays connected.
```

- Use `patch` for fixes and `minor` for new APIs.
- Write it for app developers: one or two plain sentences about what they will notice. Leave out
internal details such as class names, root causes and implementation; those belong in the commit
message or PR.

<skills_system priority="1">

## Available Skills
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ internal constructor(
// ByteBuffer just sent has been drained by DataChannel.send.
reliableMessageBuffer.queue(DataPacketItem(ByteBuffer.wrap(packetBytes), dataPacket.sequence))
reliableDataSequence++
reliableMessageBuffer.trim(channel.bufferedAmount())

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.

🔴 Reliable publish crash during teardown

If disconnect or full reconnect disposes the channel after a successful send, bufferedAmount() can fail the publish. Channel disposal does not share reliableStateLock. If disposal clears the pointer between WebRTC’s Java check and native lookup, the app can crash.

Learn more

publishData calls the engine from the caller’s coroutine, and reliable sends take reliableStateLock. Room disconnect and full reconnect tear down the publisher separately. closeResources runs on the RTC executor and calls closeEarlyPublisher, which disposes the reliable channel without the send lock. In WebRTC M150, bufferedAmount() checks the Java native pointer before JNI reads it again, and dispose() releases and clears that pointer. The Java exception turns the accepted send into a failure; a JNI lookup of the cleared pointer is not catchable.

Example: A participant publishes while the room starts a full reconnect. WebRTC accepts the packet, then the RTC executor disposes the old publisher channel. Reading the amount from that old channel can return a publish failure even though WebRTC accepted the packet.

Recommended fix: Serialize the channel lookup, send, and buffered-amount read with disposal on the RTC executor. Acquire the reliable-state lock inside the executor work rather than waiting for the RTC executor while holding it. Keep sequence assignment and buffer mutation ordered with replay, and verify that a post-send trim cannot change an accepted publish into a failure.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The IllegalStateException is caught by the surrounding try/catch in sendDataImpl, so it can't crash; at worst a send during teardown reports failure which is acceptable.

}
} catch (e: Exception) {
e.rethrowIfCancellationSignal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,30 @@ class RTCEngineMockE2ETest : MockE2ETest() {
assertArrayEquals(payload, replay2.user.payload.toByteArray())
}

/**
* Regression for https://github.com/livekit/client-sdk-android/issues/1063: the reliable buffer
* must be trimmed on send, even when the channel's buffered amount never changes.
*/
@Test
fun reliableBufferTrimsOnSendWithoutBufferedAmountChange() = runTest {
connect()
val pubDataChannel = getPublisherPeerConnection()
.dataChannels[RTCEngine.RELIABLE_DATA_CHANNEL_LABEL] as MockDataChannel

// 50 x 60 KB = 3 MB, past the 2.5 MB retry allowance; bufferedAmount stays 0 throughout.
val packetCount = 50
repeat(packetCount) {
assertTrue(room.localParticipant.publishData(ByteArray(60_000)).isSuccess)
}
pubDataChannel.clearSentBuffers()

assertTrue(rtcEngine.resendReliableMessagesForResume(0).isSuccess)

val replayedSequences = pubDataChannel.sentPayloads.map { DataPacket.parseFrom(it).sequence }
assertTrue("oldest packets should be trimmed, got $replayedSequences", replayedSequences.first() > 1)
assertEquals(packetCount, replayedSequences.last())
}

@Test
fun resendReliableMessagesReturnsFailureWhenReplaySendFails() = runTest {
connect()
Expand Down
Loading