Skip to content

POOL: Release Channel when SimpleChannelPool acquire is cancelled - #17569

Merged
normanmaurer merged 1 commit into
netty:4.2from
renechoi:fix/pool-healthcheck-acquire-cancel
Sep 21, 2026
Merged

normanmaurer merged 1 commit into
netty:4.2from
renechoi:fix/pool-healthcheck-acquire-cancel

Conversation

@renechoi

Copy link
Copy Markdown
Contributor

Motivation

notifyConnect() completes the acquire Promise with trySuccess(...) and releases the Channel if already completed. notifyHealthCheck(), the reuse path, still uses setSuccess(...).

doHealthCheck runs via loop.execute(...) when acquire() is called off its event loop, so a cancel can land while that task is queued, even with the stock ACTIVE checker. setSuccess(...) then throws into closeAndFail(...): a healthy pooled Channel is closed and its channelAcquired(...) gets no channelReleased(...).

#17287 gave FixedChannelPool this guard.

Modification

notifyHealthCheck() mirrors notifyConnect(), plus a regression test on a hand-completed health check.

Result

Reverting just the source hunk fails that test on releasedCount(), 2 expected, 1 seen. With it, 34/34.

Motivation:

SimpleChannelPool hands an acquired Channel to the caller's Promise in two
places. notifyConnect() completes the Promise with trySuccess(...) and releases
the Channel back into the pool when the Promise turns out to be completed
already, which is what happens when the caller cancelled the acquire.
notifyHealthCheck(), the path taken when an already pooled Channel is reused,
still completes the Promise with setSuccess(...).

So when the acquire Promise is completed while the health check of a pooled
Channel is still pending, setSuccess(...) throws IllegalStateException. The
surrounding catch routes it to closeAndFail(...), which closes a healthy pooled
Channel instead of offering it back. handler.channelAcquired(...) has already
been invoked at that point, so the matching channelReleased(...) callback never
arrives and a ChannelPoolHandler that tracks acquire and release counts stays
out of balance for the rest of the pool's lifetime.

FixedChannelPool.AcquireListener already guards the same race the same way.

Modifications:

- Complete the acquire Promise in notifyHealthCheck() with trySuccess(...) and
  release the Channel again when it was completed already, mirroring what
  notifyConnect() does.
- Add a regression test that cancels the acquire Promise while the health check
  of a pooled Channel is pending, and asserts that the Channel stays active, is
  handed back by the next acquire, and that the pool handler callbacks stay
  balanced.

Result:

Cancelling an acquire no longer destroys a pooled Channel, and the
channelAcquired and channelReleased callbacks stay balanced.
@testlens-app

testlens-app Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

🚨 TestLens detected 1 failed test 🚨

Here is what you can do:

  1. Inspect the test failures carefully.
  2. If you are convinced that some of the tests are flaky, you can mute them below.
  3. Finally, trigger a rerun by checking the rerun checkbox.

Test Summary

Build PR / macos-x86_64-java11-boringssl build > Netty/Transport/Native/KQueue

Test Runs Flakiness
KQueueJdkLoopbackSocketSslEchoTest > testSslEcho(SslContext, SslContext, Renegotiation, boolean, boolean, boolean, boolean, boolean, TestInfo) > 49: serverEngine = io.netty.handler.ssl.JdkSslServerContext@1c98b4eb, clientEngine = io.netty.handler.ssl.JdkSslClientContext@45801322, renegotiation = CLIENT_INITIATED(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA), serverUsesDelegatedTaskExecutor = true, clientUsesDelegatedTaskExecutor = false, autoRead = false, useChunkedWriteHandler = false, useCompositeByteBuf = false ❌ 1% 🟡

🏷️ Commit: f82323a
▶️ Tests: 435059 executed
🟡 Checks: 29/32 completed

Test Failures

KQueueJdkLoopbackSocketSslEchoTest > testSslEcho(SslContext, SslContext, Renegotiation, boolean, boolean, boolean, boolean, boolean, TestInfo) > 49: serverEngine = io.netty.handler.ssl.JdkSslServerContext@1c98b4eb, clientEngine = io.netty.handler.ssl.JdkSslClientContext@45801322, renegotiation = CLIENT_INITIATED(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA), serverUsesDelegatedTaskExecutor = true, clientUsesDelegatedTaskExecutor = false, autoRead = false, useChunkedWriteHandler = false, useCompositeByteBuf = false (Netty/Transport/Native/KQueue in Build PR / macos-x86_64-java11-boringssl build)
java.util.concurrent.TimeoutException: testSslEcho(io.netty.handler.ssl.SslContext, io.netty.handler.ssl.SslContext, io.netty.testsuite.transport.socket.SocketSslEchoTest$Renegotiation, boolean, boolean, boolean, boolean, boolean, org.junit.jupiter.api.TestInfo) timed out after 30000 milliseconds
	at java.base/java.util.Optional.ifPresent(Optional.java:183)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
	at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
	at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
	at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
	at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	Suppressed: java.lang.InterruptedException
		at java.base/java.lang.Object.wait(Native Method)
		at java.base/java.lang.Object.wait(Object.java:328)
		at io.netty.util.concurrent.DefaultPromise.await(DefaultPromise.java:268)
		at io.netty.util.concurrent.DefaultPromise.sync(DefaultPromise.java:419)
		at io.netty.util.concurrent.DefaultPromise.sync(DefaultPromise.java:37)
		at io.netty.testsuite.transport.socket.SocketSslEchoTest.testSslEcho(SocketSslEchoTest.java:326)
		at io.netty.testsuite.transport.socket.SocketSslEchoTest$1.run(SocketSslEchoTest.java:256)
		at io.netty.testsuite.transport.socket.SocketSslEchoTest$1.run(SocketSslEchoTest.java:253)
		at io.netty.testsuite.transport.AbstractComboTestsuiteTest.run(AbstractComboTestsuiteTest.java:52)
		at io.netty.testsuite.transport.socket.SocketSslEchoTest.testSslEcho(SocketSslEchoTest.java:253)
		at java.base/java.lang.reflect.Method.invoke(Method.java:566)
		... 40 more

Rerun Controls

Note

Checks are currently running using the configuration below.

Select tests to mute in this pull request:

🔲 KQueueJdkLoopbackSocketSslEchoTest > testSslEcho(SslContext, SslContext, Renegotiation, boolean, boolean, boolean, boolean, boolean, TestInfo)

Reuse successful test results:

🔲 ♻️ Only rerun the tests that failed or were muted before

Click the checkbox to trigger a rerun:

🔲 Rerun jobs


Learn more about TestLens at testlens.app/docs.

@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Sep 21, 2026
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Sep 21, 2026
@normanmaurer
normanmaurer merged commit 6b257ce into netty:4.2 Sep 21, 2026
29 of 30 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17578

@netty-project-bot

Copy link
Copy Markdown
Contributor

Could not create auto-port PR.
Got conflicts when cherry-picking onto 5.0.

@normanmaurer

Copy link
Copy Markdown
Member

Port for 5.0: #17580

normanmaurer added a commit that referenced this pull request Sep 21, 2026
…ed (#17569) (#17580)

`notifyConnect()` completes the acquire `Promise` with `trySuccess(...)`
and releases the `Channel` if already completed. `notifyHealthCheck()`,
the reuse path, still uses `setSuccess(...)`.

`doHealthCheck` runs via `loop.execute(...)` when `acquire()` is called
off its event loop, so a cancel can land while that task is queued, even
with the stock `ACTIVE` checker. `setSuccess(...)` then throws into
`closeAndFail(...)`: a healthy pooled `Channel` is closed and its
`channelAcquired(...)` gets no `channelReleased(...)`.

`notifyHealthCheck()` mirrors `notifyConnect()`, plus a regression test
on a hand-completed health check.

Reverting just the source hunk fails that test on `releasedCount()`, 2
expected, 1 seen. With it, 34/34.

Co-authored-by: renechoi <[email protected]>
normanmaurer pushed a commit that referenced this pull request Sep 22, 2026
…7569)

`notifyConnect()` completes the acquire `Promise` with `trySuccess(...)`
and releases the `Channel` if already completed. `notifyHealthCheck()`,
the reuse path, still uses `setSuccess(...)`.

`doHealthCheck` runs via `loop.execute(...)` when `acquire()` is called
off its event loop, so a cancel can land while that task is queued, even
with the stock `ACTIVE` checker. `setSuccess(...)` then throws into
`closeAndFail(...)`: a healthy pooled `Channel` is closed and its
`channelAcquired(...)` gets no `channelReleased(...)`.

`notifyHealthCheck()` mirrors `notifyConnect()`, plus a regression test
on a hand-completed health check.

Reverting just the source hunk fails that test on `releasedCount()`, 2
expected, 1 seen. With it, 34/34.

(cherry picked from commit 6b257ce)
normanmaurer added a commit that referenced this pull request Sep 22, 2026
…s cancelled (#17578)

Auto-port of #17569 to 4.1
Cherry-picked commit: 6b257ce

---
### Motivation

`notifyConnect()` completes the acquire `Promise` with `trySuccess(...)`
and releases the `Channel` if already completed. `notifyHealthCheck()`,
the reuse path, still uses `setSuccess(...)`.

`doHealthCheck` runs via `loop.execute(...)` when `acquire()` is called
off its event loop, so a cancel can land while that task is queued, even
with the stock `ACTIVE` checker. `setSuccess(...)` then throws into
`closeAndFail(...)`: a healthy pooled `Channel` is closed and its
`channelAcquired(...)` gets no `channelReleased(...)`.

#17287 gave `FixedChannelPool` this guard.

### Modification

`notifyHealthCheck()` mirrors `notifyConnect()`, plus a regression test
on a hand-completed health check.

### Result

Reverting just the source hunk fails that test on `releasedCount()`, 2
expected, 1 seen. With it, 34/34.

---------

Co-authored-by: renechoi <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
@normanmaurer normanmaurer removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 16, 2026
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…s cancelled (netty#17578)

Auto-port of netty#17569 to 4.1
Cherry-picked commit: 6b257ce

---
### Motivation

`notifyConnect()` completes the acquire `Promise` with `trySuccess(...)`
and releases the `Channel` if already completed. `notifyHealthCheck()`,
the reuse path, still uses `setSuccess(...)`.

`doHealthCheck` runs via `loop.execute(...)` when `acquire()` is called
off its event loop, so a cancel can land while that task is queued, even
with the stock `ACTIVE` checker. `setSuccess(...)` then throws into
`closeAndFail(...)`: a healthy pooled `Channel` is closed and its
`channelAcquired(...)` gets no `channelReleased(...)`.

netty#17287 gave `FixedChannelPool` this guard.

### Modification

`notifyHealthCheck()` mirrors `notifyConnect()`, plus a regression test
on a hand-completed health check.

### Result

Reverting just the source hunk fails that test on `releasedCount()`, 2
expected, 1 seen. With it, 34/34.

---------

Co-authored-by: renechoi <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
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.

3 participants