Skip to content

[Bug] Topic candidate selection races with concurrent cache eviction #10892

Description

@Aias00

Runtime platform environment

All platforms; reproduced by a deterministic unit test on the current develop branch.

RocketMQ version

develop at e3458616d207ee636b1762f0f8dcf788a590d59d.

Describe the Bug

Both the Java producer and Proxy availability detectors select a cached topic by first checking whether a concurrent map is empty and then creating a new iterator and immediately calling next().

If the final cache entry is evicted between the two operations, next() throws NoSuchElementException. The client path is DefaultMQProducerImpl.pickTopic; the Proxy has the same pattern in TopicRouteService.pickTopic.

Steps to Reproduce

  1. Populate the producer topic cache with one entry.
  2. Simulate eviction immediately after isEmpty() returns false.
  3. Invoke pickTopic through the availability detector helper.
  4. Observe NoSuchElementException.

Expected Behavior

A cache that becomes empty during candidate selection returns an empty Optional, causing the detector to report no candidate rather than throwing.

Actual Behavior

Candidate selection throws before the detector can return a normal unavailable result.

Proposed Fix

Use one iterator and check hasNext() before calling next() in both Client and Proxy implementations. Add a regression test for the Client implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions