diff --git a/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsChannelPool.java b/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsChannelPool.java index f1c3ebd09..55a8aefb2 100644 --- a/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsChannelPool.java +++ b/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsChannelPool.java @@ -178,6 +178,14 @@ private void acquireWithinEventExecutor(final Promise acquirePromise) { this.acquireWithinEventExecutor(acquirePromise); } } else { + // If the deferred acquirePromise is cancelled, remove it from pendingAcquisitionPromises + // to allow garbage collection. + acquirePromise.addListener(future -> { + if (future.isCancelled()) { + pendingAcquisitionPromises.remove(acquirePromise); + } + }); + // We don't have any connections ready to go, and don't have any more capacity to create new // channels. Add this acquisition to the queue waiting for channels to become available. pendingAcquisitionPromises.add(acquirePromise); diff --git a/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClient.java b/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClient.java index 6e119448c..c06703e0c 100644 --- a/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClient.java +++ b/pushy/src/main/java/com/eatthepath/pushy/apns/ApnsClient.java @@ -184,7 +184,8 @@ public PushNotificationFuture>) acquireFuture -> { + Future acquirePromise = this.channelPool.acquire(); + acquirePromise.addListener((GenericFutureListener>) acquireFuture -> { if (acquireFuture.isSuccess()) { final Channel channel = acquireFuture.getNow(); @@ -207,6 +208,12 @@ public PushNotificationFuture