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 I'll implement the fix in
checkStatus(): setRecoveringbefore starting recovery replay, and retry fromFailedwhen the policy still requires a transition. - Assistant
Now let me check the
ManagedLedgerUtils.openCursorhelper to know what to mock in a test: - Assistant
Now let me check the
asyncOpenCursorsignature used: - Assistant
Now add the two unit tests to
MessageDuplicationTest: - Assistant
Both edits are in. Now compile and run the tests. Check the build setup first:
- Assistant
Gradle is still compiling (large build). I'll report back when the monitor fires with the build result.
- Assistant
I'm waiting on the background Gradle test run to complete — it will notify when done. The implementation itself is finished:
Fix in
MessageDeduplication.checkStatus()(pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java:159):- The enable path now sets
status = Status.Recoveringinside the synchronized block before starting the async cursor open + replay, so a concurrentcheckStatus()hits the existingRecoveringguard and cannot start a second overlapping replay. - A
Failedstatus is now retried: it resets toInitializedand re-dispatches, so a transient enable failure no longer leaves dedup permanently disabled while the policy requires it.
Tests in
MessageDuplicationTest:testConcurrentCheckStatusStartsSingleRecovery(deterministic reproduction of the race — captures the open-cursor callback to hold recovery "in progress", asserts the secondcheckStatus()doesn't open a second cursor) andtestCheckStatusRetriesEnableAfterFailure(assertsFailed→ retry →Enabled). - The enable path now sets
- Assistant
The build is still running (first compile of pulsar-broker and its dependencies takes a while; the output is piped through
tail -30so nothing appears until Gradle exits). The background task itself will notify me when it finishes — no need to re-arm the monitor since the file only gets written at completion. I'll report the result then. - Assistant
Build succeeded. Let me confirm both tests actually ran and passed (not silently filtered out):
- Assistant
Both tests ran and passed (
tests="2" failures="0" errors="0"). Let me also run checkstyle on the module to make sure the changes pass lint: - Assistant
Checkstyle is running in the background (should be fast now that compilation is cached). I'll confirm the final result when it completes.
Status so far:
- ✅ Fix implemented in
MessageDeduplication.checkStatus()— entersRecoveringbefore starting recovery replay, and retries fromFailed - ✅ Both new deterministic tests pass:
testConcurrentCheckStatusStartsSingleRecovery,testCheckStatusRetriesEnableAfterFailure(BUILD SUCCESSFUL, 2 tests, 0 failures) - ⏳ Checkstyle verification in progress
- ✅ Fix implemented in
- Assistant
All checks pass. The task is complete.
Summary
Bug:
MessageDeduplication.checkStatus()started dedup cursor recovery replay without first transitioning toRecovering, so a concurrent (e.g. →→ racing with the topic-load chain) could observe / and start a second overlapping replay of the same dedup cursor — corrupting recovered producer sequence state and letting duplicates through. Additionally, a transient enable failure left the status stuck in with no retry.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.