From 525ff8117c55aa6e3ef4614822b16e3635ae728f Mon Sep 17 00:00:00 2001 From: davidliu Date: Wed, 25 Nov 2026 19:27:49 +0900 Subject: [PATCH 1/3] Trim the reliable message buffer after each send The reliable buffer was only trimmed when the data channel's bufferedAmount flow emitted. That flow is a StateFlow, and small packets leave the channel at once, so the value stays at 0 and never emits again. With room metrics on by default, two reliable packets a second piled up until the room disconnected. Trim with the channel's live bufferedAmount() after every reliable send, as the JS SDK does. The flow collector stays to trim after a drain. Fixes #1063 Co-Authored-By: Claude Opus 5.5 --- .changeset/reliable-buffer-trim-on-send.md | 5 ++++ .../java/io/livekit/android/room/RTCEngine.kt | 1 + .../android/room/RTCEngineMockE2ETest.kt | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 .changeset/reliable-buffer-trim-on-send.md diff --git a/.changeset/reliable-buffer-trim-on-send.md b/.changeset/reliable-buffer-trim-on-send.md new file mode 100644 index 000000000..bc1f966c2 --- /dev/null +++ b/.changeset/reliable-buffer-trim-on-send.md @@ -0,0 +1,5 @@ +--- +"client-sdk-android": patch +--- + +Fix memory growing without limit while a room stays connected. Reliable data packets are kept in a buffer so they can be resent after a resume, but the buffer was only trimmed when the data channel's buffered amount changed. Small packets, such as the client metrics sent twice a second by default, never change it, so every packet was kept until the room disconnected, about 2 MB an hour per room. The buffer is now trimmed after each reliable send, as in the JavaScript SDK (#1063). diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt index 7cd8b07d2..0de92ab86 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt @@ -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()) } } catch (e: Exception) { e.rethrowIfCancellationSignal() diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt index 4a0bebb61..aae2e4033 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt @@ -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() From b3093e5e0b46245a679f215b47d91d63a47f61db Mon Sep 17 00:00:00 2001 From: davidliu Date: Wed, 25 Nov 2026 19:38:50 +0900 Subject: [PATCH 2/3] Shorten the changeset to what users notice Co-Authored-By: Claude Opus 5.5 --- .changeset/reliable-buffer-trim-on-send.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/reliable-buffer-trim-on-send.md b/.changeset/reliable-buffer-trim-on-send.md index bc1f966c2..23b8c5751 100644 --- a/.changeset/reliable-buffer-trim-on-send.md +++ b/.changeset/reliable-buffer-trim-on-send.md @@ -2,4 +2,4 @@ "client-sdk-android": patch --- -Fix memory growing without limit while a room stays connected. Reliable data packets are kept in a buffer so they can be resent after a resume, but the buffer was only trimmed when the data channel's buffered amount changed. Small packets, such as the client metrics sent twice a second by default, never change it, so every packet was kept until the room disconnected, about 2 MB an hour per room. The buffer is now trimmed after each reliable send, as in the JavaScript SDK (#1063). +Fix memory usage growing steadily for as long as a room stays connected. From 8fd2e1226e2775500eaf217a63daa39486029dbd Mon Sep 17 00:00:00 2001 From: davidliu Date: Wed, 25 Nov 2026 19:38:50 +0900 Subject: [PATCH 3/3] Document changesets in AGENTS.md Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 1d311dd78..82f91698d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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. + ## Available Skills