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 start by exploring the repository structure and finding the relevant files for the deduplication issue.
Assistant
Let me search for the relevant files.
Assistant
Let me see the full file content with line numbers.
Assistant
Now I understand the issue. Let me look at the test file to understand testing conventions.
Assistant
Now I understand the issue. The enable path in
checkStatus()doesn't set the status toRecoveringbefore starting the async cursor open/replay, so a concurrentcheckStatus()can start a second replay. Also, theFailedstatus never retries enabling. Let me fix the enable path.Assistant
Now let me fix the enable path to set
Recoveringbefore starting the async work and to retry fromFailed:Assistant
There's a duplicated
elseblock at lines 248-253. Let me remove the duplicate.Assistant
The file looks good now. Let me verify the full
checkStatus()method to ensure correctness.Assistant
The fix looks correct. Now let me look at the existing test to understand the test setup and write a reproduction test.
Assistant
Let me look at the
openCursorandreplayCursorto understand the flow. TheManagedLedgerUtils.openCursorandManagedLedgerReplayTask.replayare key. Let me look at the existing testcheckStatusFail. The test mocksasyncOpenCursorto complete immediately.Let me write a reproduction test. The reproduction is:
- Keep MessageDeduplication in Initialized or Disabled.
- Call checkStatus() once and delay async cursor open or replay completion.
- Call checkStatus() again before the first recovery finishes.
- Observe that the second call enters the enable path again and starts another replay.
To do this, I need to delay the
asyncOpenCursorcallback. Let me write a temporary test.Let me look at how
openCursorworks in ManagedLedgerUtils.Assistant
Now let me write a reproduction test. I'll create a temporary test file that delays the cursor open callback and checks that a second
checkStatus()call doesn't re-enter the enable path.Let me first compile the project to make sure things work, then write a temporary test.
Assistant
Let me write a temporary test to reproduce the race condition. I'll create a temporary test that delays the openCursor callback.
Assistant
Build succeeded. Let me check the actual test output.
Assistant
The test passes with my fix:
openCursorCalls = 1. Now let me verify the test would fail WITHOUT my fix. Let me temporarily revert the fix and run the test.Assistant
Let me temporarily revert the source change to confirm the test fails without the fix.
Assistant
The test fails without the fix (confirming the bug reproduction). Now let me restore the fix.
Assistant
Now let me run the test again with the fix to confirm it passes.
Assistant
It says
FROM-CACHE. Let me rerun without cache to be sure.Assistant
The test passes with the fix. Now let me run the existing deduplication tests to make sure I haven't broken anything.
Assistant
All existing tests pass. Let me also run the broader deduplication-related tests to ensure nothing else breaks.
Assistant
All tests pass. Let me review the final diff once more and clean up.
Assistant
The temporary test file has been removed and the diff is clean. Let me verify the final state of the modified method one more time.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.