Skip to content

ARTEMIS-6362 A send refused after its duplicate id check leaves the duplicate id cache as it was - #6883

Open
grepjava wants to merge 1 commit into
apache:mainfrom
grepjava:ARTEMIS-6362
Open

grepjava wants to merge 1 commit into
apache:mainfrom
grepjava:ARTEMIS-6362

Conversation

@grepjava

@grepjava grepjava commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

For a send outside a transaction, PostOfficeImpl.checkNotBridgeDuplicateID starts a transaction and calls addToCache(duplID, tx, true), which put the id in the cache at once, overwriting the oldest slot of a full cache, and registered an AddDuplicateIDOperation that deleted the new id on rollback. When routing then failed (an address full under FAIL, say), the rollback took the new id out, but the id it had overwritten was gone: sending that older message again was delivered.

Both caches now keep such an id aside, in a count of ids in flight that contains also checks, so a concurrent send of it is still a duplicate. It takes its slot in beforeCommit, once the send was routed, and a rollback just releases it, so a send that fails leaves the cache as it was. The bridge's atomicVerify goes the same way.

DuplicateDetectionTest.testRefusedSendKeepsTheIdBeforeIt uses an address with id-cache-size 1 and FAIL: "d1" is accepted, the address is filled, "d2" is refused, and once the queue is drained "d1" sent again must be dropped as a duplicate. It fails without the fix.

https://issues.apache.org/jira/browse/ARTEMIS-6362

@clebertsuconic

Copy link
Copy Markdown
Contributor

I see... this is where the duplicate came from?

@grepjava

Copy link
Copy Markdown
Contributor Author

Not quite, this one is a separate path. Here the problem is the slot an ID overwrites, not an ID held twice. A send outside a transaction put its ID in the cache straight away, overwriting the oldest slot of a full cache. If routing then failed (an address full under FAIL), the rollback took the new ID out, but the older ID it had overwritten was already gone, so sending that older message again got delivered. With this change the ID waits in an in-flight count until the send is routed. contains sees that count, so a concurrent send is still a duplicate, and a failed send leaves the cache as it was.

@grepjava

Copy link
Copy Markdown
Contributor Author

FYI #6904 (ARTEMIS-6366) builds on this one: it holds a transaction's ids in the same in-flight count from its prepare or commit. I'll rebase it once this is merged.

@jbertram

jbertram commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Here's some feedback from a Claude review which I looked over and approved...

There's one problem with the reservation lifecycle, plus two smaller ones that share its root cause.

The reservation leaks when the broker-started transaction is abandoned.

release() only runs from beforeCommit/beforeRollback. In PostOfficeImpl.route, the transaction started by the duplicate check (line 1189) is explicitly rolled back only for ActiveMQAddressFullException (line 1267). But checkAddress() (line 1212) and simpleRoute() (line 1224) both run after the reservation is made, and both throw addressDoesNotExist through a path that only calls markAsRollbackOnly(ex) and rethrows (lines 1297 and 1315). markAsRollbackOnly fires no operation callbacks, so that TransactionImpl is dropped unreferenced and release() never runs.

Before this change the id went into the ring buffer, so a failed send left a bounded, evictable entry. Now the inFlight entry is permanent:

  • a non-transacted producer sends with _AMQ_DUPL_ID=X to a non-existent address with auto-create-addresses=false
  • the send fails
  • contains(X) is true from then on, so the same message resent after the address exists is silently dropped as a duplicate
  • a client that keeps retrying grows inFlight without bound

TransactionImpl.addOperation looks to have the same hole on its COMMITTED/ROLLEDBACK fast paths. Releasing from afterRollback (or a completion hook) would cover it, or the route path could roll the started transaction back on any exception rather than only address-full.

Two smaller ones, same root cause — inFlight isn't treated as part of the cache's state:

  • clear() empties ids and cache but not inFlight, so a cleared cache still reports reserved ids as duplicates, and load()'s "valid only on empty cache" guard no longer reflects the full state.
  • deleteFromCache() doesn't consult inFlight: deleting an id that's reserved but not yet committed silently no-ops while contains() keeps returning true, and the later commit re-adds it. Reachable from MQTTPublishManager.removePacketIdFromCache, though I couldn't find a live MQTT failure since handlePublish commits synchronously under its monitor.

Both apply to InMemoryDuplicateIDCache as well as PersistentDuplicateIDCache.

Two minor notes, take or leave:

  • TransactionImpl.commit runs beforeCommit() before doCommit(), so if storage fails the id is already in the cache and the following rollback can't take it back out — the old deleteFromCache(holder, null) could. Narrow, but a behavior change on the rollback path.
  • In testRefusedSendKeepsTheIdBeforeIt, assertTrue(sent < 20, ...) sits after sent++ inside the try, so it's evaluated an iteration later than it reads. Harmless, but it would be clearer above the send.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants