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. The bug is about a race condition in the message deduplication recovery mechanism. The key problem 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 observeInitializedorDisabledand start a second replay
Let me first explore the repository structure to understand the codebase and find the relevant files.
Assistant
Let me look at the MessageDeduplication.java file first to understand the current implementation.
Assistant
I need to use the bash_terminal tool to read the file instead.
Assistant
Now I can see the issue clearly. Let me examine the
checkStatus()method more closely. The problem is in the enable path where status changes fromDisabledorInitializedtoEnabled- it doesn't first set status toRecoveringbefore starting the async replay.Let me look at the exact line numbers:
Assistant
Let me see the
replayCursormethod and the surrounding code:Assistant
Now I understand the issue completely. Let me analyze it:
The Bug:
- In
checkStatus(), when enabling deduplication (status isDisabledorInitializedandshouldBeEnabledis true), the code starts the async cursor open/replay WITHOUT first setting the status toRecovering. - The check at line 162 guards against concurrent transitions by checking for
RecoveringorRemoving, but since status isn't set toRecoveringbefore starting the replay, anothercheckStatus()call can also enter the enable path. - Additionally, when status is
Failedand deduplication should still be enabled, there's no retry logic - it falls through to "Nothing to do".
The Fix:
- Set
status = Status.RecoveringBEFORE starting the async cursor open/replay in the enable path - Handle the
Failedcase - if deduplication should be enabled but status isFailed, retry enabling
Let me also check the existing tests to understand how this is tested:
- In
Assistant
Let me look at the existing tests to understand how to test this:
Assistant
Now I have a complete understanding of the issue. Let me implement the fix:
The Bug:
- In
checkStatus(), when enabling deduplication (status isDisabledorInitializedandshouldBeEnabledis true), the code starts the async cursor open/replay WITHOUT first setting the status toRecovering. - This allows another
checkStatus()call to also enter the enable path and start a second replay. - Additionally, when status is
FailedandshouldBeEnabledis true, there's no retry logic - it falls through to "Nothing to do".
The Fix:
- Set
status = Status.RecoveringBEFORE starting the async cursor open/replay - Handle the
Failedcase - if deduplication should be enabled but status isFailed, retry enabling
- In
Assistant
Now I understand the exact fix needed. Let me edit the
checkStatus()method to:- Set
status = Status.RecoveringBEFORE starting the async cursor open/replay - Handle the
Failedcase - if deduplication should be enabled but status isFailed, retry enabling
- Set
Assistant
Now I'll apply the fix. I need to:
- Set
status = Status.RecoveringBEFORE starting the async cursor open/replay in the enable path - Handle the
Failedcase - if deduplication should be enabled but status isFailed, retry enabling
- Set
Assistant
Let me apply the fix directly by editing the file:
Assistant
Let me apply the fix using the edit command:
Assistant
I need to use sed or another approach since python3 is not available. Let me use a different approach.
Assistant
Python is not available, so I'll need to use sed or other bash tools to make the changes.
Let me think about what changes I need to make:
-
In the enable path (status == Disabled || Initialized && shouldBeEnabled), I need to add
status = Status.Recovering;BEFORE starting the async cursor open/replay. -
I need to add a new case for
status == Failed && shouldBeEnabledto retry enabling.
Let me use sed to make these changes.
-
Assistant
Perl is available. Let me use it to make the changes:
Assistant
Let me use a simpler approach with sed:
Assistant
Let me use sed to add the
status = Status.Recovering;line after the "// Enable deduping" comment.Assistant
Now let me verify the change and add the retry logic for the Failed status:
Assistant
Good, the first fix is applied. Now I need to add the retry logic for the
Failedstatus. Let me add a new branch after the first enable block:Assistant
Now I need to add the retry logic for
Failedstatus. I'll insert it before the finalelseclause (line 239):Assistant
Let me verify the change:
Assistant
The changes look correct. Let me verify the entire
checkStatus()method to make sure everything is correct:Assistant
The changes look correct. Now let me verify the
replayCursormethod to ensure the status transitions properly:Assistant
The code looks correct. Let me verify the fix by checking the complete flow:
-
Race condition fix: When enabling deduplication,
status = Status.Recoveringis now set BEFORE starting the async cursor open/replay. This means any concurrentcheckStatus()call will seeRecoveringand schedule a later check (line 162-166), preventing overlapping replays. -
Retry fix: Added a new branch for
status == Status.Failed && shouldBeEnabledthat retries enabling deduplication by setting status toRecoveringand starting the replay again.
Let me verify the code compiles by checking for any Java syntax issues:
-
Assistant
Let me check the overall structure of the file to make sure the syntax is correct:
Assistant
Let me check if there are any compilation issues by looking for the build system:
Assistant
Let me check the build system.
Assistant
This is a Gradle project. Let me verify my changes compile correctly and run any relevant tests.
Assistant
The diff looks correct. Let me run the relevant tests to confirm the changes don't break anything:
Assistant
The tests passed. Let me run more deduplication-related tests to ensure the fix doesn't break anything:
Assistant
All tests are passing. Let me run a couple more deduplication-related tests:
Assistant
All tests pass. Let me verify the complete fix one more time:
Assistant
The fix is complete. Let me summarize what was done:
Summary of Changes
The bug was in
MessageDeduplication.javain thecheckStatus()method. There were two issues:Issue 1: Race condition during dedup recovery
Problem: When enabling deduplication (status transitions from
DisabledorInitializedto enable the dedup cursor), the code started the async cursor open/replay without first setting the status toRecovering. This allowed a concurrentcheckStatus()call to observe the still-old status and start a second overlapping replay.Fix: Added
status = Status.Recovering;before starting the async cursor open/replay. Now whencheckStatus()seesRecoveringstatus (line 162), it will schedule a later check instead of starting another replay, preventing duplicate replays.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.