Skip to content

[Bug] Flaky test: InboxMiddlewareTest hits ConcurrentModificationException on the mock's request list #4132

Closed

Description

@ningmao-hlyz

Describe the bug

InboxMiddlewareTest.notificationReachesModelRequestUnderRealScheduling fails intermittently on CI:

java.util.ConcurrentModificationException
  at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1604)
  at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
  at io.agentscope.harness.agent.middleware.InboxMiddlewareTest.notificationReachesModelRequestUnderRealScheduling(InboxMiddlewareTest.java:311)

MockModel.recordedMessages is a plain ArrayList, appended in stream(...) from whichever
thread issues the model call. The test reads it through getRecordedMessages() right after the
turn returns, while the background memory-maintenance turn may still be issuing a model call —
the test's own comment says so:

Background memory maintenance reuses this model for consolidation requests after the call
returns, so the last recorded request races that pipeline under load

That append invalidates the iterator the test is draining.

To Reproduce

No threads required — hold an iterator, record one more request, then keep draining:

MockModel model = new MockModel("ack");
model.stream(List.of(probe), null, null).blockLast();

Iterator<List<Msg>> inFlight = model.getRecordedMessages().iterator();
inFlight.next();
model.stream(List.of(probe), null, null).blockLast();   // recorded mid-iteration
while (inFlight.hasNext()) inFlight.next();              // ConcurrentModificationException

Observed frequency

  • 2 of the 6 most recent failed Java CI with Maven runs hit this test (43013023879,
    43013520954). The same commits passed the windows job, so it is load-dependent.
  • Locally (JDK 17, Windows) 12/12 runs pass; the CI load is what exposes it.

Expected

Reading the recorded requests must not throw because another thread recorded a request.

Fix

Record into a CopyOnWriteArrayList (append-only, read-mostly) and pin the interleaving with a
test that records a request midway through an iteration; against the old ArrayList it throws the
same ConcurrentModificationException.

PR: #4133

Activity

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

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