GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port: Don't leak FixedChannelPool slots on unrelated IllegalArgumentE… · lltopk/netty@23e63cc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport/src
    • main/java/io/netty/channel/pool
      • FixedChannelPool.java
      • SimpleChannelPool.java
    • test/java/io/netty/channel/pool
      • FixedChannelPoolTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport/src/main/java/io/netty/channel/pool/FixedChannelPool.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/pool/FixedChannelPool.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ public void operationComplete(Future<Void> future) {
311311
} else {
312312
Throwable cause = future.cause();
313313
// Check if the exception was not because of we passed the Channel to the wrong pool.
314-
if (!(cause instanceof IllegalArgumentException)) {
314+
// Other failures (for example an IllegalArgumentException thrown by the ChannelPoolHandler)
315+
// must still give back the slot.
316+
if (!(cause instanceof ChannelNotAcquiredFromPoolException)) {
315317
decrementAndRunTaskQueue();
316318
}
317319
promise.setFailure(future.cause());
Collapse file

‎transport/src/main/java/io/netty/channel/pool/SimpleChannelPool.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/pool/SimpleChannelPool.java
+15-2Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ private void doReleaseChannel(Channel channel, Promise<Void> promise) {
304304
if (channel.attr(POOL_KEY).getAndSet(null) != this) {
305305
closeAndFail(channel,
306306
// Better include a stacktrace here as this is an user error.
307-
new IllegalArgumentException(
308-
"Channel " + channel + " was not acquired from this ChannelPool"),
307+
new ChannelNotAcquiredFromPoolException(channel),
309308
promise);
310309
} else {
311310
if (releaseHealthCheck) {
@@ -429,6 +428,20 @@ public Void call() throws Exception {
429428
});
430429
}
431430

431+
/**
432+
* Fails the release of a {@link Channel} that was not acquired from this pool. This lets {@link FixedChannelPool}
433+
* tell it apart from other release failures, which may also be {@link IllegalArgumentException}s thrown by user
434+
* code.
435+
*/
436+
static final class ChannelNotAcquiredFromPoolException extends IllegalArgumentException {
437+
438+
private static final long serialVersionUID = -7390314262213853178L;
439+
440+
ChannelNotAcquiredFromPoolException(Channel channel) {
441+
super("Channel " + channel + " was not acquired from this ChannelPool");
442+
}
443+
}
444+
432445
private static final class ChannelPoolFullException extends IllegalStateException {
433446

434447
private ChannelPoolFullException() {
Collapse file

‎transport/src/test/java/io/netty/channel/pool/FixedChannelPoolTest.java‎

Copy file name to clipboardExpand all lines: transport/src/test/java/io/netty/channel/pool/FixedChannelPoolTest.java
+40-1Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public void testReleaseDifferentPool() throws Exception {
335335
Channel sc = t.sb.bind(t.address).syncUninterruptibly().channel();
336336
ChannelPoolHandler handler = new TestChannelPoolHandler();
337337
ChannelPool pool = new FixedChannelPool(t.cb, handler, 1, 1);
338-
final ChannelPool pool2 = new FixedChannelPool(t.cb, handler, 1, 1);
338+
final FixedChannelPool pool2 = new FixedChannelPool(t.cb, handler, 1, 1);
339339

340340
final Channel channel = pool.acquire().syncUninterruptibly().getNow();
341341

@@ -345,6 +345,8 @@ public void execute() throws Throwable {
345345
pool2.release(channel).syncUninterruptibly();
346346
}
347347
});
348+
// Nothing was acquired from pool2, so the failed release must not give back a slot there.
349+
assertEquals(0, pool2.acquiredChannelCount());
348350
sc.close().syncUninterruptibly();
349351
channel.close().syncUninterruptibly();
350352
pool.close();
@@ -397,6 +399,43 @@ public void testReleaseClosed() {
397399
pool.close();
398400
}
399401

402+
@Test
403+
public void testReleaseFailsWithIllegalArgumentExceptionFromHandler() throws Exception {
404+
Tuple t = bootstrap();
405+
406+
// Start server
407+
Channel sc = t.sb.bind(t.address).syncUninterruptibly().channel();
408+
final IllegalArgumentException exception = new IllegalArgumentException();
409+
FixedChannelPool pool = new FixedChannelPool(t.cb, new AbstractChannelPoolHandler() {
410+
@Override
411+
public void channelCreated(Channel ch) {
412+
// NOOP
413+
}
414+
415+
@Override
416+
public void channelReleased(Channel ch) {
417+
throw exception;
418+
}
419+
}, 1);
420+
421+
Channel channel = pool.acquire().syncUninterruptibly().getNow();
422+
assertEquals(1, pool.acquiredChannelCount());
423+
424+
Future<Void> releaseFuture = pool.release(channel).awaitUninterruptibly();
425+
assertSame(exception, releaseFuture.cause());
426+
// The exception was not caused by releasing the Channel to the wrong pool, so the slot must be given back.
427+
assertEquals(0, pool.acquiredChannelCount());
428+
429+
Future<Channel> acquireFuture = pool.acquire();
430+
assertTrue(acquireFuture.await(1, TimeUnit.SECONDS));
431+
assertTrue(acquireFuture.isSuccess());
432+
assertEquals(1, pool.acquiredChannelCount());
433+
434+
sc.close().syncUninterruptibly();
435+
acquireFuture.getNow().close().syncUninterruptibly();
436+
pool.close();
437+
}
438+
400439
@Test
401440
public void testCloseAsync() throws ExecutionException, InterruptedException {
402441
Tuple t = bootstrap();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.