GeezerVison is showing github.com — [#17039] Fix inflight NameResolver leak when first caller's client is shut down by 014-code · Pull Request #17048 · netty/netty
Conversation
014-code
commented
Jul 3, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
DnsAddressResolverGroup shares the in-flight resolution maps between all the resolvers of the group, but an entry is removed only when the promise that performs the resolution completes. That promise is bound to the event loop of the caller that started the resolution. If that loop is shut down while the lookup is still in flight (for example EventLoopGroup#shutdownGracefully() during a connect), the promise is never completed, the map slot is retained for the lifetime of the JVM, and every later caller for the same hostname attaches to a dead entry instead of starting a new lookup.
A caller that had already attached before the shutdown was not better off: it was waiting on listeners of the same in-flight promise, whose executor was gone.
Modification:
Replace the promise-valued maps with an InflightEntry that holds the resolver that started the lookup (owner), the in-flight promise, and the promises of the callers that attached to it. No reference counting: a count only tells how many callers are left, while the shutdown path needs the promises themselves to complete them from a thread that is still running.
The entry is released from InflightNameResolver.close(), which AddressResolverGroup already invokes when the event loop of a resolver terminates, instead of relying on the completion of the in-flight promise:
close() sweeps the shared maps, removes the entries owned by this resolver with remove(key, expectedValue), and aborts them;
- aborting completes every attached caller with a
CancellationException from the closing thread, so they are notified on their own executors;
- the in-flight promise is only cancelled when its executor is not shutting down, because a notification for a terminated executor is dropped (and logged) by
DefaultPromise;
- a caller that attaches to an entry that was aborted in the meantime removes the stale slot and retries as the new first caller, so it starts a fresh lookup.
The first caller's promise is still the one handed to the delegate, and the consolidation of concurrent lookups for the same hostname across event loops is preserved.
Result:
Fixes #17039.
A terminated client no longer leaks its in-flight lookups. A later caller for the same hostname starts a new lookup, and a caller that was already waiting is completed with a failure instead of hanging.
Tests: three tests were added to DnsAddressResolverGroupTest (the shutdown of the first client clears the slot and the second client resolves; a pending waiter is completed when the first client shuts down; concurrent lookups from four event loops still result in a single delegate call). All 5 tests in the class pass, and checkstyle and revapi pass.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
* Package-private so that {@link DnsAddressResolverGroup} can declare the in-flight maps
|
|
|
* with the correct value type. Not exposed outside {@code io.netty.resolver.dns}.
|
|
|
*/
|
|
|
static final class InflightEntry<T> {
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just extend AbstractReferenceCounted ?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, done. I changed InflightEntry to extend AbstractReferenceCounted and use Netty's standard refCnt/retain/release machinery.
I kept the small tryAcquire()/tryRelease() helpers because these cleanup paths can race with executor termination and promise completion. Once the entry reaches refCnt == 0, AbstractReferenceCounted will throw on retain/release, while this code needs to treat that as a stale/zombie entry and remove/retry without surfacing an IllegalReferenceCountException from a listener.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair question, and the earlier revision of this PR did extend AbstractReferenceCounted. I removed it again in the latest push because the count does not help with the case this fix is about.
When the executor that owns the resolution terminates, the in-flight promise never completes, so the cleanup/transfer listeners never run: DefaultPromise schedules their notification on that executor, and safeExecute drops it once the executor is terminated. From the terminating side I need the promises of the callers that are still attached, so that I can complete them directly - a refCnt only tells me how many there are. InflightEntry therefore carries the owning resolver plus the list of waiter promises.
The slot is released from InflightNameResolver.close(), which AddressResolverGroup already invokes when the owning executor terminates. That also let me drop the per-resolve terminationFuture listener the previous revision added, which registered one listener per lookup that was never removed.
The current commit is rebased on 4.2 and force-pushed. Happy to go back to the ref-counted shape if you still prefer it, but it would not remove the waiter list.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Hi @014-code, are you still planning to continue this one? I recently fixed a related event-loop lifecycle issue (#18035) and would be happy to help, or pick it up if you've moved on.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Hi @014-code, are you still planning to continue this one? I recently fixed a related event-loop lifecycle issue (#18035) and would be happy to help, or pick it up if you've moved on.
Hi, I am interested in continuing this part of the work. Do you have any suggestions for me
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Sure, a few thoughts:
- I'd start with Norman's review comments on InflightNameResolver from August, since those are what's blocking this.
- On the approach, @ivanchuchulski's point in #17039 seems key: the in-flight resolution shouldn't depend on the first caller's lifecycle. If that caller's client shuts down mid-resolve, the other waiters should still get the result, or at least the map entry should be cleared so the next caller starts a fresh query instead of hanging. That might also cover #17040 and #17042.
- A regression test for exactly that would help a lot: two clients sharing one DnsAddressResolverGroup, shut the first down while its lookup is pending, and assert the second still resolves.
Happy to review or test once you push an update.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
DnsAddressResolverGroup shares the in-flight resolution maps between all the
resolvers of the group, but an entry is only removed when the promise that
performs the resolution completes. That promise is bound to the event loop of
the caller that started the resolution: if that loop is shut down while the
lookup is still in flight, the promise is never completed, the map slot is
retained forever, and every later caller for the same hostname attaches to a
dead entry instead of starting a new lookup.
Modification:
Replace the promise-valued maps with an InflightEntry that knows which resolver
started the lookup and the promises of the callers that attached to it. The
entry is aborted from InflightNameResolver.close(), which AddressResolverGroup
already invokes when the event loop of a resolver terminates: the entry is
removed from the shared map and the attached callers are completed, so they can
no longer hang. A caller that attaches to an entry that has already been
aborted retries as the new first caller and starts a fresh lookup, so a
resolution does not depend on the first caller's lifecycle anymore.
Result:
A terminated client no longer leaks its in-flight lookups, and both the next
caller and the callers that were already waiting make progress: the former
starts a new lookup, the latter are failed instead of hanging. The
consolidation of concurrent lookups for the same hostname is preserved.
|
Picked this back up and pushed a rewrite of the fix (rebased on current 4.2, force-pushed as a single commit), thanks for the pointers.
I also revisited @normanmaurer's comment about AbstractReferenceCounted. The branch already used it, but while reworking the shutdown path I came to the conclusion that a reference count cannot drive it: a count only tells you how many callers are left, while completing them from the terminating thread requires which promises they are. On top of that, listeners registered on the in-flight promise run on the first caller's executor, and once that executor terminates DefaultPromise drops the notification (safeExecute catches the RejectedExecutionException and logs "Failed to submit a listener notification task"). That is why the previous version still leaked the slot and, worse, left an already attached waiter hanging forever. For the same reason I removed the per-resolve terminationFuture listener: it registered one listener per lookup on the event loop's termination future and never removed it, which is a leak of its own.
What the new version does:
InflightEntry holds the resolver that started the lookup (owner), the in-flight promise, and the list of promises of the callers that attached to it. No reference counting.
- The slot is released from
InflightNameResolver.close(). AddressResolverGroup already closes the resolver of an executor when that executor terminates, so the shutdown path is driven by the existing lifecycle instead of by the promise.
close() sweeps the shared maps, removes only the entries that belong to this resolver (remove(key, value)), and aborts them. Attached waiters are completed (cancelled) directly from the closing thread, so they are notified on their own, still running executors. The in-flight promise is only cancelled when its executor is not shutting down; completing it on a dead executor would just log an error.
- A caller that finds an entry that was aborted in that window drops the stale slot and retries as the new first caller, so it starts a fresh lookup instead of failing.
- Cross-event-loop consolidation is unchanged: the first caller's promise is still the one handed to the delegate.
Tests, all in DnsAddressResolverGroupTest and driven through a DnsAddressResolverGroup subclass that overrides newNameResolver (no sleeps, no white-box assertions on the map):
testInflightResolveIsClearedWhenClientEventLoopShutsDown: the DnsAddressResolverGroup prevents future DNS resolution attempts for a hostname if client was terminated during resolution #17039 repro. Client 1 starts a lookup, its loop is shut down while the lookup is pending, client 2 on another loop starts a new lookup and succeeds.
testPendingWaiterIsCancelledWhenFirstClientEventLoopShutsDown: client 2 attaches to the in-flight lookup before client 1 is shut down; its future is completed instead of hanging, and the next lookup goes through.
testInflightResolveIsSharedAcrossEventLoops: four concurrent lookups from four loops still produce a single delegate call and all callers get the result.
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, and mvnw -pl resolver-dns -DskipTests verify (checkstyle + revapi) passes.
One behavioural choice worth calling out: a waiter that was already attached when the owning resolver is closed is cancelled, it is not transparently retried on its own resolver. A retry would have to keep the caller's promise pending across a second lookup; I can add that if you think it is worth the extra state. I also do not think this covers #17040/#17042, those look like DnsCache/TTL lifecycle issues rather than the in-flight map.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I found one more race in the close path: a resolver could pass the initial closed check, insert its entry after close() had already swept the map, and then cancel only its own promise. A concurrent caller that attached in that window was left pending indefinitely. The post-insert closed branch now aborts the whole entry, which also cancels attached waiters. I added a deterministic regression test for this interleaving. DnsAddressResolverGroupTest passes (6 tests), and mvnw -pl resolver-dns verify -DskipTests passes, including Checkstyle and Revapi.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
testlens-app
Bot
commented
Nov 26, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|