Repository navigation
[fix][misc] Avoid deadlock when disposing synthesized TLS subscriptions - #27651
codelipenghui merged 2 commits into
Conversation
lhotari
left a comment
There was a problem hiding this comment.
LGTM. Thanks for tracking down this lock-order inversion and for the regression tests that pin it.
Moving the underlying dispose() out of the subscription monitor breaks the cycle with a factory that delivers reloads and disposes subscriptions under one lock. The disposed flag is still set under the same monitor as publish(), so no consumer callback can run after dispose() returns. This matches what JettyReloadCoordinator.bind() in JettyTlsFactory.java:597-602 already does on the Jetty path.
I checked that the tests pin both halves of the change with retries disabled. Restoring the previous TlsContextAcquisition.java makes TlsContextAcquisitionTest.disposeDoesNotDeadlockWithReload fail. Dropping the synchronized (this) block makes SslParametersSynthesisTest.disposeWaitsForAnInFlightCallback fail. Both pass at the PR head.
I left one optional suggestion inline.
lhotari
left a comment
There was a problem hiding this comment.
LGTM. Thanks for adding the guard so quickly.
dispose() now checks and sets disposed inside the monitor, so a second caller returns without touching the underlying handle, and jdkHandle.dispose() still runs after the monitor is released. The new repeatedDisposeReleasesUnderlyingHandleOnce and concurrentDisposeReleasesUnderlyingHandleOnce tests in TlsContextAcquisitionTest pin that: without the guard, each would see two calls to the underlying dispose().
Fixes #26387
Motivation
A custom TLS factory that supplies only
SSLContextcan deadlock during concurrent reload and shutdown. The reload callback holds the factory's source lock and waits for the synthesized subscription's monitor, whiledispose()holds that monitor and waits for the factory lock.Modifications
Mark the subscription disposed under the same monitor used by
publish(), then release the underlying factory handle outside the monitor. This retains the wait for an in-flight callback and suppresses late deliveries while removing the inverted lock order. Document the disposal lock rule inPulsarTlsFactory.Verifying this change
Added three regression cases: concurrent reload and disposal, disposal waiting for an in-flight callback, and a deferred companion completing after disposal. The deadlock test uses latches to control the lock ordering and an interruptible factory lock to clean up the failing baseline.
On unpatched master, the deadlock test times out and the two controls pass. With the fix, the selected TLS tests report 105 passed, no failures and two existing TLS 1.3 skips, with retries disabled.
./gradlew sanityCheckalso passes.Does this pull request potentially affect one of the following parts:
Factory handle disposal runs after releasing the subscription monitor. Callback synchronization is retained.