Untitled conversation
- User
[Bug] Broker may accept duplicate messages after dedup recovery race
Issue Description
Message deduplication recovery can be started more than once for the same topic before the first recovery replay completes.
MessageDeduplicationdefines aRecoveringstatus to prevent overlapping transitions, but the enable path starts dedup cursor replay without first changing the status toRecovering.Because of that, another
checkStatus()call can still observeInitializedorDisabledand start a second replay for the same dedup cursor.One practical trigger path is topic load with existing topic-level policies:
Time Flow A: existing topic-level policy during load Flow B: normal topic load chain Dedup status / effect T1 initTopicPolicy()loads existing topic policiesdedup status is InitializedorDisabledT2 onUpdate()applies topic policies and callscheckDeduplicationStatus()first dedup recovery replay starts T3 first replay is still running asynchronously status is still InitializedorDisabledT4 topic load continues and BrokerServiceexplicitly callscheckDeduplicationStatus()T5 second check also enters the enable path second replay starts for the same dedup cursor T6 first replay rebuilds producer sequence state second replay also rebuilds producer sequence state overlapping replay can advance shared replay/cursor state Dedup recovery rebuilds producer sequence information from the dedup cursor. If overlapping replay leaves the recovered sequence state incomplete or inconsistent, the broker may fail to identify already-published messages as duplicates.
The impact is that duplicate messages can be accepted after topic load or policy refresh even though message deduplication is enabled.
There is also a retry gap after recovery failure. If enabling deduplication fails transiently, the status moves to
Failed, but later checks do not retry enabling even when the topic policy still requires deduplication.Error messages
- Assistant
Now let me check the
ManagedLedgerUtils.openCursorhelper and howgetStatusis exposed: - Assistant
Let me check
ManagedLedgerReplayTask.replayand the other test file: - Assistant
Now let me run the tests. Let me check the build setup first:
- Assistant
The test run is in the background; I'll wait for its completion notification.
- Assistant
While that runs, let me review the final state of the edited
checkStatus()block and the test file: - Assistant
The main fix looks good. Waiting on the test result now.
- Assistant
Gradle is running. I'll wait for the completion event before reporting results.
- Assistant
The build is still compiling. I'll summarize the change while the test run completes in the background — I'll report the test outcome when the notification arrives.
Fix (
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java:227):- The enable path now transitions to
Status.Recoveringbefore starting the async cursor open + replay, so a concurrentcheckStatus()observes the in-progress transition (line 162 guard) and defers instead of starting a second replay for the same dedup cursor. Status.Failedwas added to the enable-path condition, so a transiently failed recovery is retried on a later check when the policy still requires deduplication.- Removed a no-op
future.completeExceptionally(e)inside theexceptionallyhandler (the future was already exceptionally completed at that point).
Tests (
BrokerMessageDeduplicationTest):checkStatusDoesNotStartConcurrentRecovery— reproduces the race from the issue: holds the cursor-open callback so the first recovery stays in flight, calls again, and verifies is invoked exactly once; then completes recovery and asserts dedup ends up .
- The enable path now transitions to
- Assistant
All 5 tests passed —
BUILD SUCCESSFUL,tests="5" failures="0" errors="0", including both new regression tests and the pre-existing ones.Summary
Root cause: In
MessageDeduplication.checkStatus(), the enable path started the async dedup cursor open + replay whilestatusremainedInitialized/. A second (e.g. from 's topic-load chain racing with → → ) could therefore enter the enable path again and start an overlapping replay of the same cursor, potentially leaving recovered producer sequence state inconsistent and letting duplicates through. Additionally, was a terminal state — enable was never retried after a transient failure.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.