Skip to content

Stop SSE streams leaking on cancel, keep network capture to its own session, and read code fences like GitHub in the docs link checker - #4193

Closed
ramybarsoum wants to merge 1 commit into
garrytan:mainfrom
ramybarsoum:fix/state-transition-races
Closed

ramybarsoum wants to merge 1 commit into
garrytan:mainfrom
ramybarsoum:fix/state-transition-races

Conversation

@ramybarsoum

@ramybarsoum ramybarsoum commented Nov 9, 2026 •

Copy link
Copy Markdown

Live activity and inspector streams now release their subscriptions as soon as a client goes away, network capture keeps each session's responses to itself, and the docs link checker reads code fences the way GitHub renders them. That keeps the browser sidebar from leaking connections and keeps the docs link gate from reporting false broken links.

Why (in your own words)

Three places in gstack keep running after the thing they belong to has ended.

  • Live activity and inspector streams leak. If the sidebar closes or the client cancels a stream, createSseEndpoint only cleaned up on the request's abort signal. A cancelled stream kept its subscription and heartbeat. A request that was already aborted still subscribed. A subscriber whose unsubscribe threw stopped the stream from closing.
  • Network capture mixes sessions. A response whose body was still loading when you ran network --capture stop, or started a new capture, was pushed into the buffer afterwards.
  • The docs link checker misreads fences. It closed a code block on a shorter fence or on a fence followed by text, and opened one on a line of inline code that starts with backticks. It also gave repeated headings anchors that can collide (# A, # A, # A-1), and reported a file named like ..notes.md as outside the repository.

The fix ties each listener and subscription to its own session and cleans up on every way a stream can end. The link checker now follows CommonMark and github-slugger.

Live evidence

The new regression tests against origin/main's source for lib/doc-links.ts, browse/src/sse-helpers.ts and browse/src/network-capture.ts, and then on this branch:

$ bun test test/doc-links-regressions.test.ts browse/test/state-machine-regressions.test.ts   # main's source
(fail) heading IDs remain unique when a natural suffix collides with a duplicate
(fail) shorter fences and backticks followed by text do not close a code block
(fail) a backtick line with backticks after the marker is inline code, not a fence
(fail) a repository filename beginning with two dots is not an escape
(fail) pending response cannot cross stop
(fail) pending response cannot cross restart
(fail) pending response cannot cross clear
(fail) stale listener cannot start work after stop; invalid filter preserves active session
(fail) SSE cancellation releases subscription immediately and once
(fail) already aborted SSE request never subscribes or replays
(fail) abort during subscribe releases returned disposer exactly once
 0 pass
 11 fail

$ bun test test/doc-links-regressions.test.ts browse/test/state-machine-regressions.test.ts   # this branch
 11 pass
 0 fail

I also ran brokenLinks over every tracked .md file in the repo. origin/main reports 203 broken links and this branch reports 197. The branch adds none, and the 6 it no longer reports are false positives in docs/troubleshooting.md anchors.

Scope

  • Changed:
    • browse/src/sse-helpers.ts
    • browse/src/network-capture.ts
    • lib/doc-links.ts
    • Two new test files
    • changelog.d/fix-state-transition-races.md
  • Verified live by:
    • The before/after test runs above.
    • bun run typecheck, which is clean.
    • Every existing test that imports the changed modules: browse/test/sse-helpers.test.ts, browse/test/data-platform.test.ts, browse/test/server-sanitize-surrogates.test.ts, test/docs-only.test.ts and test/docs-only-workflow.test.ts. 103 pass. The one failure is test/docs-only.test.ts "a wrong-case docs tree", which also fails on origin/main on a case-insensitive macOS disk.
    • The repo-wide broken-link comparison above.
  • Behavior change to note: responses still loading when --capture stop runs are now dropped instead of arriving late. The count stop prints now matches what --export writes.
  • Defensive only: clearCapture has no production caller today. Its revision counter only protects the path the tests use.
  • Did NOT test:
    • A real Chromium session with the sidebar open. The SSE and capture paths are covered by unit tests with a fake request and response.
    • The paid eval tiers, which need maintainer keys.

Liveness proof (required for external contributors)

Pending. @ramybarsoum will attach the GSTACK PR screenshot before marking this ready for review.

Checklist

  • Liveness screenshot attached (GSTACK PR typed live into a real surface) or PR author is @garrytan (owner exemption)
  • This is not a generated-file-only diff (I edited the source/template and regenerated)
  • No ETHOS.md edits, and no changes to voice / founder perspective / YC references
  • New public command / external service / host adapter has an accepted issue linked (or N/A)
  • Linked issue or reproduction: # (reproduction in Live evidence above)

…ion, and read fences like GitHub in the docs link checker

createSseEndpoint now cleans up when the client cancels the stream, when the
request is already aborted before start, when abort fires during subscribe, and
when subscribe throws. A throwing unsubscribe no longer stops the stream from
closing.

Network capture ties each response listener to its session, so a response that
was still loading at stop, restart or clear is not pushed afterwards.

lib/doc-links.ts closes a fence only on a bare line with the same marker that
is at least as long, treats a backtick line with backticks after the marker as
inline code, gives repeated headings github-slugger's suffixes, and no longer
reports a file named like ..notes.md as outside the repository. Across the
repository's tracked Markdown this reports 6 fewer broken links than main and
no new ones.
@trunk-io

trunk-io Bot commented Nov 9, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@ramybarsoum ramybarsoum changed the title fix(browse): release SSE subscriptions on cancel, scope network capture to its session, read fences like GitHub in the docs link checker Stop SSE streams leaking on cancel, keep network capture to its own session, and read code fences like GitHub in the docs link checker Nov 9, 2026
@capy-ai

capy-ai Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Thanks @ramybarsoum for the careful tests. We're closing this one. On main the SSE helper already tears a dead stream down on the next heartbeat or failed write, so a cancelled stream is released within one heartbeat interval instead of leaking for the session. The capture and link-checker cases don't change how gstack's workflows run today, so we'd rather not take a three-area change for them. If you have a case where a stream stays subscribed past the heartbeat, or a real doc the link gate misreports, please reply with it and we'll reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant