@@ -425,6 +425,14 @@ public void release(final PoolEntry<R, C> entry, final boolean reusable) {
425425 }
426426 }
427427
428+ triggerDrainIfMany ();
429+ } else if (!handOffToCompatibleWaiter (entry , seg )) {
430+ offerAvailable (seg , entry );
431+
432+ if (!seg .waiters .isEmpty ()) {
433+ enqueueIfNeeded (seg );
434+ }
435+
428436 triggerDrainIfMany ();
429437 }
430438 } else {
@@ -745,9 +753,12 @@ private void maybeCleanupSegment(final R route, final Segment seg) {
745753 if (seg .reclaimEnqueued .getAndSet (false )) {
746754 reclaimQueue .remove (seg );
747755 }
748- final PendingRoute pending = seg .pending .getAndSet ( null );
756+ final PendingRoute pending = seg .pending .get ( );
749757 if (pending != null ) {
750- pendingRouteCount .decrementAndGet ();
758+ pendingQueue .remove (pending );
759+ if (seg .pending .compareAndSet (pending , null )) {
760+ pendingRouteCount .decrementAndGet ();
761+ }
751762 }
752763 }
753764 }
@@ -880,6 +891,20 @@ private void enqueueIfNeeded(final Segment seg) {
880891 pendingRouteCount .incrementAndGet ();
881892 if (seg .pending .compareAndSet (null , pending )) {
882893 pendingQueue .offer (pending );
894+
895+ // The registration can be retired between the CAS and the offer.
896+ // Remove the just-published token if it is already obsolete.
897+ if (seg .pending .get () != pending ) {
898+ pendingQueue .remove (pending );
899+ return ;
900+ }
901+
902+ // The last waiter can disappear while the registration is being
903+ // published. Retire it immediately rather than leaving a stale
904+ // marker for a later drain.
905+ if (seg .waiters .isEmpty ()) {
906+ dequeueIfDrained (seg );
907+ }
883908 } else {
884909 pendingRouteCount .decrementAndGet ();
885910 }
@@ -891,17 +916,39 @@ private void dequeueIfDrained(final Segment seg) {
891916 }
892917
893918 final PendingRoute pending = seg .pending .get ();
894- if (pending != null && seg .pending .compareAndSet (pending , null )) {
895- pendingRouteCount .decrementAndGet ();
919+ if (pending != null ) {
920+ // Remove the exact generation before dropping the registration
921+ // count. If a drain has already polled it, remove() simply fails
922+ // and that drain will observe the generation change below.
923+ pendingQueue .remove (pending );
896924
897- // A waiter can arrive while the old registration is being
898- // retired. In that case publish a new generation.
899- if (!seg .waiters .isEmpty ()) {
900- enqueueIfNeeded (seg );
925+ if (seg .pending .compareAndSet (pending , null )) {
926+ pendingRouteCount .decrementAndGet ();
927+
928+ // A waiter can arrive while the old registration is being
929+ // retired. In that case publish a new generation.
930+ if (!seg .waiters .isEmpty ()) {
931+ enqueueIfNeeded (seg );
932+ }
901933 }
902934 }
903935 }
904936
937+ private void requeuePending (final PendingRoute pending ) {
938+ final Segment seg = pending .segment ;
939+ if (seg .pending .get () != pending ) {
940+ return ;
941+ }
942+
943+ pendingQueue .offer (pending );
944+
945+ // Close the race with cancellation / timeout retiring the generation
946+ // between the validation above and the queue offer.
947+ if (seg .pending .get () != pending ) {
948+ pendingQueue .remove (pending );
949+ }
950+ }
951+
905952 private boolean hasDrainOpportunity () {
906953 return totalAllocated .get () < maxTotal .get () || !reclaimQueue .isEmpty ();
907954 }
@@ -978,21 +1025,26 @@ private int serveRoundRobin(final int budget, final int maxCreated) {
9781025 private int serveRoundRobin (final int budget , final int maxCreated , final Segment excludedSegment ) {
9791026 int created = 0 ;
9801027 int examined = 0 ;
1028+ int scanned = 0 ;
9811029 final int attempts = Math .min (budget , pendingRouteCount .get ());
9821030
983- while (examined < attempts && created < maxCreated ) {
1031+ // 'attempts' bounds live route registrations examined for fairness.
1032+ // 'scanned' independently bounds physical queue work, so even tokens
1033+ // made obsolete by a concurrent cancellation cannot turn this helper
1034+ // into an unbounded caller-side scan.
1035+ while (examined < attempts && scanned < budget && created < maxCreated ) {
9841036 final PendingRoute pending = pendingQueue .poll ();
9851037 if (pending == null ) {
9861038 break ;
9871039 }
1040+ scanned ++;
9881041
9891042 final Segment seg = pending .segment ;
9901043 final R route = seg .route ;
9911044
9921045 // A cancellation or timeout can retire a registration while its
993- // token is still in the lock-free queue. Tokens are generation
994- // specific: an old token must never act on a later registration
995- // for the same segment or route.
1046+ // token is concurrently being polled. Tokens are generation
1047+ // specific: an old token must never act on a later registration.
9961048 if (seg .pending .get () != pending ) {
9971049 continue ;
9981050 }
@@ -1004,8 +1056,6 @@ private int serveRoundRobin(final int budget, final int maxCreated, final Segmen
10041056 continue ;
10051057 }
10061058
1007- // Empty registrations are housekeeping, not a fair RR attempt.
1008- // Do not let stale tokens consume the bounded scan budget.
10091059 if (seg .waiters .isEmpty ()) {
10101060 if (seg .pending .compareAndSet (pending , null )) {
10111061 pendingRouteCount .decrementAndGet ();
@@ -1020,16 +1070,12 @@ private int serveRoundRobin(final int budget, final int maxCreated, final Segmen
10201070 examined ++;
10211071
10221072 if (seg == excludedSegment ) {
1023- if (seg .pending .get () == pending ) {
1024- pendingQueue .offer (pending );
1025- }
1073+ requeuePending (pending );
10261074 continue ;
10271075 }
10281076
10291077 if (!tryAllocateOne (route , seg )) {
1030- if (seg .pending .get () == pending ) {
1031- pendingQueue .offer (pending );
1032- }
1078+ requeuePending (pending );
10331079 continue ;
10341080 }
10351081
@@ -1050,9 +1096,7 @@ private int serveRoundRobin(final int budget, final int maxCreated, final Segmen
10501096 }
10511097
10521098 if (!seg .waiters .isEmpty ()) {
1053- if (seg .pending .get () == pending ) {
1054- pendingQueue .offer (pending );
1055- }
1099+ requeuePending (pending );
10561100 } else if (seg .pending .compareAndSet (pending , null )) {
10571101 pendingRouteCount .decrementAndGet ();
10581102
0 commit comments