Skip to content

fix(preview): close streams when their session is invalidated - #802

Merged
Jacksondr5 merged 5 commits into
j5/mainfrom
fix/798-preview-revocation
Oct 31, 2026
Merged

Jacksondr5 merged 5 commits into
j5/mainfrom
fix/798-preview-revocation

Conversation

@BastiHu

@BastiHu BastiHu commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

An established preview WebSocket keeps streaming and accepting input after its session is revoked, including when its collaborator is demoted. The ordinary browser reload masks this for normal clients, but a client retaining the socket keeps access. Closes #798.

What changed

I race the entire scoped preview viewer lifecycle against SessionStore.awaitInvalidation, following the existing RPC socket behavior. Invalidation interrupts the stream and input fibers and runs the viewer finalizers. Existing frame backpressure and control handling remain intact. An action already dispatched to Playwright cannot be rolled back.

Evidence

At e77cd431d5d74200706d9d8d0b0d40fcaf645bca, based on j5/main at 5e45a791e238dd5357c17b188a9cdda1b5700afa, using pinned Node 26.12.1 and pnpm 12.14.0:

  • vp run check:changed passed: formatting, lint, both Knip checks, generated GraphQL checks, typechecks for all three affected packages, and 13 preview-stream tests. The stream suite includes two deterministic established-socket revocation cases.
  • vp test run apps/server/src/auth/SessionStore.test.ts: 16 tests passed, for 29 focused tests total.
  • git diff --check passed and the working tree is clean.
  • Main merged cleanly and brings the separate intermittent browser-test fix from test: fix the two intermittent J5 CI tests #813. The net PR diff remains the four intended preview-stream and documentation files. Preview-stream and SessionStore source/tests are unchanged from the prior head 8456ca762.

Not verified: full CI on this new head, browser tests locally for this merge, desktop/mobile, relay/tailnet, expiry or restart live scenarios, post-invalidation owner click receipt, and #799's separate preview-initialization problem. No UI source changed, so there are no visual changes requiring screenshots.

Live testing

PASS on an isolated real server, real SessionStore and real server Chromium at d3443282eaaaaf9545bbce2ad22b04711667edbd, base 6a6aa43d08034bcd3bb69f2364903c061e7c8137. The current head e77cd431d5d74200706d9d8d0b0d40fcaf645bca includes the later fork-table/D33 documentation commit and a merge of j5/main. Preview-stream and SessionStore source/tests remain unchanged; focused automated validation was repeated, but this live flow was not rerun for the merge.

An independent tester retained Node preview WebSockets across each event, avoiding the ordinary client's automatic reload:

  • Before revocation, the collaborator received frames, took control and clicked a local counter button; the counter recorded the click.
  • Owner session revocation closed the collaborator socket with code 1001. Its frame count stayed at 23; the owner's stream continued from 29 to 41 frames and regained control.
  • Collaborator-to-Viewer demotion through j5.people.setLevel independently closed the retained socket with code 1001. Its frame count stayed at 25; the owner's stream continued from 30 to 42 and regained control.
  • Both old cookies failed stream reopening with HTTP 401 and reported authenticated: false on the session endpoint.

Observation windows were finite. Owner control acquisition and continuing frames were observed; an owner click after invalidation was not separately counted. No provider turn was sent. The disposable test servers and sockets were stopped after verification.

Upstream impact

apps/server/src/preview/ServerBrowserStream.ts and its test are recorded in FORK.md, section “Preview stream session invalidation (#798)” and the file table. D33 in docs/j5/product/upstream.md now states the explicit preview invalidation behavior. The issue requests this shared authorization fix; offering it upstream is a separate publication step. #799 is outside this change.

Merge Danger

Door: two-way; a revert restores the prior stream lifecycle.

Blast Radius: authenticated preview-stream lifecycle and its scoped cleanup.

After merge: consider offering the shared fix upstream separately; no upstream PR is created here.

Checklist

  • One concern: the description has no "also"
  • Tests cover the changed behavior (backend changes ship with focused tests)
  • UI changes: before/after screenshots above, and a video for motion or interaction — no UI change
  • Live testing: the section above says what ran live and what did not
  • Upstream-owned files: each one is recorded in FORK.md (case text and file-table row) in this PR
  • Upstream product: any change to what upstream's product does has a human decision linked above and a register entry in docs/j5/product/upstream.md — An open preview stream outlives its session's revocation #798 and D33
  • Surfaces: entry points, clients, providers, contracts, reverse states, connection modes (see AGENTS.md) — real server/Chromium revocation and demotion verified; wider surfaces listed as unverified
  • Docs: definitions under docs/j5/product/ and user docs rewritten where this changes them — existing D33 implementation statement corrected

@vercel

vercel Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
j5-code Ready Ready Preview Oct 31, 2026 9:44pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Warning

Review limit reached

  • Run on-demand review

This review includes 4 billable files and costs up to $1.00.

Or wait 54 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 074eed5 and f396760.

Files selected for processing: 4
  • FORK.md
  • apps/server/src/preview/ServerBrowserStream.test.ts
  • apps/server/src/preview/ServerBrowserStream.ts
  • docs/j5/product/upstream.md

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 5e45a79 and 074eed5.

Files selected for processing: 4
  • FORK.md
  • apps/server/src/preview/ServerBrowserStream.test.ts
  • apps/server/src/preview/ServerBrowserStream.ts
  • docs/j5/product/upstream.md

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

The preview WebSocket handler now races an established viewer session against session invalidation. Invalidation ends the stream and interrupts active input. Tests cover the viewer lifecycle, and documentation records the behavior.

Changes: Preview stream invalidation

Layer / File(s) Summary
Race preview streams against session invalidation
apps/server/src/preview/ServerBrowserStream.ts, apps/server/src/preview/ServerBrowserStream.test.ts, FORK.md, docs/j5/product/upstream.md
The handler observes authenticated-session invalidation during an established stream. Tests check viewer detachment, socket closure, and active input interruption. Documentation records the stream behavior and its test coverage.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant ViewerSocket
  participant ServerBrowserStream
  participant SessionStore
  participant InputFiber
  ViewerSocket->>ServerBrowserStream: Open authenticated preview stream
  ServerBrowserStream->>SessionStore: Await invalidation for the session
  SessionStore-->>ServerBrowserStream: Signal session invalidation
  ServerBrowserStream-->>ViewerSocket: Complete stream with an empty response
  ServerBrowserStream-->>InputFiber: Interrupt active input
Loading

Possibly related issues

  • Jacksondr5/j5code issue 276: It tracks revoked or expired preview sessions continuing to receive frames and send input, which this change addresses.

Possibly related PRs

  • Jacksondr5/j5code#797: It reports established preview streams remaining usable after a collaborator is demoted; this change ends the stream when its session is invalidated.

Suggested reviewers: juliusmarminge, maria-rcks

Merge Risk: Minimal · previous review 074ee

No merge-blocking risk is established from the supplied review evidence.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: closing preview streams when their session is invalidated.
Description check Passed The description is detailed and follows the required structure. It explains the problem, implementation, testing evidence, live testing limits, upstream impact, merge risk, and checklist status. The U…
Linked Issues check Passed The changes satisfy the coding requirements in #798. apps/server/src/preview/ServerBrowserStream.ts retrieves SessionStore and races the scoped viewer lifecycle against awaitInvalidation. This i…
Out of Scope Changes check Passed The four changed files stay within #798. The source change implements session invalidation. The test verifies the required behavior. FORK.md records the upstream-owned file. `docs/j5/product/upstrea…
Full details: Description check

Explanation

The description is detailed and follows the required structure. It explains the problem, implementation, testing evidence, live testing limits, upstream impact, merge risk, and checklist status. The UI section is omitted appropriately because no UI changed, and the unchecked Surfaces item is explained by the listed unverified scenarios.

Full details: Linked Issues check

Explanation

The changes satisfy the coding requirements in #798. apps/server/src/preview/ServerBrowserStream.ts retrieves SessionStore and races the scoped viewer lifecycle against awaitInvalidation. This interrupts stream and input fibers and runs scope finalizers. The test adds established-socket coverage for read-only and operate sessions, including socket closure, input interruption, detachment, and rejection of later frames. FORK.md records the upstream-owned change. The linked objective treats offering the fix upstream as a separate publication step, so no upstream pull request is required here.

Full details: Out of Scope Changes check

Explanation

The four changed files stay within #798. The source change implements session invalidation. The test verifies the required behavior. FORK.md records the upstream-owned file. docs/j5/product/upstream.md documents the resulting product behavior. No unrelated implementation or product change is shown.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions github-actions Bot added the vouch:trusted Author is trusted by repo permissions or the VOUCHED list. label Oct 30, 2026
@github-actions github-actions Bot added the size:S 10-29 effective changed lines (test files excluded in mixed PRs). label Oct 30, 2026

@Jacksondr5 Jacksondr5 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The race wraps the whole scoped attach/upgrade/viewer lifecycle, so invalidation interrupts both the stream and the input fibers and releases the viewer. awaitInvalidation subscribes before it reads, so a revocation during the upgrade isn't lost. The tests wait on Deferred milestones, not timers. FORK.md records both upstream files. No code findings. Jackson reviews the D33 wording separately.

Review panel: Claude Opus 5.5 (lead) and GPT-6 Astra, for @Jacksondr5.

@Jacksondr5
Jacksondr5 merged commit 26aa08f into j5/main Oct 31, 2026
32 checks passed
@Jacksondr5
Jacksondr5 deleted the fix/798-preview-revocation branch October 31, 2026 21:58

This branch was successfully deployed

1 active deployment
Preview — f396760c Deployed Oct 31, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S 10-29 effective changed lines (test files excluded in mixed PRs). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An open preview stream outlives its session's revocation

2 participants