Skip to content

Commit 361f1ea

Browse files
authored
check data stream closed (#369)
1 parent 32174ed commit 361f1ea

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

livekit-rtc/livekit/rtc/data_stream.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def __init__(
166166
self._next_chunk_index: int = 0
167167
self._destination_identities = destination_identities
168168
self._sender_identity = sender_identity or self._local_participant.identity
169+
self._closed = False
169170

170171
async def _send_header(self):
171172
req = proto_ffi.FfiRequest(
@@ -191,6 +192,8 @@ async def _send_header(self):
191192
raise ConnectionError(cb.send_stream_header.error)
192193

193194
async def _send_chunk(self, chunk: proto_DataStream.Chunk):
195+
if self._closed:
196+
raise RuntimeError(f"Cannot send chunk after stream is closed: {chunk}")
194197
req = proto_ffi.FfiRequest(
195198
send_stream_chunk=proto_room.SendStreamChunkRequest(
196199
chunk=chunk,
@@ -238,6 +241,9 @@ async def _send_trailer(self, trailer: proto_DataStream.Trailer):
238241
async def aclose(
239242
self, *, reason: str = "", attributes: Optional[Dict[str, str]] = None
240243
):
244+
if self._closed:
245+
raise RuntimeError("Stream already closed")
246+
self._closed = True
241247
await self._send_trailer(
242248
trailer=proto_DataStream.Trailer(
243249
stream_id=self._header.stream_id, reason=reason, attributes=attributes

0 commit comments

Comments
 (0)