Skip to content

fix(api): register NuQ RabbitMQ consumer before setting listener - #4263

Open
edenbuilds wants to merge 4 commits into
firecrawl:mainfrom
edenbuilds:fix/nuq-consume-try-finally-4244
Open

edenbuilds wants to merge 4 commits into
firecrawl:mainfrom
edenbuilds:fix/nuq-consume-try-finally-4244

Conversation

@edenbuilds

@edenbuilds edenbuilds commented Aug 7, 2026 •

Copy link
Copy Markdown

Summary

  • NuQ.startListener() set this.listener inside try/finally, then called channel.consume() outside that block.
  • If consume() threw, the early-return guard saw a truthy listener forever and never re-registered a consumer — every waitForJob() hung until timeout.
  • Move consume() into the try, only publish this.listener after a successful consume, and clear it on failure.

Fixes #4244

Test plan

  • Simulate channel.consume() throwing after connect/assertQueue; confirm a later startListener() retries instead of no-op'ing.
  • Confirm normal RabbitMQ listen path still delivers completion messages to waitForJob().

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

Fixes a bug where NuQ could set a listener without a consumer, causing waitForJob() to hang. We now register the RabbitMQ consumer first, only set this.listener on success, and clean up with closeAmqpResource on failure; fixes #4244.

  • Bug Fixes
    • Move channel.consume() into startListener() and set this.listener only after a successful consume; noAck: false unchanged.
    • On failure, clear the listener and close the channel/connection via closeAmqpResource to prevent leaks and allow retries; centralize onClose for fast auto-retry; update its expected-close log to "already closing" for restart safety.

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

Review in cubic

Move channel.consume() into the startListener try block and clear
this.listener on failure so a consume() throw cannot leave a
consumer-less channel that permanently trips the early-return guard
and hangs every waitForJob (firecrawl#4244).

@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 1 file

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

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/services/worker/nuq.ts Resolved
Clearing the listener alone left half-open broker resources when
channel.consume() failed. Close the channel and connection in the catch
path so retries do not leak.

Addresses cubic P2 on firecrawl#4263 / firecrawl#4244.
@edenbuilds

Copy link
Copy Markdown
Author

Addressed cubic P2: on channel.consume() failure we now close the half-open channel and connection (in addition to clearing this.listener) so retries do not leak broker resources.

@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 1 file (changes from recent commits).

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

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/services/worker/nuq.ts Resolved
Reuse the shared AMQP close helper instead of swallowing every error with
.catch(() => {}), while still preserving the original consume failure.
@edenbuilds

Copy link
Copy Markdown
Author

Addressed cubic P3: consume-failure cleanup now uses closeAmqpResource (same helper as shutdown) instead of .catch(() => {}). Unexpected close errors are logged; the original consume failure is still what we rethrow so retries stay tied to the real cause.

@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 1 file (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Comment thread apps/api/src/services/worker/nuq.ts Resolved
Expected close log said "during shutdown" but the same helper also
cleans half-open listeners after consume failures.
@edenbuilds

Copy link
Copy Markdown
Author

Also took the follow-up cubic P3 on the log wording: closeAmqpResource now says `already closing` (not `during shutdown`), since the same helper is used for consume-failure cleanup as well as process shutdown.

@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 1 file (changes from recent commits).

Re-trigger cubic

devin-ai-integration Bot pushed a commit to JasonOA888/firecrawl that referenced this pull request Nov 20, 2026
Continue firecrawl#4263 against current main and verify subscription recovery with RabbitMQ and Postgres.

Co-authored-by: edenbuilds <[email protected]>
Co-authored-by: omkar1work <[email protected]>
@edenbuilds

Copy link
Copy Markdown
Author

For anyone finding this older PR: the work continues against current main in #5923, with additional recovery and regression coverage. Thanks @JasonOA888 for carrying it forward.

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: NuQ channel.consume() outside try/finally permanently breaks the RabbitMQ listener — all waitForJob calls hang after one consume() failure

2 participants