From 349cd23e0e0330f2191649d40006dad6c9e0b9b7 Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:40:46 +0800 Subject: [PATCH 1/7] Report a data stream batch as sent or failed after sending it The outgoing pump pulls a batch, sends its packets and then completes the batch, or fails it when the send throws. The core resolves the send from that outcome, so a send error reaches the caller and the pump no longer logs and drops it. --- AGENTS.md | 2 +- lib/src/data_stream/data_streams_native.dart | 26 +++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 759fbe82d..73d906971 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,7 +45,7 @@ There is no dynamic library to load on the web, so `ffi.dart` splits native/web Two things to know when touching the native path: - **The core's push delegates cannot be used from Dart.** uniffi compiles a callback interface to `Pointer.fromFunction`, which is only valid on the isolate's thread, and the core invokes those delegates from its tokio runtime — the VM aborts with `Cannot invoke native callback outside an isolate`, which is not catchable. The managers are therefore built through the crate's `polled*` adapters (`livekit-ffi/src/core_modules/data_stream/polled.rs`), which implement the delegates *in Rust*, buffer into a channel, and expose an `async fn next_*` we await. `RemoteParticipantRegistryDelegate` is the one callback we implement directly, and it is safe: it is only called synchronously inside a `send*` future, which uniffi polls from the calling (Dart) thread. -- **Whoever awaits a `livekit_ffi` object is the only thing that may dispose it.** Freeing the Rust handle while a `next()`/`nextPackets()` is in flight is a use-after-free that surfaces as a SIGBUS with no Dart stack. Hence readers are disposed by their pump rather than from a subscription's `onCancel`, and `dispose()` calls `close()` on the queues to wake their pumps instead of releasing them directly. +- **Whoever awaits a `livekit_ffi` object is the only thing that may dispose it.** Freeing the Rust handle while a `next()`/`nextBatch()` is in flight is a use-after-free that surfaces as a SIGBUS with no Dart stack. Hence readers are disposed by their pump rather than from a subscription's `onCancel`, and `dispose()` calls `close()` on the queues to wake their pumps instead of releasing them directly. ### Local development loop diff --git a/lib/src/data_stream/data_streams_native.dart b/lib/src/data_stream/data_streams_native.dart index 0bb2bc9a5..6c74263a7 100644 --- a/lib/src/data_stream/data_streams_native.dart +++ b/lib/src/data_stream/data_streams_native.dart @@ -248,8 +248,8 @@ class NativeDataStreams implements DataStreams { ); } - /// Fails a send the room can no longer deliver. The core reports a send as done once its - /// packets are queued for the pump, so without this a send on a closed room would look sent. + /// Fails a send the room can no longer deliver before it reaches the core, so the caller gets + /// the same error as on any other use of a closed room. void _ensureOpen() { final room = _room.target; if (room == null || _disposed || room.engine.isClosed) { @@ -264,15 +264,20 @@ class NativeDataStreams implements DataStreams { /// without a separate send chain and paces the wire as `WritableStream` did on main. /// /// The pump owns the queue's lifetime and is the only thing that may dispose it — freeing it - /// while a `nextPackets` is in flight is a use-after-free. [dispose] wakes us by closing the + /// while a `nextBatch` is in flight is a use-after-free. [dispose] wakes us by closing the /// queue rather than releasing it. Future _pumpOutgoing() async { try { while (true) { - final batch = await _outgoingPackets.nextPackets(); + final batch = await _outgoingPackets.nextBatch(); if (batch == null) break; // closed or shutting down - for (final encoded in batch) { - await _send(encoded); + try { + await _send(batch.packets()); + batch.complete(); + } catch (e) { + batch.fail(reason: '$e'); + } finally { + batch.dispose(); } } } catch (e) { @@ -282,10 +287,10 @@ class NativeDataStreams implements DataStreams { } } - Future _send(Uint8List encoded) async { + Future _send(List packets) async { final room = _room.target; - if (room == null || _disposed) return; - try { + if (room == null || _disposed) throw StateError('data streams were disposed'); + for (final encoded in packets) { await room.engine.waitForBufferStatusLow(Reliability.reliable); // Back through the engine rather than the data channel directly, so E2EE wrapping, // reliable sequencing and resume-resend all still apply. @@ -293,9 +298,6 @@ class NativeDataStreams implements DataStreams { lk_models.DataPacket.fromBuffer(encoded), reliability: Reliability.reliable, ); - } catch (e) { - // The core acknowledges sends unconditionally, so there is nobody to propagate this to. - logger.warning('[DataStreams] failed to send outbound packet: $e'); } } From 61ae6c6918cb80bbd71c43cf6a190d37f1adcb0d Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:40:46 +0800 Subject: [PATCH 2/7] Keep closing a stream writer quiet once the room is closed The trailer cannot be sent on a closed room, and the core now reports that. Closing is cleanup, so the failure is not passed on. --- lib/src/data_stream/data_streams_native.dart | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/src/data_stream/data_streams_native.dart b/lib/src/data_stream/data_streams_native.dart index 6c74263a7..e5ef00113 100644 --- a/lib/src/data_stream/data_streams_native.dart +++ b/lib/src/data_stream/data_streams_native.dart @@ -614,6 +614,21 @@ class _Registry implements ffi.RemoteParticipantRegistryDelegate { Participant? _participant(String identity) => _room.target?.remoteParticipants[identity]; } +/// Closes a writer. Once the room is closed its trailer can no longer be sent, and because closing +/// is cleanup, that failure is not reported. +Future _closeWriter(Future Function() close, void Function() ensureOpen) async { + try { + await mappingFfiErrors(close); + } on DataStreamError { + try { + ensureOpen(); + } on UnexpectedConnectionState { + return; + } + rethrow; + } +} + /// Bridges the core's text writer onto the [StreamWriter] the public writer wraps. class _FfiTextStreamWriter implements StreamWriter { _FfiTextStreamWriter(this._writer, this._ensureOpen); @@ -628,7 +643,7 @@ class _FfiTextStreamWriter implements StreamWriter { } @override - Future close() => mappingFfiErrors(() => _writer.close()); + Future close() => _closeWriter(_writer.close, _ensureOpen); } class _FfiByteStreamWriter implements StreamWriter { @@ -644,5 +659,5 @@ class _FfiByteStreamWriter implements StreamWriter { } @override - Future close() => mappingFfiErrors(() => _writer.close()); + Future close() => _closeWriter(_writer.close, _ensureOpen); } From 67698d724d07791fb3bbaac6bb1322c367864419 Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:40:46 +0800 Subject: [PATCH 3/7] Add the change entry --- .changes/data-stream-send-after-delivery | 1 + 1 file changed, 1 insertion(+) create mode 100644 .changes/data-stream-send-after-delivery diff --git a/.changes/data-stream-send-after-delivery b/.changes/data-stream-send-after-delivery new file mode 100644 index 000000000..ef7ba93b0 --- /dev/null +++ b/.changes/data-stream-send-after-delivery @@ -0,0 +1 @@ +patch type="fixed" "A data stream send completes once its packets are sent, so text sent right before a disconnect arrives, a failed send throws, and a large send no longer buffers the whole payload in memory" \ No newline at end of file From 49bf78752dd5af6224032853b2d00b3f555ae857 Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Wed, 18 Nov 2026 02:29:09 +0800 Subject: [PATCH 4/7] Say what the change entry can promise Text sent right before a disconnect still depends on the data channel draining before the connection closes, which this change does not cover. --- .changes/data-stream-send-after-delivery | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/data-stream-send-after-delivery b/.changes/data-stream-send-after-delivery index ef7ba93b0..618715c3d 100644 --- a/.changes/data-stream-send-after-delivery +++ b/.changes/data-stream-send-after-delivery @@ -1 +1 @@ -patch type="fixed" "A data stream send completes once its packets are sent, so text sent right before a disconnect arrives, a failed send throws, and a large send no longer buffers the whole payload in memory" \ No newline at end of file +patch type="fixed" "A data stream send completes once its packets are handed to the data channel, so a failed send throws and a large send no longer buffers the whole payload in memory" \ No newline at end of file From 44436fe67df676f1b51f083c88655cd2916880e1 Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Wed, 18 Nov 2026 05:16:44 +0800 Subject: [PATCH 5/7] Follow the batch record of the polled queue A batch now carries its packets as a field and the outcome as a separate object to report on. --- lib/src/data_stream/data_streams_native.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/data_stream/data_streams_native.dart b/lib/src/data_stream/data_streams_native.dart index e5ef00113..ac3d904d9 100644 --- a/lib/src/data_stream/data_streams_native.dart +++ b/lib/src/data_stream/data_streams_native.dart @@ -272,12 +272,12 @@ class NativeDataStreams implements DataStreams { final batch = await _outgoingPackets.nextBatch(); if (batch == null) break; // closed or shutting down try { - await _send(batch.packets()); - batch.complete(); + await _send(batch.packets); + batch.outcome.complete(); } catch (e) { - batch.fail(reason: '$e'); + batch.outcome.fail(reason: '$e'); } finally { - batch.dispose(); + batch.outcome.dispose(); } } } catch (e) { From 9c5c4a19c6d1a01415bd30cd538ae5bc049d1894 Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Wed, 18 Nov 2026 05:30:59 +0800 Subject: [PATCH 6/7] Follow the rename of the batch's ack --- lib/src/data_stream/data_streams_native.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/data_stream/data_streams_native.dart b/lib/src/data_stream/data_streams_native.dart index ac3d904d9..3705b2528 100644 --- a/lib/src/data_stream/data_streams_native.dart +++ b/lib/src/data_stream/data_streams_native.dart @@ -273,11 +273,11 @@ class NativeDataStreams implements DataStreams { if (batch == null) break; // closed or shutting down try { await _send(batch.packets); - batch.outcome.complete(); + batch.ack.complete(); } catch (e) { - batch.outcome.fail(reason: '$e'); + batch.ack.fail(reason: '$e'); } finally { - batch.outcome.dispose(); + batch.ack.dispose(); } } } catch (e) { From 789272b698f70eda686be57572f3acfc290f5ede Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:41:32 +0800 Subject: [PATCH 7/7] Require livekit_ffi 0.15.0 It has the batch acknowledgement the outgoing pump now uses, and bindings that free the native buffers of byte and string payloads. --- .changes/bump-livekit-ffi-0.15.0 | 1 + pubspec.lock | 4 ++-- pubspec.yaml | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 .changes/bump-livekit-ffi-0.15.0 diff --git a/.changes/bump-livekit-ffi-0.15.0 b/.changes/bump-livekit-ffi-0.15.0 new file mode 100644 index 000000000..533af43fe --- /dev/null +++ b/.changes/bump-livekit-ffi-0.15.0 @@ -0,0 +1 @@ +patch type="changed" "Require livekit_ffi 0.15.0, whose Dart bindings no longer leak the byte and string payloads that cross to the Rust core" \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index be950fca7..8a33ee468 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -444,10 +444,10 @@ packages: dependency: "direct main" description: name: livekit_ffi - sha256: "1bda89c8affc60752a78c880f42f8b35db0db5cf7455ba86274ff34ca196eabe" + sha256: c107a4c63d5c2a37f878bf9af7582238e12dec69f3041c80193dd6a2ccb2c694 url: "https://pub.dev" source: hosted - version: "0.13.4" + version: "0.15.0" logger: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 43fd1136c..1ef383f47 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -59,7 +59,7 @@ dependencies: # Rust core (livekit-ffi), delivered as a bundled cdylib via Native Assets. # Native platforms only, see lib/src/ffi/. - livekit_ffi: ^0.13.4 + livekit_ffi: ^0.15.0 dev_dependencies: flutter_test: