Skip to content

Commit 8c880c5

Browse files
authored
fix(rtc): drop late SFU subscriber offers when peer connection is closed (#240)
`_on_subscriber_offer` unconditionally called `setRemoteDescription` on `subscriber_pc`, so an offer that arrived after the connection was torn down raised `InvalidStateError: Cannot handle offer in signaling state "closed"`. The exception propagated through the pyee error path and killed the session. Reproduces reliably under load: when the asyncio loop falls behind (e.g. several concurrent agent sessions on one pod), session cleanup and a late SFU renegotiation race, the offer arrives at a closed peer connection, and the resulting exception drops the agent's remaining sessions. Guard the handler: if `subscriber_pc` is missing or `signalingState` is `"closed"`, drop the offer and log at debug level — there is nothing to negotiate with a closed connection.
1 parent 2b1e111 commit 8c880c5

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

getstream/video/rtc/connection_manager.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ async def _on_ice_trickle(self, event):
198198
async def _on_subscriber_offer(self, event: events_pb2.SubscriberOffer):
199199
logger.info("Subscriber offer received")
200200

201+
# Offers can arrive after the subscriber peer connection has been
202+
# torn down (slow asyncio loop under load, SFU sending a late
203+
# renegotiation). `setRemoteDescription` would raise
204+
# `InvalidStateError: Cannot handle offer in signaling state "closed"`
205+
# and the exception propagates through the pyee error path, killing
206+
# the session. Drop the offer instead — there is nothing to
207+
# negotiate with a closed connection.
208+
if self.subscriber_pc is None or self.subscriber_pc.signalingState == "closed":
209+
logger.debug("Subscriber offer arrived after PC closed; dropping")
210+
return
211+
201212
with telemetry.start_as_current_span("rtc.on_subscriber_offer") as span:
202213
await self.subscriber_negotiation_lock.acquire()
203214

0 commit comments

Comments
 (0)