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 — Don't leak FixedChannelPool slots on unrelated IllegalArgumentExcepti… · netty/netty@430fdff

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
@@ -308,7 +308,9 @@ public Future<Void> release(final Channel channel, final Promise<Void> promise)
308308
} else {
309309
Throwable cause = future.cause();
310310
// Check if the exception was not because of we passed the Channel to the wrong pool.
311-
if (!(cause instanceof IllegalArgumentException)) {
311+
// Other failures (for example an IllegalArgumentException thrown by the ChannelPoolHandler)
312+
// must still give back the slot.
313+
if (!(cause instanceof ChannelNotAcquiredFromPoolException)) {
312314
decrementAndRunTaskQueue();
313315
}
314316
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
@@ -294,8 +294,7 @@ private void doReleaseChannel(Channel channel, Promise<Void> promise) {
294294
if (channel.attr(POOL_KEY).getAndSet(null) != this) {
295295
closeAndFail(channel,
296296
// Better include a stacktrace here as this is an user error.
297-
new IllegalArgumentException(
298-
"Channel " + channel + " was not acquired from this ChannelPool"),
297+
new ChannelNotAcquiredFromPoolException(channel),
299298
promise);
300299
} else {
301300
if (releaseHealthCheck) {
@@ -414,6 +413,20 @@ public Void call() throws Exception {
414413
});
415414
}
416415

416+
/**
417+
* Fails the release of a {@link Channel} that was not acquired from this pool. This lets {@link FixedChannelPool}
418+
* tell it apart from other release failures, which may also be {@link IllegalArgumentException}s thrown by user
419+
* code.
420+
*/
421+
static final class ChannelNotAcquiredFromPoolException extends IllegalArgumentException {
422+
423+
private static final long serialVersionUID = -7390314262213853178L;
424+
425+
ChannelNotAcquiredFromPoolException(Channel channel) {
426+
super("Channel " + channel + " was not acquired from this ChannelPool");
427+
}
428+
}
429+
417430
private static final class ChannelPoolFullException extends IllegalStateException {
418431

419432
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
@@ -329,7 +329,7 @@ public void testReleaseDifferentPool() throws Exception {
329329
Channel sc = t.sb.bind(t.address).syncUninterruptibly().channel();
330330
ChannelPoolHandler handler = new TestChannelPoolHandler();
331331
ChannelPool pool = new FixedChannelPool(t.cb, handler, 1, 1);
332-
final ChannelPool pool2 = new FixedChannelPool(t.cb, handler, 1, 1);
332+
final FixedChannelPool pool2 = new FixedChannelPool(t.cb, handler, 1, 1);
333333

334334
final Channel channel = pool.acquire().syncUninterruptibly().getNow();
335335

@@ -339,6 +339,8 @@ public void execute() throws Throwable {
339339
pool2.release(channel).syncUninterruptibly();
340340
}
341341
});
342+
// Nothing was acquired from pool2, so the failed release must not give back a slot there.
343+
assertEquals(0, pool2.acquiredChannelCount());
342344
sc.close().syncUninterruptibly();
343345
channel.close().syncUninterruptibly();
344346
pool.close();
@@ -391,6 +393,43 @@ public void testReleaseClosed() {
391393
pool.close();
392394
}
393395

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