diff --git a/.changeset/reliable-buffer-trim-on-send.md b/.changeset/reliable-buffer-trim-on-send.md new file mode 100644 index 000000000..23b8c5751 --- /dev/null +++ b/.changeset/reliable-buffer-trim-on-send.md @@ -0,0 +1,5 @@ +--- +"client-sdk-android": patch +--- + +Fix memory usage growing steadily for as long as a room stays connected. 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 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()