Skip to content

fix: close sse stream on abort#1812

Merged
Priyanshu-byte-coder merged 3 commits into
Priyanshu-byte-coder:mainfrom
Divv1524:fix/issue-1608-sse-stream-cleanup
Jun 3, 2026
Merged

fix: close sse stream on abort#1812
Priyanshu-byte-coder merged 3 commits into
Priyanshu-byte-coder:mainfrom
Divv1524:fix/issue-1608-sse-stream-cleanup

Conversation

@Divv1524
Copy link
Copy Markdown
Contributor

@Divv1524 Divv1524 commented Jun 1, 2026

Summary

Fixed the SSE stream lifecycle in /api/stream so server-side resources are cleaned up reliably when clients disconnect, streams are canceled, or connections become stale.

Closes #1608


Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactor / code cleanup

Changes Made

  • Centralized SSE cleanup into one idempotent close path
  • Safely closes the ReadableStream controller on client abort
  • Added guarded controller.enqueue() handling to prevent writes to closed streams
  • Added a 5-minute maximum SSE connection duration
  • Added cleanup support for canceled stream readers and already-aborted requests
  • Added regression tests for SSE cleanup behavior

How to Test

Steps for the reviewer to verify this works:

  1. Run npm run lint
  2. Run npm run type-check
  3. Run npm run build
  4. Run vitest run test/sse-stream-route.test.ts
  5. Confirm SSE stream connections release their active connection slot after abort, cancel, or timeout

Screenshots (if UI change)

N/A


Checklist

  • Linked issue in summary
  • npm run lint passes locally
  • No TypeScript errors (npm run type-check)
  • Self-reviewed the diff
  • Added/updated tests if applicable

Accessibility Checklist

  • Proper keyboard navigation tested
  • Responsive UI verified
  • Accessibility labels added where needed

Additional Notes

Full npm test was also run. The focused SSE test suite passed, but the full suite currently has unrelated pre-existing failures outside this change.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

@Divv1524 is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:testing GSSoC type bonus: tests (+10 pts) labels Jun 1, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

GSSoC Label Checklist 🏷️

@Priyanshu-byte-coder — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Thanks for your first PR on DevTrack! 🎉

A maintainer will review it within 48 hours. While you wait:

  • Make sure CI is passing (type-check + lint)
  • Double-check the PR description is filled out and the issue is linked
  • Feel free to ask questions in Discussions if you need help

If you find DevTrack useful, a ⭐ star on the repo is always appreciated — it helps the project grow and attract more contributors!

@Priyanshu-byte-coder Priyanshu-byte-coder added level2 GSSoC Level 2 - Medium complexity (25 points) quality:clean GSSoC: Clean quality multiplier (×1.2) labels Jun 2, 2026
@Priyanshu-byte-coder
Copy link
Copy Markdown
Owner

Please rebase on main to resolve conflicts. Run: git fetch origin && git rebase origin/main

@Priyanshu-byte-coder
Copy link
Copy Markdown
Owner

This PR has merge conflicts because the stream route was updated after you opened this PR. The abort-on-close logic from this PR was partially incorporated in main already. Please rebase on the latest main to bring in the remaining improvements (max-duration timeout, safeEnqueue helper, closeStream helper).

@Priyanshu-byte-coder Priyanshu-byte-coder added the gssoc:approved GSSoC: PR approved for scoring label Jun 2, 2026
@Divv1524 Divv1524 force-pushed the fix/issue-1608-sse-stream-cleanup branch from 4fb6e5a to fa62c76 Compare June 2, 2026 08:36
@Priyanshu-byte-coder Priyanshu-byte-coder added level:beginner GSSoC: Beginner difficulty (20 pts) level:intermediate GSSoC: Intermediate difficulty (35 pts) labels Jun 3, 2026
@Priyanshu-byte-coder Priyanshu-byte-coder merged commit 7feb78e into Priyanshu-byte-coder:main Jun 3, 2026
4 of 10 checks passed
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

🎉 Merged! Thanks for contributing to DevTrack.

If the project has been useful to you, a ⭐ star on the repo is the easiest way to support it — it helps DevTrack get discovered by more developers.

Keep an eye on open issues for your next contribution!

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

Labels

gssoc:approved GSSoC: PR approved for scoring gssoc26 GSSoC 2026 contribution level:beginner GSSoC: Beginner difficulty (20 pts) level:intermediate GSSoC: Intermediate difficulty (35 pts) level2 GSSoC Level 2 - Medium complexity (25 points) quality:clean GSSoC: Clean quality multiplier (×1.2) type:bug GSSoC type bonus: bug fix type:testing GSSoC type bonus: tests (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] SSE stream in /api/stream/route.ts does not close controller on client abort

2 participants