Skip to content

Issue 4797:Prevent thread leak when BookKeeper client constructor fails#4798

Open
kroosxu wants to merge 1 commit into
apache:masterfrom
kroosxu:bkClient-resource-leak
Open

Issue 4797:Prevent thread leak when BookKeeper client constructor fails#4798
kroosxu wants to merge 1 commit into
apache:masterfrom
kroosxu:bkClient-resource-leak

Conversation

@kroosxu
Copy link
Copy Markdown

@kroosxu kroosxu commented May 25, 2026

Descriptions of the changes in this PR:

Fix #4797

Motivation

When BookKeeper.forConfig(conf).build() fails part-way through construction
(for example because metadataServiceUri points to an unreachable or invalid
ZooKeeper ensemble, or MetadataDrivers#getClientDriver(URI) cannot resolve
a driver for the configured scheme), the constructor leaks every resource it
has already allocated up to the point of failure.

Changes

  • In the BookKeeper constructor, when initialization fails after the
    worker pools / bookie client have been created but before the object is
    fully constructed, invoke close() on the partially-initialized instance
    so that all already-allocated resources are
    shut down before the exception is propagated to the caller.

  • Add BookKeeperConstructorFailureTest under
    bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ covering
    the regression:

    • testNoThreadLeakWhenMetadataServiceUnavailable — a single failing
      build() must not leave any client-owned thread behind.
    • testRepeatedFailedBuildsDoNotAccumulateThreads — 20 consecutive
      failing build() calls must not accumulate threads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] BookKeeper client constructor leaks resources (threads / connections) when initialization fails partway

1 participant