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.
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
I'll start by exploring the repository structure to understand the codebase and locate relevant files. Let's first check what kind of project this is.
- Assistant
This is the Apache Pulsar repository. The issue is about message deduplication recovery race condition. I need to find the
MessageDeduplicationclass and understand the status transitions. Let's search for it. - Assistant
Let me examine the MessageDeduplication.java file to understand the current implementation:
- Assistant
Let me see the complete
checkStatus()method and thereplayCursormethod: - Assistant
Let me get the full file properly:
- Assistant
Let me read the key parts of the file - the
checkStatus()method and thereplayCursor()method. - Assistant
Now let me look at the replayCursor method and the rest of the file:
- Assistant
Now I can clearly see the bug. Let me also check if there's a PR #25953 in the git history:
- Assistant
Let me search for any existing tests related to deduplication status transitions:
- Assistant
Let me search more specifically for the deduplication test files:
- Assistant
Let me check the git log for the related PR (#25953):
- Assistant
Now I have a clear understanding of the bug. Let me examine the complete
checkStatus()method to understand the exact code flow and then implement the fix: - Assistant
Now I understand the bugs clearly. Let me analyze the
checkStatus()method:-
Race condition: When status is
DisabledorInitializedandshouldBeEnabledis true, the code starts an async cursor open and replay but does NOT set the status toRecoveringfirst. This means a concurrent call tocheckStatus()can also enter this branch and start a second replay. -
Failed status retry gap: When status is
Failed, the code falls through to theelsebranch ("Nothing to do"), meaning it never retries even when deduplication should still be enabled.
Let me implement the fix:
-
- Assistant
Good. Let me verify the complete
checkStatus()method to make sure it looks correct: - Assistant
The changes look correct. Let me verify the fix addresses all the issues mentioned:
- Line 230:
status = Status.Recovering;is now set BEFORE starting the asyncopenCursorcall, preventing concurrentcheckStatus()calls from starting another recovery. - Line 227: Added
Status.Failedto the condition, so failed deduplication can be retried when policy still requires it. - Lines 233-236: Removed the redundant
future.completeExceptionally(e)call in the exceptionally handler (the future is already completed exceptionally when the handler runs).
Now let me run the existing tests to make sure nothing is broken:
- Line 230:
- Assistant
Tests pass. Let me run the other deduplication tests as well:
- Assistant
Let me also run the DeduplicationDisabledBrokerLevelTest and TopicDuplicationTest:
- Assistant
All tests pass. Let me also check the TopicDuplicationTest:
- Assistant
All tests pass. Let me verify the diff of my changes:
- Assistant
The diff looks clean and correct. Let me summarize the three changes made:
-
Added
Status.Failedto the enable condition (line 227): This allows retry of deduplication enabling when a previous attempt failed but deduplication is still required by policy. Previously, once status becameFailed, thecheckStatus()method would fall through to the "Nothing to do" branch even when deduplication should still be enabled. -
Set
status = Status.Recoveringbefore starting async cursor open (line 230): This is the key fix for the race condition. By setting the status toRecoveringbefore the asyncopenCursor()call, any concurrentcheckStatus()calls will seeRecoveringstatus and schedule a later check (line 162-165), preventing overlapping dedup recovery replays. -
Removed redundant
future.completeExceptionally(e)(line 236 removed): Theexceptionallyhandler is called when the future is already completed exceptionally, so callingfuture.completeExceptionally(e)on an already-failed future is a no-op. Removing it makes the code cleaner.
Let me now verify the complete checklist:
-
- Assistant
Let me also verify the
replayCursormethod to make sure it still correctly transitions toEnabled: - Assistant
The state machine flow looks correct. Let me verify the complete fix once more by reviewing all the state transitions:
- Recovering → Enabled: When
replayCursorsucceeds (line 275-277), status transitions fromRecoveringtoEnabled✓ - Recovering → Failed: When
openCursororreplayCursorfails (exceptionally handler, line 233-236), status transitions fromRecoveringtoFailed✓ - Failed → Recovering: When
checkStatusis called again withshouldBeEnabledtrue (line 227-228, now includesStatus.Failed), status transitions fromFailedtoRecovering✓ - Initialized/Disabled → Recovering: When
checkStatusis called withshouldBeEnabledtrue (line 227-230), status transitions fromInitialized/DisabledtoRecoveringbefore starting async work ✓ - Recovering → skip + schedule: When
checkStatusis called while status isRecovering(line 162-165), it schedules a later check instead of starting a second recovery ✓
Let me also verify that the fix handles the specific race scenario described in the issue:
- Recovering → Enabled: When
- Assistant
Now I'm confident the fix is correct. Let me do a final check by running one more related test:
- Assistant
That test is in a different package, let me check:
- Assistant
The integration test timed out, but the core unit tests for the modified class all pass. Let me now create my final summary and submit.
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
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →