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/.changes/data-stream-send-after-delivery b/.changes/data-stream-send-after-delivery new file mode 100644 index 000000000..618715c3d --- /dev/null +++ b/.changes/data-stream-send-after-delivery @@ -0,0 +1 @@ +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 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..3705b2528 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.ack.complete(); + } catch (e) { + batch.ack.fail(reason: '$e'); + } finally { + batch.ack.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'); } } @@ -612,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); @@ -626,7 +643,7 @@ class _FfiTextStreamWriter implements StreamWriter { } @override - Future close() => mappingFfiErrors(() => _writer.close()); + Future close() => _closeWriter(_writer.close, _ensureOpen); } class _FfiByteStreamWriter implements StreamWriter { @@ -642,5 +659,5 @@ class _FfiByteStreamWriter implements StreamWriter { } @override - Future close() => mappingFfiErrors(() => _writer.close()); + Future close() => _closeWriter(_writer.close, _ensureOpen); } 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: