Skip to content

fix(client): settle the close() drain when the last connect fails - #3400

Merged
nkaradzhov merged 1 commit into
redis:masterfrom
kyungseopk1m:fix/pool-close-drain-on-failed-connect
Aug 14, 2026
Merged

nkaradzhov merged 1 commit into
redis:masterfrom
kyungseopk1m:fix/pool-close-drain-on-failed-connect

Conversation

@kyungseopk1m

@kyungseopk1m kyungseopk1m commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #3398.

#create() pushes the client onto #clientsInUse before it awaits client.connect(), so a connecting client counts toward capacity. On a connect failure it removes the node directly instead of going through #returnClient(), which is the only place that resolves #drainResolve. If close() was already waiting for the pool to drain, #clientsInUse reached 0 without waking it and close() never settled.

Signalling the drain from that path is not enough on its own. A task queued behind the pending connect is exactly what close() is still waiting for, and once the connect fails there is no client left to run it on: execute() rejects new work while closing, and nothing will call #returnClient() again. So the failure path now settles those tasks with the connection error before it resolves the drain. The caller gets the real reason instead of a later TimeoutError, or nothing at all when acquireTimeout is 0.

This is scoped to a closing pool. While the pool is open a failed scale-up leaves the queued task on its existing acquireTimeout path, which is #3397 and a separate discussion. Draining the queue here only uses shift(), so it does not touch SinglyLinkedList.remove().

Two tests cover it, neither needs a server:

  • close() settles when the only pending connect fails
  • a task queued behind that connect is rejected with the connection error, close() settles, and the task's acquire timer is cleared with it (otherwise it later fires on a node that is already out of the queue and remove() throws)

Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

Note

Medium Risk
Touches pool shutdown and task-queue settlement on connect failure; behavior is scoped to closing pools but affects promise lifecycle and timers.

Overview
Fixes a hang in RedisClientPool.close() when shutdown overlaps a failing connect(): #create() removed the in-flight client from #clientsInUse on error without invoking #returnClient(), so #drainResolve never ran even though the pool was empty.

On that path, when the pool is closing and no clients remain in use, the failure handler now drains #tasksQueue—clearTimeout on each waiter, reject with the connect error, then call #drainResolve—so queued execute() work fails with the real connection error instead of waiting on acquireTimeout, and close() can finish.

Adds two no-server regression tests: close() resolves after the only pending connect fails, and a task queued behind that connect is rejected, timers are cleared, and close() still completes.

Reviewed by Cursor Bugbot for commit 274ffd3. Bugbot is set up for automated code reviews on this repo. Configure here.

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @kyungseopk1m, this looks good to go

@nkaradzhov
nkaradzhov merged commit 2c7a857 into redis:master Aug 14, 2026
15 checks passed
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.

Pool: close() hangs forever if a scale-up connect fails while draining

2 participants