Skip to content

Trim the reliable message buffer after each send - #1077

Open
davidliu wants to merge 3 commits into
mainfrom
dl/reliable-buffer-trim-on-send
Open

davidliu wants to merge 3 commits into
mainfrom
dl/reliable-buffer-trim-on-send

Conversation

@davidliu

Copy link
Copy Markdown
Contributor

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 due to conflation. With room metrics on by default, reliable packets a second piled up until the room disconnected if there weren't any large data packets to cause backpressure and have bufferedAmount emit values.

This change does a 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 noreply@anthropic.com

davidliu and others added 3 commits November 25, 2026 19:27
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 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8fd2e12

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.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

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

@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 │ +42 B 
 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.2 MiB │  3.2 MiB │ +42 B 

 JAR     │ old   │ new   │ diff      
─────────┼───────┼───────┼───────────
 classes │  1807 │  1807 │ 0 (+0 -0) 
 methods │ 22721 │ 22721 │ 0 (+0 -0) 
  fields │  6031 │  6031 │ 0 (+0 -0)
AAR
 size    │ diff  │ path          
─────────┼───────┼───────────────
 3.1 MiB │ +42 B │ ∆ classes.jar 
─────────┼───────┼───────────────
 3.1 MiB │ +42 B │ (total)

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.

reliableMessageBuffer is never trimmed while connected, so default room metrics grow the heap without bound

1 participant