diff --git a/.changes/data-stream-error-livekit-exception b/.changes/data-stream-error-livekit-exception new file mode 100644 index 000000000..8847e9fd8 --- /dev/null +++ b/.changes/data-stream-error-livekit-exception @@ -0,0 +1 @@ +patch type="fixed" "`DataStreamError` is now a `LiveKitException`, so `on LiveKitException` also catches data stream failures, such as a write cut off by disposing the room" diff --git a/lib/src/data_stream/errors.dart b/lib/src/data_stream/errors.dart index afb534f9b..bafc155df 100644 --- a/lib/src/data_stream/errors.dart +++ b/lib/src/data_stream/errors.dart @@ -1,8 +1,12 @@ +import '../exceptions.dart'; + enum DataStreamErrorReason { // Unable to open a stream with the same ID more than once. AlreadyOpened, - // Stream closed abnormally by remote participant. + // Stream ended before completing: the sender disconnected mid-stream, the local room was + // disconnected or disposed, or sending or receiving failed. Also used for failures that map to no + // more specific reason. AbnormalEnd, // Incoming chunk data could not be decoded. @@ -24,8 +28,9 @@ enum DataStreamErrorReason { AlreadyClosed, } -class DataStreamError implements Exception { +class DataStreamError implements LiveKitException { final DataStreamErrorReason reason; + @override final String message; DataStreamError({required this.reason, required this.message}); diff --git a/test/core/data_stream_v2_test.dart b/test/core/data_stream_v2_test.dart index 3a193aed8..829bf8bc5 100644 --- a/test/core/data_stream_v2_test.dart +++ b/test/core/data_stream_v2_test.dart @@ -582,6 +582,24 @@ void main() { expect(streamPackets().where((p) => !p.hasStreamTrailer()), isEmpty); }); + test('writes cut off by disposing the room fail with a LiveKitException', () async { + await container.simulateRemoteParticipantJoin('alice', clientProtocol: 2); + final writer = await room.localParticipant!.streamBytes(StreamBytesOptions(topic: 'cut-off')); + + // Not awaited: the room is disposed while these are still in the core, which fails them with + // a DataStreamError. Apps catch the SDK's errors as LiveKitException, so it has to be one. + final errors = []; + final writes = [ + for (var i = 0; i < 50; i++) + writer.write(Uint8List(30 * 1024)).then((_) {}, onError: (Object e) => errors.add(e)), + ]; + await room.dispose(); + await Future.wait(writes); + + expect(errors, isNotEmpty, reason: 'no write was cut off, so there is nothing to check'); + expect(errors, everyElement(isA())); + }); + test('a stream packet still on its way in does not open a stream', () async { final ds = room.dataStreams as NativeDataStreams; var opened = 0;