From 2eaf998e8a31ab30c89fd42cd1c75a4d7ce0cdb0 Mon Sep 17 00:00:00 2001 From: Marcelo Bottoni Date: Tue, 3 Feb 2026 17:07:18 -0300 Subject: [PATCH] fix: prevent adding events to closed StreamController Add isClosed checks to DataStreamController's write() and error() methods to prevent "Bad state: Cannot add event after closing" exception. This error occurs when a participant disconnects and validateParticipantHasNoActiveDataStreams() tries to send errors to stream controllers that have already been closed. Co-Authored-By: Claude Opus 4.5 --- lib/src/types/data_stream.dart | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/src/types/data_stream.dart b/lib/src/types/data_stream.dart index a5963ff54..5bad84795 100644 --- a/lib/src/types/data_stream.dart +++ b/lib/src/types/data_stream.dart @@ -161,9 +161,17 @@ class DataStreamController { Future close() => streamController.close(); - void write(T chunk) => streamController.add(chunk); + void write(T chunk) { + if (!streamController.isClosed) { + streamController.add(chunk); + } + } - void error(DataStreamError error) => streamController.addError(error); + void error(DataStreamError error) { + if (!streamController.isClosed) { + streamController.addError(error); + } + } } class ByteStreamInfo extends BaseStreamInfo {