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 tests, then identify the enable/failure transitions and build or run the narrowest relevant test before modifying code.
- Assistant
rgis unavailable, so I’ll usefind/grepto locate the implementation and references. - Assistant
I found the implementation and focused tests. I’ll inspect the state transitions and recovery method in full, plus existing tests that may encode expected lifecycle behavior.
- Assistant
The defect is visible: the enable branch calls
openCursorwhile retainingInitialized/Disabled, and its predicate excludesFailed. The minimal state-machine correction is to setRecoveringsynchronously before cursor open and allowFailedto enter that branch. Before editing, I’ll inspect related status consumers and test infrastructure to ensureFailedhas no distinct terminal semantics and to select a targeted Maven test. - Assistant
No other production logic treats
Failedas terminal. I’ll apply the two state transition changes only: includeFailedwhen policy requires enablement, and assignRecoveringinside the synchronized block before any asynchronous cursor operation can begin. - Assistant
Python is also unavailable; I’ll use the repository editor’s exact text replacement, then inspect the diff and module layout.
- Assistant
The editor located the block but requires it in the displayed window. I’ll open that location and replace the exact three lines.
- Assistant
The diff now matches the requested state-machine behavior and passes whitespace validation. To verify deterministically without adding tests, I’ll inspect the existing test fixture setup and available build commands; then I can use the existing failure test to validate
Recovering → Failed → retry, while compilation and broader class tests catch regressions. - Assistant
This checkout uses Gradle. The existing fixture mocks immediate cursor open, so it covers failure propagation but not delayed overlap. Since repository instructions prohibit adding tests, I’ll run the targeted test as-is and use a temporary test variant outside tracked files only if needed. First I’ll identify the broker project’s Gradle path and test filtering syntax.
- Assistant
I’ll execute the focused TestNG class through Gradle. This validates compilation and the existing asynchronous recovery failure behavior after introducing
Recovering. - Assistant
The focused test class passes. For deterministic race verification without touching repository tests, I can compile a temporary Java harness in
/tmpagainst the already-built broker test runtime classpath. It will mock a cursor open callback that never completes, invokecheckStatus()twice, and assert thatasyncOpenCursoris called exactly once; it can also fail the first open and assert a later check performs a second call. I’ll obtain the runtime classpath via a temporary Gradle init script outside the repository. - Assistant
The temporary classpath task conflicted with Gradle’s configuration cache; I’ll rerun it with that cache disabled, then create the external harness. No tracked files are involved.
- Assistant
I’ll inspect the callback signature and exception constructors, then write the harness. The overlap assertion only needs invocation counts; the retry assertion will complete the first open exceptionally and verify the next check reopens the cursor.
- Assistant
The necessary APIs are available. I’ll now run the external harness with two independent deduplication instances: one held in recovery to prove concurrent checks do not replay twice, and one failed transiently to prove a subsequent check retries.
- Assistant
The fixed harness passes both assertions. To explicitly reproduce the original bug as requested, I’ll compile an untracked copy of this single class with the two changed lines reverted and place it first on the harness classpath. The same harness should then fail because the delayed second check invokes
asyncOpenCursortwice. - Assistant
The old implementation deterministically reproduced the defect with two cursor opens, while the changed implementation passed and also retried after failure. I’ll run the broker Java formatting check and inspect the final tracked diff/status to ensure there are no unintended files.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.