[fix][test] Make LocalBookkeeperEnsemble robust to port race and partial-setup cleanup#25690
Open
merlimat wants to merge 3 commits intoapache:masterfrom
Open
[fix][test] Make LocalBookkeeperEnsemble robust to port race and partial-setup cleanup#25690merlimat wants to merge 3 commits intoapache:masterfrom
merlimat wants to merge 3 commits intoapache:masterfrom
Conversation
…ial-setup cleanup `ShadowTopicRealBkTest.setup` occasionally fails with `BindException: Address already in use`: `PortManager.nextLockedFreePort()` only locks the port at the JVM level, so another process can grab the port between allocation and the ZK bind. When that happens, the subsequent `cleanup()` call also fails with NPE because `LocalBookkeeperEnsemble.stop()` dereferences `bookieComponents` (and other fields) that are still null from the aborted setup. * `LocalBookkeeperEnsemble.runZookeeper` now retries on `BindException` up to 5 attempts and falls back to a kernel-assigned port (port 0) on the last try. Callers must read the actual bound port via `getZookeeperPort()` after start. * `LocalBookkeeperEnsemble.stop` null-guards every field it touches so cleanup never NPEs when setup failed mid-way. * `ShadowTopicRealBkTest` now reads the bound ZK port back from the ensemble after start, so the metadata store URL stays correct even when the retry fallback kicks in. The cleanup path also tolerates a partially-initialized PulsarService.
Drop the BindException retry loop in LocalBookkeeperEnsemble.runZookeeper and instead pass 0 for the ZK port from the test. The kernel guarantees a free port at bind time, so there is no race window. The actual bound port is read back via bk.getZookeeperPort(). The cleanup null-safety fix in LocalBookkeeperEnsemble.stop() is kept because any partial-init failure (not just port bind) would otherwise NPE the cleanup.
dao-jun
approved these changes
May 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
ShadowTopicRealBkTest.setupflakes intermittently withBindException: Address already in use:PortManager.nextLockedFreePort()only locks the port inside the JVM's static set — another process can grab the OS-level port between allocation and the ZK bind.When that happens,
cleanup()also fails with NPE becauseLocalBookkeeperEnsemble.stop()dereferencesbookieComponents(and other fields) that the aborted setup left null:Failing CI: https://github.com/apache/pulsar/actions/runs/25387331732/job/74455050956
Modifications
ShadowTopicRealBkTest: pass0for the ZK port so the kernel picks a free port at bind time. This eliminates the JVM-vs-OS race entirely. The actual bound port is read back viabk.getZookeeperPort()after start.LocalBookkeeperEnsemble.stop: null-guardbookieComponents,zkc,zks,serverFactory, andstreamStorage, and wrap each shutdown in try/catch so cleanup never NPEs after a partial setup (the port-bind failure was one trigger, but any setup failure could leave fields uninitialized).ShadowTopicRealBkTest: tolerate a partially-initializedPulsarServicein cleanup.Verifying this change
ShadowTopicRealBkTestpasses locally with the change.Does this pull request potentially affect one of the following parts: