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
Duplicate
} else {at line 240-241. Let me fix.Assistant
Good, that fixed the duplicate. Now let's also check the top-level status==Failed and !shouldBeEnabled case — currently falls to else, doing nothing which is fine since Failed+disabled means nothing needed (already effectively disabled/not enabled). Let's verify the full method now looks correct.
Assistant
This test doesn't test concurrency but let's run it to make sure the fix compiles and passes.
Assistant
This project uses Gradle, not Maven. Let's check.
Assistant
Compiled successfully. Now let's run the existing test.
Assistant
Let's check the test report to confirm pass/fail.
Assistant
All 3 tests pass. Now let's write a quick reproduction test to confirm the race is fixed (not to be committed, just temporary).
Assistant
Let's confirm the test would fail without the fix (verify it actually catches the regression).
Assistant
Confirmed: without the fix, the test fails (openCursor called twice — the race is reproduced). Now restore the fix.
Assistant
Now remove my temp test file (per instructions, not to add new tests to repo) and re-run the existing test suite to confirm everything passes with the fix.
Assistant
All 3 tests pass with the fix in place. Let's view the final diff.
Assistant
This is a minimal, targeted fix that addresses both issues described:
-
Race condition fix:
status = Status.Recovering;is now set synchronously (inside thesynchronized(this)block) before starting the asyncopenCursor().thenCompose(replayCursor)chain. Since this assignment happens within the synchronized block before any async operation is kicked off, any concurrentcheckStatus()call will either block until this synchronized block completes (and then seeRecoveringstatus, triggering the retry-scheduling branch instead of starting another replay) or will see the already-updatedRecoveringstatus directly. This closes the window where a second caller could observeInitialized/Disabledand start an overlapping replay. -
Failed retry fix: Added
status == Status.Failedto the enable-path condition, so that when a subsequentcheckStatus()call happens andshouldBeEnabledis true, a previous transient failure will trigger a retry of the enable/recovery flow instead of getting stuck inFailedforever.
Let's go through the checklist.
-
Assistant
Checklist review:
-
If an error was reported in the issue description, I have successfully reproduced it. ✅ I wrote a temporary test (
DedupRaceTest, later removed) that simulated the race by delaying completion and calling twice concurrently. Against the original (unfixed) code, it failed with , confirming the double-replay race described in the issue.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.