Skip to content

Bump uniffi-dart so the Dart bindings free native buffers - #1636

Merged
hiroshihorie merged 4 commits into
mainfrom
hiroshi/bump-uniffi-dart-pin
Nov 17, 2026
Merged

hiroshihorie merged 4 commits into
mainfrom
hiroshi/bump-uniffi-dart-pin

Conversation

@hiroshihorie

@hiroshihorie hiroshihorie commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

The generated Dart bindings leaked every byte or string payload that crossed the boundary. Upstream fixed it in Uniffi-Dart/uniffi-dart#180 and Uniffi-Dart/uniffi-dart#182. Every published livekit_ffi so far is generated from the 2026-08-31 pin and has the leak.

This moves tools/bindgens from 67547701 to 304bae06, upstream main as of 2026-11-17. There is no newer tagged release, and the pin stays on uniffi 0.31. The lockfile diff is the two uniffi-dart entries.

The public API of the generated package is unchanged. Also included from upstream:

unawaited_return_in_try_block and dead_code join the generated-code ignores in support/dart/analysis_options.yaml.

Checks

  • cargo make dart-package, dart analyze clean, 5 of 5 package tests, dart pub publish --dry-run 0 warnings.
  • e2e-flutter on macOS against client-sdk-flutter main (a40ca98e), data stream scenarios plus the Rust core load: 10 pass, 2 expected failures, the same as with the published package.
  • Memory, same e2e run, RSS growth:
published 0.13.4 this branch
30 x 8 MiB sendBytes round trips +1006 MB +254 and +623 MB in two runs
256 MiB sendFile, peak +826 MB +147 MB

The round trip reading is noisy on this branch because it depends on when garbage collection runs. Over 120 round trips it moves between +224 MB and +710 MB and falls back twice. The published package added about 300 MB every 10 round trips over the 30 it was measured for.

The generated Dart bindings leaked every byte or string payload that
crossed the boundary: the scratch allocated when lowering owned
arguments was never freed, and incoming RustBuffers were lifted as views
and never released. On a data stream round trip of 8 MiB the process
grew by about 44 MB per transfer and never gave it back.

Upstream fixed both in uniffi-dart #180 and #182. This moves the pin
from 67547701 (2026-08-31) to e644a4ad, upstream main, which also
carries #172 (disposal guards), #176 (dollar escaping in string
defaults) and #185 (CI only).

The regenerated runtime trips unawaited_return_in_try_block on a
synchronous lift inside a generic async helper, so that lint joins the
generated-code ignores in the package's analysis options. The publish
dry run is back to 0 warnings.

Measured with the e2e-flutter memory probe (30 x 8 MiB sendBytes round
trips between two participants on a local server, RSS after each batch
of 10): before +461, +903, +1317 MB; after +258, +232, +229 MB.
@hiroshihorie
hiroshihorie requested a review from ladvoc as a code owner November 6, 2026 07:14
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Changeset ✓

This PR includes a changeset covering all affected packages:

Package Bump
livekit-ffi patch

devin-ai-integration[bot]

This comment was marked as resolved.

Upstream main now also avoids temporary RustBuffers in enum and callback
conversion and completes a callback whose error cannot be encoded. The
regenerated callbacks trip dead_code on a guard that is never true, so
that lint joins the generated-code ignores.
@hiroshihorie
hiroshihorie merged commit cef6f57 into main Nov 17, 2026
68 of 70 checks passed
@hiroshihorie
hiroshihorie deleted the hiroshi/bump-uniffi-dart-pin branch November 17, 2026 17:42
hiroshihorie added a commit that referenced this pull request Nov 18, 2026
…t them (#1691)

The polled outgoing data stream adapter in `core-modules` acknowledged a
send as soon as its packets were buffered. A `send_*` or `write` call
resolved before anything reached the transport, transport errors were
lost, and nothing slowed a producer down. Flutter is the one SDK on this
adapter. It shows as unbounded memory growth on a large `sendFile` and
as send errors that never reach the caller. Swift and Android pass their
own delegate, which already resolves a send once the packets are handed
to the data channel, and this brings Flutter to the same point.

`OutgoingPacketQueue.next_packets()` is replaced by `next_batch()`,
which returns an `OutgoingPacketBatch` record: the `packets`, moved out
without a copy, and an `ack` object with `complete()` and
`fail(reason)`. The send resolves when the host completes the ack. It
fails when the ack fails, is released without an outcome, or the queue
is closed. The manager already awaits each delegate call, so at most one
batch is pending.

The queue is split into two halves. The manager holds the sending half
and the host holds `OutgoingPacketQueue`, the receiving half. Releasing
the queue then drops a batch nobody pulled, which fails its send and
lets the forwarding task finish. The sending half also watches the token
its manager cancels on drop, so the task finishes when the manager goes
away, whatever the host still holds. `outgoing.rs` gains a constructor
that takes that token. Its forwarding task is unchanged, so hosts that
pass their own delegate are not affected.

This is a breaking change for hosts on the polled adapter, so the
changeset is `livekit-ffi: major`.

### Checks

- `cargo test -p livekit-ffi --no-default-features --features
core-modules` passes, 78 tests. New `polled_*` tests cover the wait, a
failed batch, a dropped batch, a closed queue, a queue released with an
unpulled batch, a manager dropped while the host keeps the queue or a
pulled batch, and 200 repeated sends that check every ack is released.
- e2e-flutter on macOS against a local server, client-sdk-flutter with
livekit/client-sdk-flutter#1295. Run together with the uniffi-dart pin
from #1636. The table is from 37c5951, and the macOS run was repeated
on the current API with the same result:

| | published 0.13.4 | #1636 only | #1636 and this PR |
|---|---|---|---|
| 256 MiB `sendFile`, peak RSS growth | +826 MB | +147 MB | +45 MB |
| `sendText` then `disconnect()`, localhost | never arrives | never
arrives | arrives |
| other 10 data stream and core scenarios | pass | pass | pass |

The same scenarios pass on an iOS simulator and on an iPhone 17 Pro over
Wi-Fi, with one exception on the phone: `sendText` then `disconnect()`
is still not reliable there. A send now resolves when its packets are
handed to the data channel, and Flutter's `disconnect()` does not wait
for the channel to drain. The JS, Swift and Android SDKs send the Leave
without that wait too, so it is a gap of its own and not part of this
PR.
hiroshihorie added a commit to livekit/client-sdk-flutter that referenced this pull request Nov 19, 2026
## Summary

Data streams (`sendText`, `sendBytes`, `sendFile`, `streamText`,
`streamBytes`) on native platforms had three problems since they moved
to the shared Rust core:

- **Memory.** Sending a large file loaded all of it into memory. A 256
MiB file grew the app by about 826 MB. It now grows by about 43 MB.
- **Silent failures.** If a packet could not be sent, the error was only
logged and the call still reported success. The call now throws.
- **Finishing too early.** A send reported "done" as soon as the data
was queued inside the SDK, before any of it went out. It now reports
"done" once the data has been handed to WebRTC, which is what the JS,
Swift and Android SDKs do.

The fix is in the Rust core and shipped in `livekit_ffi` 0.15.0. This PR
moves Flutter to that version and makes the small change needed to use
it. That version also fixes a memory leak in the Dart bindings.

There is no public API change, and the web is not affected.

One thing this does **not** fix: a message sent right before
`disconnect()` can still be lost on a real network. See "Not covered"
below.

## Details

Companion to livekit/rust-sdks#1691. The dependency moves from `^0.13.4`
to `^0.15.0`, which also brings livekit/rust-sdks#1636, the bindings
that free native buffers.

Flutter cannot take callbacks from the Rust core's threads, so it pulls
outgoing packets from a queue. That queue used to acknowledge a send the
moment its packets were queued. In 0.15.0 the host acknowledges each
batch itself.

- The outgoing pump pulls a batch with `nextBatch()`, sends its
`packets`, then calls `ack.complete()`, or `ack.fail()` when the send
throws. The core resolves the send from that. Because the pump only
pulls the next batch after that, a fast producer is now paced by the
data channel.
- Closing a stream writer after the room closed stays quiet. The core
now reports that the trailer could not be sent, and closing is cleanup.

### Not covered

`await sendText()` followed at once by `disconnect()` is still not
reliable on a real network. A send now resolves when its packets are
handed to the data channel, and `disconnect()` does not wait for the
channel to drain. On an iPhone over Wi-Fi a 200 KB text was cut off, and
arrived with a one second pause before the disconnect. The other SDKs
disconnect the same way, so that is a gap of its own.

### Checks

Against the published `livekit_ffi` 0.15.0:

- `flutter analyze` clean, 596 unit tests pass.
- e2e-flutter data stream scenarios. The macOS column is with the
published package. The two iOS columns are from an earlier local build
of the same Rust change:

| | macOS | iOS simulator | iPhone 17 Pro, Wi-Fi |
|---|---|---|---|
| 10 data stream and core scenarios | pass | pass | pass |
| 256 MiB `sendFile`, peak RSS growth | +43 MB | not run | not run |
| `sendText` then `disconnect()` | arrives | arrives | not reliable |

The 826 MB figure is the same file send on macOS with `livekit_ffi`
0.13.4.
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.

2 participants