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 explore the repository structure and find the relevant files for this issue.
Assistant
Let me explore the repository structure and find the relevant files for this issue.
Assistant
Let me see the full file, especially the
replayCursormethod and surrounding area.Assistant
Let me look at the test files for MessageDeduplication and the related PR.
Assistant
Let me look at the test files and related callers.
Assistant
Now let me look at the BrokerService call and find the existing tests for this class.
Assistant
Now I have a clear understanding of the issue. Let me look at the exact
checkStatus()method lines to make the edits.Assistant
Now I understand the issue. Let me fix the
checkStatus()method. The key changes are:- Set
status = Status.Recoveringbefore starting the enable replay (so concurrent checks won't enter the enable path again). - Add
Status.Failedto the enable condition so it retries enabling when deduplication should be enabled. - Handle
Status.Failed && !shouldBeEnabledto clean up toDisabled.
Let me make the edits:
- Set
Assistant
Let me use the edit command directly. Let me open the file first.
Assistant
Now let me edit the first block (Initialized removal) to also handle Failed and return a future properly:
Assistant
Let me verify the changes:
Assistant
Let me check the
isEnabled()method and verify the test still works:Assistant
The changes look good. Now let me check if there are other tests that might be affected by these changes, particularly around status transitions:
Assistant
Let me check the BrokerService caller and any tests checking deduplication status transitions during load:
Assistant
Now let me write a reproduction test to confirm the fix. First, let me check the build setup:
Assistant
This is a Gradle project. Let me try compiling the broker module to verify the code compiles:
Assistant
The offline mode can't find plugins. Let me try without offline:
Assistant
The code compiles successfully. Now let me write a reproduction test to verify the fix addresses the race condition. Let me create a temporary test:
Assistant
The
Statusenum is package-private. Let me usetoString()comparison instead:Assistant
getStatus()returns the enum. Let me compare with.toString():Assistant
There are still 3 errors. Let me look at what they are.
Assistant
The enum is package-private, so I can't call methods on it directly from outside the package. Let me use
String.valueOf:Assistant
The test passes with my fix. Now let me verify the test would FAIL without the fix (to confirm it actually catches the bug). Let me temporarily revert the fix and run:
Assistant
The test fails without the fix (status was still
Initialized/Disabledduring recovery) and passes with the fix. Now let me add a test for the retry-after-failure case to verify that part of the fix too:Assistant
The append placed the method outside the class braces. Let me fix the file structure:
Assistant
Let me rewrite the test file cleanly:
Assistant
Both tests pass. Now let me run the existing dedup tests to make sure I haven't broken anything:
Assistant
All existing tests pass. Let me also run the broker-level dedup test to be thorough:
Assistant
Now let me clean up the temporary reproduction test file since I shouldn't add new tests to the repository:
Assistant
Let me review the final diff to make sure everything is clean:
Assistant
The diff is clean and addresses all three requirements from the issue. Let me verify no stray files remain and the final state:
Assistant
Let me do a final review of the complete fixed method:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.