Skip to content

fix(rtc): drop late SFU subscriber offers when peer connection is closed#240

Merged
aliev merged 1 commit into
mainfrom
fix/drop-late-subscriber-offer
May 11, 2026
Merged

fix(rtc): drop late SFU subscriber offers when peer connection is closed#240
aliev merged 1 commit into
mainfrom
fix/drop-late-subscriber-offer

Conversation

@aliev
Copy link
Copy Markdown
Member

@aliev aliev commented May 11, 2026

Why

_on_subscriber_offer unconditionally calls setRemoteDescription on subscriber_pc. If an offer arrives after the connection has been torn down (slow asyncio loop under load, SFU sending a late renegotiation), aiortc raises InvalidStateError: Cannot handle offer in signaling state "closed". The exception then propagates through the pyee error path and kills the surrounding session.

Reproduces reliably under load: when several concurrent agent sessions run on one pod and the asyncio loop falls behind, session cleanup and a late SFU offer race. We've been observing this pattern on a production deploy that runs multiple agent sessions per pod — each unhandled error visibly drops sessions for users mid-call.

Changes

  • In _on_subscriber_offer, guard against a missing or closed subscriber_pc: drop the offer and log at debug level. Nothing to negotiate with a closed connection.

Summary by CodeRabbit

  • Bug Fixes
    • Improved video connection stability by ignoring late or invalid subscriber offers when a session is no longer active.
    • Such offers are now safely dropped and logged at debug level, preventing unnecessary negotiation attempts and avoiding unexpected connection errors or terminations.
    • Reduces spurious error propagation for closed or missing subscriber sessions.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b71a7676-6c81-466c-ace1-04f9f3509d72

📥 Commits

Reviewing files that changed from the base of the PR and between e19c479 and b1442bf.

📒 Files selected for processing (1)
  • getstream/video/rtc/connection_manager.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • getstream/video/rtc/connection_manager.py

📝 Walkthrough

Walkthrough

A single defensive guard is added to ConnectionManager._on_subscriber_offer that detects and drops subscriber SDP offers when the peer connection is unavailable or already closed, preventing InvalidStateError from propagating through the session.

Changes

Subscriber SDP Offer Late-Arrival Guard

Layer / File(s) Summary
Offer Reception Guard
getstream/video/rtc/connection_manager.py
After acquiring the subscriber negotiation lock, an early-return check drops offers if subscriber_pc is None or signalingState == "closed". A debug log message records the dropped offer.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through late-arriving streams,
Checks if the connection still dreams—
If closed it be, the offer falls,
Silent logs where echo stalls,
The session sleeps and error calms. 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding a guard to drop late SFU subscriber offers when the peer connection is closed, which directly matches the code change and PR objectives.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/drop-late-subscriber-offer

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@aliev aliev marked this pull request as ready for review May 11, 2026 12:20
@aliev aliev force-pushed the fix/drop-late-subscriber-offer branch from e19c479 to f43c97d Compare May 11, 2026 12:21
`_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.
@aliev aliev force-pushed the fix/drop-late-subscriber-offer branch from f43c97d to b1442bf Compare May 11, 2026 13:45
@aliev aliev merged commit 8c880c5 into main May 11, 2026
30 of 31 checks passed
@aliev aliev deleted the fix/drop-late-subscriber-offer branch May 11, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants