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
I’ll locate the deduplication state machine and its focused tests, then reproduce the transition logic, patch it, and run the narrowest relevant test suite.
- Assistant
rgis unavailable in this checkout, so I’ll usefind/grepto identify the implementation and tests. - Assistant
I found the implementation and two focused test classes. I’ll inspect the full status transitions and asynchronous enable/recovery path before deciding the minimal synchronization change.
- Assistant
The bug is visible: the enable branch starts
openCursorwithout settingRecovering, while its predicate excludesFailed. The intended fix is likely to setRecoveringsynchronously inside the lock and permitFailedto enter the same enable branch. I’ll inspect history/style and all uses ofFailedto ensure retry semantics don’t conflict with disable behavior. - Assistant
Failedis otherwise terminal and has no special disable handling. I’ll make the enable transition atomic under the existing synchronization: includeFailedonly when policy requires enablement, and assignRecoveringbefore invoking cursor open. This prevents a second check from launching replay and preserves current handling when policy is disabled. - Assistant
Python is also unavailable, so I’ll apply the same small edit with the repository editor command and immediately inspect the diff.
- Assistant
The editor’s displayed line numbers shifted from the prior
sedoutput, causing the first edit to remove the disable branch’sreturn futureand duplicate the cursor-open declaration. I caught this in the diff and will correct the entire branch boundary before testing. - Assistant
The corrected diff now contains only the two intended state-machine changes. Before running tests, I’ll inspect the module’s test configuration and the complete focused test class to choose a command that compiles the affected code without triggering unrelated suites.
- Assistant
This checkout uses Gradle. I’ll run the focused
BrokerMessageDeduplicationTest; it directly exercises enable recovery failure and therefore verifies the changed transition still propagates failures correctly. - Assistant
The focused test passed, including compilation of the broker and test sources. I’ll now verify the patch against the referenced upstream proposal if the remote permits it, and inspect repository cleanliness so no build artifacts or accidental edits are submitted.
- Assistant
There is no configured Git remote, but the working tree is clean except for the intended source edit and
diff --checkpasses. I’ll run the other focused deduplication test class to cover policy-driven enable/disable transitions, which is the production trigger described in the issue. - Assistant
Both focused suites pass. Final review:
- Reproduced reported error/race: The pre-fix code inspection deterministically showed repeated
checkStatus()calls could both satisfy the enable predicate because status remainedInitialized/DisabledwhileopenCursorand replay were pending. No runtime error message was expected. - Confirmed the fix resolves it:
Recoveringis now assigned synchronously while holding the existing monitor, before any asynchronous cursor work starts; subsequent checks hit the transition guard and cannot launch another replay. - Considered edge cases: A failed enable now retries only when policy still requires deduplication. Disabled policy does not accidentally trigger enable. Existing recovery/removal handling and successful
Enabledcompletion remain unchanged. - Ran affected tests:
BrokerMessageDeduplicationTestandTopicDuplicationTestboth passed. They cover recovery failure propagation and policy-driven deduplication transitions. - Repository hygiene: No tests were added or changed; only is modified, and passes.
- Reproduced reported error/race: The pre-fix code inspection deterministically showed repeated
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.