|
20 | 20 | import io.netty.util.concurrent.EventExecutor;
|
21 | 21 | import io.netty.util.concurrent.Future;
|
22 | 22 | import io.netty.util.concurrent.FutureListener;
|
| 23 | +import io.netty.util.concurrent.GlobalEventExecutor; |
23 | 24 | import io.netty.util.concurrent.Promise;
|
24 | 25 | import io.netty.util.internal.ObjectUtil;
|
25 | 26 | import io.netty.util.internal.ThrowableUtil;
|
@@ -437,27 +438,43 @@ public void acquired() {
|
437 | 438 |
|
438 | 439 | @Override
|
439 | 440 | public void close() {
|
440 |
| - executor.execute(new Runnable() { |
441 |
| - @Override |
442 |
| - public void run() { |
443 |
| - if (!closed) { |
444 |
| - closed = true; |
445 |
| - for (;;) { |
446 |
| - AcquireTask task = pendingAcquireQueue.poll(); |
447 |
| - if (task == null) { |
448 |
| - break; |
449 |
| - } |
450 |
| - ScheduledFuture<?> f = task.timeoutFuture; |
451 |
| - if (f != null) { |
452 |
| - f.cancel(false); |
453 |
| - } |
454 |
| - task.promise.setFailure(new ClosedChannelException()); |
455 |
| - } |
456 |
| - acquiredChannelCount = 0; |
457 |
| - pendingAcquireCount = 0; |
458 |
| - FixedChannelPool.super.close(); |
| 441 | + if (executor.inEventLoop()) { |
| 442 | + close0(); |
| 443 | + } else { |
| 444 | + executor.submit(new Runnable() { |
| 445 | + @Override |
| 446 | + public void run() { |
| 447 | + close0(); |
| 448 | + } |
| 449 | + }).awaitUninterruptibly(); |
| 450 | + } |
| 451 | + } |
| 452 | + |
| 453 | + private void close0() { |
| 454 | + if (!closed) { |
| 455 | + closed = true; |
| 456 | + for (;;) { |
| 457 | + AcquireTask task = pendingAcquireQueue.poll(); |
| 458 | + if (task == null) { |
| 459 | + break; |
| 460 | + } |
| 461 | + ScheduledFuture<?> f = task.timeoutFuture; |
| 462 | + if (f != null) { |
| 463 | + f.cancel(false); |
459 | 464 | }
|
| 465 | + task.promise.setFailure(new ClosedChannelException()); |
460 | 466 | }
|
461 |
| - }); |
| 467 | + acquiredChannelCount = 0; |
| 468 | + pendingAcquireCount = 0; |
| 469 | + |
| 470 | + // Ensure we dispatch this on another Thread as close0 will be called from the EventExecutor and we need |
| 471 | + // to ensure we will not block in a EventExecutor. |
| 472 | + GlobalEventExecutor.INSTANCE.execute(new Runnable() { |
| 473 | + @Override |
| 474 | + public void run() { |
| 475 | + FixedChannelPool.super.close(); |
| 476 | + } |
| 477 | + }); |
| 478 | + } |
462 | 479 | }
|
463 | 480 | }
|
0 commit comments