[fix][fn] Make exclusiveLeaderProducer volatile in FunctionMetaDataManager - #26046
Merged
lhotari merged 1 commit intoJun 17, 2026
Merged
Conversation
…aDataManager exclusiveLeaderProducer is written by acquireLeadership(), which is intentionally not synchronized (it waits for the metadata tailer thread to drain, and holding the monitor there would deadlock with the tailer's synchronized processUpdate/processDeregister). The field is then read by the synchronized methods updateFunctionOnLeader(), start() and giveupLeadership() as the sole "am I the leader?" signal. Because the write happens without holding the monitor, there is no happens-before edge to those synchronized reads, so the Java Memory Model permits a reader to observe a stale null and spuriously throw "Not the leader". Marking the field volatile establishes the missing happens-before edge, matching how the sibling cross-thread field lastMessageSeen is already declared.
lhotari
approved these changes
Jun 17, 2026
sandeep-ctds
pushed a commit
to datastax/pulsar
that referenced
this pull request
Jul 31, 2026
…nager (apache#26046) Co-authored-by: maxlisongsong <maxlisongsong@didiglobal.com> (cherry picked from commit b8b1e2c)
nodece
pushed a commit
to ascentstream/pulsar
that referenced
this pull request
Aug 28, 2026
…nager (apache#26046) Co-authored-by: maxlisongsong <maxlisongsong@didiglobal.com> (cherry picked from commit b8b1e2c)
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.
PR Description
Motivation
In
FunctionMetaDataManager, theexclusiveLeaderProducerfield serves adouble duty: a non-null value means this worker is the leader, a null value
means it is not. It is the sole signal the write path uses to decide whether
this worker may publish to the function metadata topic.
The field is written by
acquireLeadership(), which is intentionally notsynchronized: it waits for the metadata tailer thread to drain(
tailer.stopWhenNoMoreMessages().get()), and holding the monitor there woulddeadlock with the tailer thread, which calls the
synchronizedprocessUpdate()/processDeregister(). The field is then read by thesynchronizedmethodsupdateFunctionOnLeader(),start()andgiveupLeadership().Because the write in
acquireLeadership()does not hold the monitor, there isno happens-before edge between that write and the synchronized reads. Under the
Java Memory Model this permits a reader to observe a stale
nullafterleadership has actually been acquired, and
updateFunctionOnLeader()would thenspuriously throw
IllegalStateException("Not the leader"). The field is plain(non-volatile), unlike the sibling cross-thread field
lastMessageSeen, whichis already declared
volatile.Modifications
FunctionMetaDataManager.exclusiveLeaderProducerasvolatile.This establishes the missing happens-before edge between the unsynchronized
write in
acquireLeadership()and the synchronized reads, so the leadershipsignal is always published safely. The change is limited to the field modifier;
no logic, locking, public API, or threading model is changed, and it is
consistent with how
lastMessageSeenis already handled in the same class.Verifying this change
This change is a trivial correctness/hardening fix without dedicated test
coverage. The defect is a Java Memory Model visibility hazard that cannot be
reliably reproduced by a unit test: the reads happen inside
synchronizedblocks (acquire fence + field reload) and the JVM runs on cache-coherent
hardware, so a failing reproduction cannot be constructed deterministically or
intermittently. The fix is verified by inspection against the JMM and is
covered by the existing
FunctionMetaDataManagerTestfor behavioral regression.Does this pull request potentially affect one of the following parts: