Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/bump-livekit-ffi-0.15.0
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions .changes/data-stream-send-after-delivery
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 31 additions & 14 deletions lib/src/data_stream/data_streams_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<void> _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) {
Expand All @@ -282,20 +287,17 @@ class NativeDataStreams implements DataStreams {
}
}

Future<void> _send(Uint8List encoded) async {
Future<void> _send(List<Uint8List> 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.
await room.engine.sendDataPacket(
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');
}
}

Expand Down Expand Up @@ -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<void> _closeWriter(Future<void> 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<String> {
_FfiTextStreamWriter(this._writer, this._ensureOpen);
Expand All @@ -626,7 +643,7 @@ class _FfiTextStreamWriter implements StreamWriter<String> {
}

@override
Future<void> close() => mappingFfiErrors(() => _writer.close());
Future<void> close() => _closeWriter(_writer.close, _ensureOpen);
}

class _FfiByteStreamWriter implements StreamWriter<Uint8List> {
Expand All @@ -642,5 +659,5 @@ class _FfiByteStreamWriter implements StreamWriter<Uint8List> {
}

@override
Future<void> close() => mappingFfiErrors(() => _writer.close());
Future<void> close() => _closeWriter(_writer.close, _ensureOpen);
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading