Skip to content

fix(client): capture sessionId before await in updateLocalStreamState#2229

Merged
jdimovska merged 2 commits intomainfrom
race-condition-capture
May 7, 2026
Merged

fix(client): capture sessionId before await in updateLocalStreamState#2229
jdimovska merged 2 commits intomainfrom
race-condition-capture

Conversation

@jdimovska
Copy link
Copy Markdown
Contributor

@jdimovska jdimovska commented May 7, 2026

💡 Overview

Fixes a TOCTOU race in Call.updateLocalStreamState: this.sfuClient.sessionId is read after await this.notifyTrackMuteState(...), and a reconnect/leave during the await clears this.sfuClient, throwing on the destructure. Also drops the signal.aborted check in retryable so in-flight SFU RPCs can finish after the WebSocket is closed

📝 Implementation notes

🎫 Ticket: https://linear.app/stream/issue/XYZ-123

📑 Docs: https://github.com/GetStream/docs-content/pull/

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved robustness when muting/unmuting local tracks so updates skip if the session changes mid-operation.
    • Request cancellation now triggers immediately instead of waiting for RPC failures.
  • Tests

    • Added coverage ensuring publishing completes cleanly if the client state is cleared while an operation is in-flight.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 7, 2026

⚠️ No Changeset found

Latest commit: d3815c5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c2ecb3e5-db09-4b3b-9a82-cc26bce7c7be

📥 Commits

Reviewing files that changed from the base of the PR and between 2b8df20 and d3815c5.

📒 Files selected for processing (2)
  • packages/client/src/Call.ts
  • packages/client/src/__tests__/Call.publishing.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/client/src/tests/Call.publishing.test.ts
  • packages/client/src/Call.ts

📝 Walkthrough

Walkthrough

The PR adds a pre-RPC abort check in the retryable helper, snapshots SFU sessionId before notifying mute state in Call.updateLocalStreamState and guards subsequent participant updates, and adds a test ensuring publish doesn't throw if sfuClient is cleared while an in-flight mute-state RPC completes.

Changes

Abort Signal Handling and In-Flight RPC Safety

Layer / File(s) Summary
RPC Abort Signal Handling
packages/client/src/rpc/retryable.ts
retryable now checks signal?.aborted inside the retry loop before invoking the RPC and throws using signal.reason immediately, causing retries to abort fast when the signal is already aborted.
Session State Update Ordering
packages/client/src/Call.ts
updateLocalStreamState snapshots sessionId from this.sfuClient, returns early if absent, awaits notifyTrackMuteState, and skips participant-state updates if the current SFU session no longer matches the snapshot.
Integration Test Coverage
packages/client/src/__tests__/Call.publishing.test.ts
New test ensures call.publish resolves without throwing when sfuClient is set to undefined while sfuClient.updateMuteStates is blocked and then resumed.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hop the retry loop, ear to the air,
If Abort sings early, I stop with care.
I tuck the sessionId safe before the call,
Let muted tracks whisper, then check them all.
Publish stands steady, even if SFU falls.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: capturing sessionId before await in updateLocalStreamState to fix a race condition.
Description check ✅ Passed The description includes the required Overview and Implementation notes sections, clearly explaining the race condition fix and the signal.aborted change, with referenced ticket and docs links.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 race-condition-capture

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.

@jdimovska jdimovska requested a review from oliverlaz May 7, 2026 11:28
@jdimovska jdimovska merged commit e48ec08 into main May 7, 2026
17 checks passed
@jdimovska jdimovska deleted the race-condition-capture branch May 7, 2026 11:41
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