Skip to content

Fix the pool connection acquisition timeout - #1807

Closed
tsegismont wants to merge 1 commit into
eclipse-vertx:masterfrom
tsegismont:pool-timeout-waiter-cancellation
Closed

tsegismont wants to merge 1 commit into
eclipse-vertx:masterfrom
tsegismont:pool-timeout-waiter-cancellation

Conversation

@tsegismont

Copy link
Copy Markdown
Member

A pooled query that timed out waiting for a connection kept its place in the pool wait queue, so later requests were rejected with a ConnectionPoolTooBusyException although nothing was waiting, and the queue metric stayed inflated.

Pool#getConnection could also hang forever: when the timer fired as the pool emitted a lease, Vertx#cancelTimer and ConnectionPool#cancel both reported a loss for the same request and neither completed it, leaking the queue metric as well.

Both paths now share a PoolRequest whose lifecycle is arbitrated by a single atomically updated state, so exactly one of the timer and the pool completes the request. The timer is set from the waiter listener callbacks, where the waiter is known, so a timed out request always leaves the wait queue before it fails.

Pool#getConnection now reports a timeout with Timeout waiting for connection instead of Timeout.

A pooled query no longer hops back to the context of the caller to schedule its command, it reports the command on that context explicitly instead. As a consequence an acquisition failure is no longer dispatched on the context of the caller, which CommandScheduler#schedule now documents.

A pooled query no longer applies the beforeRecycle hook when the afterAcquire hook fails, like Pool#getConnection already did.

Assisted-by: Anthropic Claude (claude-opus-5)

A pooled query that timed out waiting for a connection kept its place in the pool wait queue, so later requests were rejected with a `ConnectionPoolTooBusyException` although nothing was waiting, and the queue metric stayed inflated.

`Pool#getConnection` could also hang forever: when the timer fired as the pool emitted a lease, `Vertx#cancelTimer` and `ConnectionPool#cancel` both reported a loss for the same request and neither completed it, leaking the queue metric as well.

Both paths now share a `PoolRequest` whose lifecycle is arbitrated by a single atomically updated state, so exactly one of the timer and the pool completes the request. The timer is set from the waiter listener callbacks, where the waiter is known, so a timed out request always leaves the wait queue before it fails.

`Pool#getConnection` now reports a timeout with `Timeout waiting for connection` instead of `Timeout`.

A pooled query no longer hops back to the context of the caller to schedule its command, it reports the command on that context explicitly instead. As a consequence an acquisition failure is no longer dispatched on the context of the caller, which `CommandScheduler#schedule` now documents.

A pooled query no longer applies the `beforeRecycle` hook when the `afterAcquire` hook fails, like `Pool#getConnection` already did.

Assisted-by: Anthropic Claude (claude-opus-5)
Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
private final long timeout;
private final Object metric;

private volatile long state = NO_TIMER;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we simply use an AtomicLong here ? atomic field updates are useful under intensive usage, here we care mostly about visibility and correctness

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To my knowledge, the field updater is useful when many instances are created, which is the case here: one PoolRequest per pool query or connection acquisition. But I have no strong opinion, if you prefer AtomicLong I will change it.

@vietj

vietj commented Nov 19, 2026

Copy link
Copy Markdown
Member

I think overrall we can improve the design : cancelTimer returns a boolean indicating whether the timer will execute, when completing the section we should use this instead of the state to decide whether we should continue or the timer ran effectively and no action should be done

@tsegismont

Copy link
Copy Markdown
Member Author

I think overrall we can improve the design : cancelTimer returns a boolean indicating whether the timer will execute, when completing the section we should use this instead of the state to decide whether we should continue or the timer ran effectively and no action should be done

Agreed, cancelTimer is the right arbiter for this:

  • complete() cancels the timer and uses the result: it continues when the timer was cancelled, and does nothing when the timer ran;
  • when the timer fires, it owns the failure of the request; the pool.cancel result only tells whether the waiter was removed from the wait queue, it does not decide anything.

However I don't see how to remove the state field completely. It would still be needed to store the timer id, and to prevent arming a timer for a request completed already, or two timers for the same request:

  • the timer is set from the listener callbacks only, because the waiter is needed to cancel from the wait queue and it is known only there, so the id must be shared with complete();
  • the pool runs the callbacks on the thread that drains the combiner, after the lock is released, so a callback can run after the request is completed, or onEnqueue and onConnect can overlap. Example: the pool emits the lease while the onEnqueue callback has not run yet, complete() sees no timer and delivers, then the callback arms a timer that fails the request again.

If the pool executed the listener callbacks ordered with the other operations (inside the combiner, or at least before the delivery to the same waiter), a plain volatile timer id would be enough, without the CAS and the completed state. Perhaps something to discuss for vert.x core. An API to cancel a waiter without holding the PoolWaiter instance would help too, the timer could be set before acquire().

Did you mean the state field can be removed completely? If you see how, I am interested.

@tsegismont
tsegismont requested a review from vietj November 25, 2026 17:50
vietj added a commit that referenced this pull request Dec 1, 2026
Motivation:

Adopt the new Vert.x connection pool implementation
(eclipse-vertx/vert.x#6587) that will provide more features, one of them
is getting away from a timer per acquisition since the pool owns the
acquisition lifecycle (#1807, #1342).

Changes:

The pool checks staleness, closes the connections it evicts and times
out acquisitions with a TimeoutException. As a side effect this fixes
the bug addressed by #1807: a timed out query no longer keeps its slot
in the wait queue, asserted by a test ported from that PR. A periodic
refresh, configured by the new refresh period option (100 ms default),
drives all the maintenance tasks; the pool cleaner period is deprecated.
The queue metric only accounts acquisitions that effectively wait.

Assisted-by: Anthropic Claude (claude-fable-5)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Julien Viet <julien@julienviet.com>
vietj added a commit that referenced this pull request Dec 2, 2026
Motivation:

Adopt the new Vert.x connection pool implementation
(eclipse-vertx/vert.x#6587) that will provide more features, one of them
is getting away from a timer per acquisition since the pool owns the
acquisition lifecycle (#1807, #1342).

Changes:

The pool checks staleness with the connection checker configured in the
builder, closes the connections it evicts and times out acquisitions
with a TimeoutException. As a side effect this fixes the bug addressed
by #1807: a timed out query no longer keeps its slot in the wait queue,
asserted by a test ported from that PR. A mandatory periodic refresh,
configured by the new refresh period option (a Duration of at least one
millisecond, 100 ms by default), drives all the maintenance tasks; the
pool cleaner period is deprecated. The queue metric only accounts
acquisitions that effectively wait.

Assisted-by: Anthropic Claude (claude-fable-5)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Julien Viet <julien@julienviet.com>
@tsegismont tsegismont removed this from the 5.3.0 milestone Dec 2, 2026
@tsegismont

Copy link
Copy Markdown
Member Author

Superseded by #1820

@tsegismont tsegismont closed this Dec 2, 2026
@tsegismont
tsegismont deleted the pool-timeout-waiter-cancellation branch December 2, 2026 08:23
vietj added a commit that referenced this pull request Dec 2, 2026
Motivation:

Adopt the new Vert.x connection pool implementation
(eclipse-vertx/vert.x#6587) that will provide more features, one of them
is getting away from a timer per acquisition since the pool owns the
acquisition lifecycle (#1807, #1342).

Changes:

The pool checks staleness with the connection checker configured in the
builder, closes the connections it evicts and times out acquisitions
with a TimeoutException. As a side effect this fixes the bug addressed
by #1807: a timed out query no longer keeps its slot in the wait queue,
asserted by a test ported from that PR. A mandatory periodic refresh,
configured by the new refresh period option (a Duration of at least one
millisecond, 100 ms by default), drives all the maintenance tasks; the
pool cleaner period is deprecated. The queue metric only accounts
acquisitions that effectively wait.

Assisted-by: Anthropic Claude (claude-fable-5)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Julien Viet <julien@julienviet.com>
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.

2 participants