Skip to content

fix(api): cleanly handle abort signals and controller state in SSE stream API#1795

Open
ArshVermaGit wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
ArshVermaGit:main_6
Open

fix(api): cleanly handle abort signals and controller state in SSE stream API#1795
ArshVermaGit wants to merge 1 commit into
Priyanshu-byte-coder:mainfrom
ArshVermaGit:main_6

Conversation

@ArshVermaGit
Copy link
Copy Markdown
Contributor

Description

This PR addresses flakiness and memory leak issues in the Server-Sent Events (SSE) streaming API (/api/stream), as well as fixing the associated test suite which was failing completely.

Previously, if a client disconnected while the backend was waiting on an asynchronous database poll, the server would subsequently attempt to call controller.enqueue() on an already closed stream. This resulted in noisy TypeError: Invalid state: Controller is already closed exceptions and potential hanging promises.

Additionally, the test/sse-stream-route.test.ts suite was completely failing due to a cross-environment Type mismatch where NextRequest rejected the Node-native AbortSignal constructed by Vitest.

Resolved Issue

Resolves #1794

Changes Made

  • Safe SSE Teardown: Introduced a robust isClosed flag to the SSE polling logic. The interval now immediately halts on abort, and any pending async database responses are safely discarded without attempting to enqueue into a dead stream.
  • Test Suite Resiliency: Updated the test mocks to bypass strict NextRequest constructor validations by injecting the AbortSignal via Object.defineProperty post-instantiation.

Impact

  • Backend Stability: Prevents noisy console errors, resolves race conditions, and ensures clean teardown of resources when users close dashboard tabs.
  • Continuous Integration: The SSE test suite is now fully unblocked and reliably passes all concurrency and memory-leak checks.

Testing

  • Ran the full SSE test suite locally (npm run test test/sse-stream-route.test.ts).
  • Verified that all 15 concurrency and abort cleanup tests pass flawlessly with 0 console errors.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 31, 2026

@ArshVermaGit 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 May 31, 2026
@github-actions
Copy link
Copy Markdown

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
Contributor Author

@ArshVermaGit ArshVermaGit left a comment

Choose a reason for hiding this comment

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

Hi @Priyanshu-byte-coder ! Issue #1794 has been resolved. Please review the PR and merge it under GSSoC. Thanks!

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

Labels

gssoc26 GSSoC 2026 contribution 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: Real-time SSE streaming API fails tests due to connection and abort controller misconfigurations

1 participant