Skip to content

Drain the reader on stop(), so a message already in its socket is not lost - #42

Merged
SerhiyGreench merged 1 commit into
masterfrom
stop-drains-the-reader
Nov 30, 2026
Merged

SerhiyGreench merged 1 commit into
masterfrom
stop-drains-the-reader

Conversation

@SerhiyGreench

Copy link
Copy Markdown
Member

What does this PR do?

RedisQueue.stop() dropped the reader socket at once. A reader blocked in BRPOP (unsafe delivery) or BLMOVE (safe delivery) whose message the broker had already popped and written to the socket lost it with the socket: a request nobody answers, whose caller waits out its timeout. @imqueue/rpc stops the queue on SIGTERM (service.stop() -> imq.stop()), so every scale-down or rollout went through that lossy path.

stop() now drains the reader first, exactly as a broker removed from a cluster already is: drain() sends CLIENT UNBLOCK on the writer, a message already in the socket reaches the message listeners, the read loop ends, and only then is the socket dropped. The wait is bounded by IMQ_CONNECTION_QUIT_TIMEOUT (1 s by default) and never rejects. stop() also clears the drain state (draining, reading, readerId), so start() after stop() reads again — a host may stop and later restart the same queue to pause reading.

destroy() is unchanged: it removes the listeners first, so there is nobody to hand a message to, and drain() already skips a destroyed queue. ClusteredRedisQueue.stop() needs nothing extra: it calls each host's stop() concurrently, so the bound is one timeout, not one per broker.

Tests:

  • a message the broker hands the reader as stop() is called reaches the listener (fails on the old stop());
  • stop() then start() reads the next message.

JSDoc of stop() (on RedisQueue and IMessageQueue), IMQ_CONNECTION_QUIT_TIMEOUT, AGENTS.md and the CHANGELOG (3.10.2) are updated.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / internal
  • Other:

Checklist

  • I have read the Contributing guide.
  • Tests added or updated, and the full suite passes locally (npm test).
  • Docs / doc-blocks updated where relevant.
  • The PR is focused on a single logical change.

Contribution terms (required)

  • I have read and agree to the @imqueue Contribution Terms.
    I grant the project owner the right to license my contribution
    commercially, royalty-free, my contribution stays available under
    GPL-3.0, I keep my copyright, and I understand I will receive no fee for
    it. If I did not agree, I would not be submitting this contribution.

…ocket is not lost

stop() destroyed the reader socket at once. A reader blocked in BRPOP or
BLMOVE whose message the broker had already popped and written to the socket
lost it with the socket: a request nobody answered, its caller waiting out
its timeout. @imqueue/rpc's graceful shutdown stops the queue on SIGTERM, so
every scale-down or rollout went through that path.

stop() now drains the reader first, as a broker removed from a cluster
already was (CLIENT UNBLOCK on the writer, bounded by
IMQ_CONNECTION_QUIT_TIMEOUT, never rejects), then drops the socket. It
clears the drain state, so start() after stop() reads again.
@SerhiyGreench
SerhiyGreench merged commit 8df7db5 into master Nov 30, 2026
11 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant