Skip to content

[fix][misc] Avoid deadlock when disposing synthesized TLS subscriptions - #27651

Merged
codelipenghui merged 2 commits into
apache:masterfrom
AuroraTwinkle:fix/tls-subscription-dispose-lock
Nov 23, 2026
Merged

codelipenghui merged 2 commits into
apache:masterfrom
AuroraTwinkle:fix/tls-subscription-dispose-lock

Conversation

@AuroraTwinkle

Copy link
Copy Markdown
Contributor

Fixes #26387

Motivation

A custom TLS factory that supplies only SSLContext can deadlock during concurrent reload and shutdown. The reload callback holds the factory's source lock and waits for the synthesized subscription's monitor, while dispose() 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 in PulsarTlsFactory.

Verifying this change

  • Make sure that the change passes the CI checks.

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 sanityCheck also passes.

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

  • 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

Factory handle disposal runs after releasing the subscription monitor. Callback synchronization is retained.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

@codelipenghui
codelipenghui merged commit 733daa6 into apache:master Nov 23, 2026
43 checks passed
lhotari pushed a commit that referenced this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[fix][misc] PIP-478: lock inversion between a custom PulsarTlsFactory reload callback and TlsContextAcquisition.dispose()

3 participants