ISSUE DESCRIPTION [Bug] Broker may accept duplicate messages after dedup recovery race…
- User
ISSUE DESCRIPTION
[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
There may be no error message. This is a race in the dedup recovery state machine.
Reproducing the issue
A deterministic unit-level reproduction can be built by delaying the first dedup recovery replay and invoking
checkStatus()again before the first replay completes:- Keep
MessageDeduplicationinInitializedorDisabled. - Call
checkStatus()once and delay async cursor open or replay completion. - Call
checkStatus()again before the first recovery finishes. - Observe that the second call enters the enable path again and starts another replay.
A production-like trigger path is:
- Enable message deduplication.
- Use a topic with existing topic-level policies.
- Load or reload the topic.
- During topic load,
initTopicPolicy()can invokeonUpdate(), which applies topic policies and callscheckDeduplicationStatus(). - The normal topic load chain later calls
checkDeduplicationStatus()again. - If the first recovery replay is still in progress, both checks can start overlapping dedup replay.
Expected behavior
- Deduplication should enter
Recoveringbefore starting recovery replay. - Concurrent status checks should not start another recovery while replay is in progress.
- If enable fails transiently, a later check should retry enabling when deduplication is still required by policy.
Actual behavior
- Enable starts replay while status remains
InitializedorDisabled. - Another
checkStatus()can start a second replay before the first completes. Failedstatus does not retry enable even when deduplication should still be enabled.
Additional information
A fix has been proposed in #25953.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /pulsar, current file: none) bash-$
- Keep
Assistant
Now let's make the fix: set status = Status.Recovering before starting enable, and allow retry from Failed status.
Assistant
Looks identical. Let's try edit command with line range instead.
Assistant
Edit applied successfully. Now let's verify build compiles.
Assistant
Let's run this test file to confirm nothing breaks.
Assistant
Gradle-based build. Let's run gradle test for this file.
Assistant
Test passes. Let's find test result details to confirm the specific test ran.
Assistant
All 3 tests passed. Now let's write a quick reproduction script to verify the fix addresses the race condition described in the issue.
Assistant
Test passed with the fix. Now let's confirm it actually reproduces the bug by reverting the fix temporarily and running again.
Assistant
Confirmed the bug reproduces without the fix (asyncOpenCursor called 2 times). Now restore fix and remove temp test.
Assistant
The fix is restored, and the temp test file is removed. Let's verify the final diff and run the existing tests one more time.
Assistant
Now let's also check other related test files like DeduplicationDisabledBrokerLevelTest to be safe.
Assistant
All tests pass. Let's also double check the final state of the file and git status.
Assistant
I've completed the fix. Let me go through the checklist:
-
Reproduced the error: I created a temporary test (
DedupRaceTest.java, later removed) that mocked a delayedasyncOpenCursorcallback and calledcheckStatus()twice before the first completed. Without the fix,asyncOpenCursorwas invoked twice (confirmed viagit stashto temporarily revert my fix and rerun the test, which failed with "Wanted 1 time... But was 2 times"). -
Confirmed the fix resolves the error: After restoring the fix, the same test passed, confirming
asyncOpenCursoris called exactly once even with concurrentcheckStatus()invocations. -
Thought about edge cases:
- The fix sets
status = Status.Recoveringsynchronously (within the existingsynchronized(this)block) before the asyncopenCursor/replayCursorchain starts, so any subsequentcheckStatus()call seesRecoveringand defers via the existing reschedule logic (pulsar.getExecutor().schedule(this::checkStatus, 1, TimeUnit.MINUTES)), instead of racing into the enable path again. - I also added
Status.Failedto the enable-path condition so that if a previous enable attempt failed transiently, a latercheckStatus()call will retry enabling deduplication when it's still required by policy, addressing the "retry gap" mentioned in the issue. - The
exceptionallyhandler still correctly transitions back toStatus.Failedon error, preserving existing failure semantics.
- The fix sets
-
Ran existing tests: I ran
BrokerMessageDeduplicationTest(all 3 tests pass, includingcheckStatusFailwhich exercises the failure path) andDeduplicationDisabledBrokerLevelTest(passes), confirming no regressions.
The final diff is minimal and targeted:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →