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. Assistant
Let me analyze this issue. It's about a race condition in message deduplication recovery in what appears to be an Apache Pulsar broker. The issue is:
MessageDeduplicationhas aRecoveringstatus to prevent overlapping transitions- But the enable path starts dedup cursor replay WITHOUT first changing the status to
Recovering - This allows another
checkStatus()call to start a second replay
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me look at the main MessageDeduplication.java file to understand the current implementation.
Assistant
Now I can see the issue clearly. Let me look at the specific
checkStatus()method more carefully:Assistant
Now I can clearly see the issue. Let me look at the exact section of code that needs fixing:
Assistant
Let me see the full checkStatus method and the replayCursor method:
Assistant
Now I fully understand the issue. Let me analyze the fix needed:
Problem 1: In the enable path (lines 227-237), when status is
DisabledorInitializedandshouldBeEnabledis true, the code starts the asyncopenCursorandreplayCursoroperation WITHOUT first settingstatus = Status.Recovering. This allows anothercheckStatus()call to also enter this path and start a second replay.Problem 2: When status is
Failed, the code falls through to theelsebranch ("Nothing to do, we are in the correct state") even whenshouldBeEnabledis true. This means failed attempts to enable deduplication are never retried.Let me look at existing tests to understand the testing approach:
Assistant
Now I have a clear understanding of the issue. Let me verify my understanding by looking at the full enable path:
Assistant
Now I have a clear understanding of the bug and the fix needed. Let me apply the fix:
Issues to fix:
- Race condition: In the enable path,
status = Status.Recoveringis NOT set before starting async operations. This allows concurrentcheckStatus()calls to start multiple replays. - No retry on failure: When status is
FailedandshouldBeEnabledis true, it falls through to "Nothing to do" instead of retrying enable.
Fix:
- Set
status = Status.RecoveringBEFORE starting the async cursor open/replay - Include
Failedstatus in the enable path so it can retry enabling deduplication
- Race condition: In the enable path,
Assistant
Let me use the proper edit command format.
Assistant
Let me try a different approach using bash:
Assistant
Let me use sed with the correct syntax.
Assistant
Now I need to use the edit tool with the proper syntax. Let me look at the tool definition again. The edit tool says I need to use:
codeedit 1:1 "Your Replacement Text Here"or with heredoc:
codeedit 1:1 << 'EOF' Line1 Line2 EOFLet me try using the heredoc approach.
Assistant
Let me use sed to make the two necessary changes:
Assistant
Good. Now let me add the
status = Status.Recovering;line before theopenCursorcall:Assistant
Now I need to add
status = Status.Recovering;after the "// Enable deduping" comment line:Assistant
Let me verify both changes were applied correctly:
Assistant
Both changes have been applied correctly:
- Added
status = Status.Recovering;before starting the async replay - Added
Status.Failedto the condition so that failed attempts can be retried
Let me verify the fix compiles and look at the complete context:
- Added
Assistant
The diff shows exactly the two changes I made:
- Added
Status.Failedto the condition so that if deduplication is in Failed state and should be enabled, it will retry enabling - Added
status = Status.Recovering;before starting the async cursor open/replay operation
This fixes both issues from the bug report. Let me now verify the code compiles and check if there are existing tests:
- Added
Assistant
The maven module name might be different. Let me check the project structure.
Assistant
The command seems to have not produced output. Let me check the project structure differently.
Assistant
This is a Gradle project. Let me compile using Gradle:
Assistant
The compilation succeeded with only warnings (no errors). Now let me check for existing tests related to MessageDeduplication:
Assistant
Let me look at the existing deduplication tests:
Assistant
Let me run the existing deduplication tests to make sure my fix doesn't break anything:
Assistant
All tests passed. Let me also run the other deduplication-related tests to make sure my fix doesn't break anything:
Assistant
All tests passed. Let me verify the fix is complete by looking at the full context of the changed code and also verify the replayCursor method properly transitions the status:
Assistant
Let me continue viewing the rest of the replayCursor method to verify the status transition to Enabled happens correctly.
Assistant
The fix is complete. Let me verify the final diff and provide a summary:
Assistant
The fix has been applied successfully. Let me provide a summary of my reasoning for each checklist item:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.