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 — [fix][client] Fail connection requests on a closed connection pool instead of never completing by merlimat · Pull Request #27514 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Motivation

Problem. After PulsarClient.close() or shutdown(), a request that needs a connection from the client's connection pool returns a future that never completes. That includes PulsarClientImpl.getSchema(), the topic lookups behind getConnection() that producers and consumers connect with, getConnectionToServiceUrl(), the proxy connections, and LookupService.getTopicsUnderNamespace(). A caller blocked on get() without a timeout hangs, and an asynchronous caller is never called back. Creating a producer, consumer or reader isn't affected: those check the client's state first.

The pool resolves the broker address with Netty's InetSocketAddressResolver, which completes its future from a listener on a promise of the client's event loop. That loop has terminated, so Netty can't submit the listener notification and drops it:

ERROR i.n.u.c.D.rejectedExecution - Failed to submit a listener notification task. Event loop shut down?
java.util.concurrent.RejectedExecutionException: event executor terminated
	...
	at io.netty.util.concurrent.DefaultPromise.addListener(DefaultPromise.java:199)
	at io.netty.resolver.InetSocketAddressResolver.doResolveAll(InetSocketAddressResolver.java:71)
	at io.netty.resolver.AbstractAddressResolver.resolveAll(AbstractAddressResolver.java:158)
	at org.apache.pulsar.client.impl.ConnectionPool.resolveName(ConnectionPool.java:457)
	...
	at org.apache.pulsar.client.impl.ConnectionPool.getConnection(ConnectionPool.java:248)
	at org.apache.pulsar.client.impl.BinaryProtoLookupService.getSchema(BinaryProtoLookupService.java:331)
	...
	at org.apache.pulsar.client.impl.PulsarClientImpl.getSchema(PulsarClientImpl.java:1375)

With an event loop shared with other clients through PulsarClientSharedResources, the loop keeps running and the closed pool opens a new connection for the request. The pool's channel group closes it at once, and the request fails with PulsarClientException: java.lang.IllegalStateException: Missing ClientCnx. This should not happen.

Example.

PulsarClientImpl client = (PulsarClientImpl) PulsarClient.builder()
        .serviceUrl("pulsar://localhost:6650")
        .build();
client.close();
// Never completes: TimeoutException
client.getSchema("persistent://public/default/t").get(10, TimeUnit.SECONDS);

The code path:

// PulsarClientImpl.getSchema(): no state check
return lookup.getSchema(topicName);

// BinaryProtoLookupService.getSchema()
client.getCnxPool().getConnection(serviceNameResolver).thenAcceptAsync(clientCnx -> { ... }, lookupPinnedExecutor)

// ConnectionPool: a closed pool still creates a connection
public CompletableFuture<ClientCnx> getConnection(InetSocketAddress logicalAddress,
        InetSocketAddress physicalAddress, final int randomKey) {
    ...
    CompletableFuture<ClientCnx> completableFuture = pool.computeIfAbsent(key, k -> createConnection(key));
    ...
}

CompletableFuture<List<InetSocketAddress>> resolveName(InetSocketAddress unresolvedAddress) {
    CompletableFuture<List<InetSocketAddress>> future = new CompletableFuture<>();
    // Never notified once the event loop has terminated
    addressResolver.resolveAll(unresolvedAddress).addListener((Future<List<InetSocketAddress>> resolveFuture) -> {
        ...
    });
    return future;
}

Modifications

ConnectionPool.getConnection() fails with AlreadyClosedException once the pool is closed, before it looks at the pooled connections or opens a new one; close() sets the flag first.

if (closed) {
    return FutureUtil.failedFuture(
            new PulsarClientException.AlreadyClosedException("Connection pool already closed"));
}

PulsarClientImpl.shutdown(), which close() also runs, closes the pool before it shuts down the event loop, so a request made after that fails at once, whether the client's event loop is its own or shared. The check is in the pool rather than in each client method because every binary-protocol request goes through the pool, including the lookups that BinaryProtoLookupService makes for other client classes. A client closes only a pool it created; a pool passed to its constructor is left to its owner, as before.

ConnectionPoolConnectFailureTest#testImmediateConnectFailureAfterEventLoopTerminated closed its pool before asking for the connection, so it would now pass on the new check. It leaves the pool open now, so it still covers a connection that fails at once after the event loop has terminated.

Not changed here: PulsarClientImpl.getPartitionedTopicMetadata(), and so getPartitionsForTopic(), still doesn't complete on a closed client. It retries the failed lookup on the client's scheduled executor, which has been shut down, and the rejected retry leaves its future pending. That is fixed in #27491.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows:

  • PulsarClientImplTest#testLookupsFailOnClosedClient: on a closed client, getSchema(), getConnection(topic) and getConnectionToServiceUrl() must fail with AlreadyClosedException within 10 seconds, both with the client's own event loop and with an event loop shared through PulsarClientSharedResources. On master, with its own event loop the first one times out, and with a shared one they fail with Missing ClientCnx. This should not happen.

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@merlimat
[fix][client] Fail connection requests on a closed connection pool in…
1223da7
…stead of never completing

After PulsarClient.close() or shutdown(), a lookup that needs a connection
from the client's connection pool never completed: getSchema(), the topic
lookups behind getConnection(), getConnectionToServiceUrl() and the
namespace topic listing all returned futures that stayed pending. The pool
resolves the broker address with Netty's address resolver, which completes
its future through a task on the client's event loop. That loop has
terminated, so Netty logs "Failed to submit a listener notification task.
Event loop shut down?" and drops the notification.

With an event loop shared with other clients, which keeps running, the
closed pool opened a new connection instead, which its channel group
closed at once, and the lookup failed with "Missing ClientCnx. This should
not happen."

ConnectionPool.getConnection() now fails with AlreadyClosedException once
the pool is closed, before it looks at the pooled connections or opens a
new one. The client closes its pool before it shuts down its event loop.
codelipenghui
codelipenghui approved these changes Nov 9, 2026
View reviewed changes
@merlimat
Merge branch 'master' into mmerli/client-closed-connection-pool
e39e6f9
codelipenghui
codelipenghui approved these changes Nov 9, 2026
View reviewed changes
@merlimat
merlimat merged commit 3637479 into apache:master Nov 9, 2026
43 checks passed

Uh oh!

There was an error while loading. Please reload this page.

lhotari pushed a commit that referenced this pull request Nov 11, 2026
@merlimat @lhotari
[fix][client] Fail connection requests on a closed connection pool in…
a8e9989
…stead of never completing (#27514)

(cherry picked from commit 3637479)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 11, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][client] Fail connection requests on a closed connection pool in…
e6df0dc
…stead of never completing (apache#27514)

(cherry picked from commit 3637479)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][client] Fail connection requests on a closed connection pool in…
d868c23
…stead of never completing (apache#27514)

(cherry picked from commit 3637479)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@merlimat @codelipenghui @lhotari
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.
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.