Skip to content

fix(api): stop crawl-status WS polling on client disconnect - #4261

Open
edenbuilds wants to merge 4 commits into
firecrawl:mainfrom
edenbuilds:fix/crawl-status-ws-stop-on-close-4242
Open

edenbuilds wants to merge 4 commits into
firecrawl:mainfrom
edenbuilds:fix/crawl-status-ws-stop-on-close-4242

Conversation

@edenbuilds

@edenbuilds edenbuilds commented Aug 7, 2026 •

Copy link
Copy Markdown

Summary

  • Crawl-status WebSockets (v1 + v2) schedule a 1s Redis/BullMQ poll loop but never stop it when the client disconnects.
  • Listen for close/error, clear the pending timeout, and set finished so polling cannot continue after hangup.
  • Also route normal completion paths through the same stop() helper so timeouts are cleared consistently.

Fixes #4242

Test plan

  • Open crawl-status WS for an in-progress crawl, disconnect client, confirm server stops scheduling setTimeout(loop, 1000) (no further Redis/queue polls for that socket).
  • Complete a crawl over WS and confirm done still closes cleanly.
  • Force a socket error and confirm the loop stops the same way as close.

Made with Cursor


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Stops crawl-status WebSocket polling when the client disconnects, including during auth or mid-await, to prevent orphaned Redis/BullMQ polls and free resources. Applies to both v1 and v2 controllers. Fixes #4242.

  • Bug Fixes
    • Add a shared stop() and scheduleLoop; clear the pending timeout and route completion, error, and early-disconnect bail paths through it to avoid a CLOSING→CLOSED race.
    • Register close/error handlers before any await (incl. auth), bail if the socket is already closed, and re-check socket state after each await/poll.
    • Clean up temporary auth listeners in finally and avoid rescheduling after disconnect.
    • Drop a dead Sentry.captureException call left over from the removed Sentry dependency; the existing logger.error() still reports errors.

Written for commit f8ab93a. Summary will update on new commits.

Review in cubic

Clear the pending loop timeout and set finished when the socket closes
or errors, so Redis/BullMQ status polling does not continue after the
client hangs up (firecrawl#4242). Applied to v1 and v2 controllers.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Tip: cubic used a learning from your PR history. Let your coding agent read cubic learnings directly with the cubic MCP.

Fix all with cubic | Re-trigger cubic

Register close/error handlers before any await (including auth), bail if
the socket is already closed, and re-check finished after each Redis /
BullMQ poll so a hangup cannot leave the 1s loop running.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Route every early return that detects a non-OPEN socket through stop()
so a CLOSING→CLOSED race cannot leave a pending setTimeout alive before
the async close handler runs.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

0 issues found across 2 files (changes from recent commits).

Re-trigger cubic

Upstream removed Sentry entirely in favor of OpenTelemetry (firecrawl#4555), which
is why this hunk conflicted even though upstream never touched this
function: the base state on both sides had no Sentry usage. Resolving
in favor of this branch's closedDuringAuth/isSocketOpen guard would have
kept an unimported `Sentry.captureException(err)` call — a leftover from
before this repo dropped the Sentry dependency, and something that would
throw ReferenceError on the exact disconnect-during-auth path this PR
hardens. Dropped it; the existing logger.error() call below already
reports the full error.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

This branch has not been deployed

No deployments
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.

api: crawl-status WebSocket polling loop never stops after client disconnects — no ws.on('close') handler

1 participant