Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 15, 2025

This PR fixes a sporadic test failure where LiveMetricsVerifier.getRequestCount() throws a ConcurrentModificationException when iterating over the points ArrayList while another thread is modifying it.

Problem

The issue manifests as:

java.util.ConcurrentModificationException
    at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013)
    at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967)
    at com.microsoft.applicationinsights.smoketest.fakeingestion.LiveMetricsVerifier.getRequestCount(LiveMetricsVerifier.java:46)

Root Cause:

  • The points field is an ArrayList<MonitoringDataPoint> accessed from multiple threads
  • One thread calls apply() to add elements while another thread iterates in getRequestCount(), getExceptionCount(), etc.
  • ArrayList is not thread-safe, causing the concurrent modification exception

Solution

Replace ArrayList with CopyOnWriteArrayList for thread-safe iteration in this read-heavy workload.

Changes made:

  • Changed ArrayList<MonitoringDataPoint> to CopyOnWriteArrayList<MonitoringDataPoint> in the points field
  • Updated imports to include java.util.concurrent.CopyOnWriteArrayList
  • Removed unused ArrayList import

Why CopyOnWriteArrayList:

  • Thread-safe for concurrent read/write operations
  • Optimal for read-heavy workloads (which matches the usage pattern in tests)
  • Provides consistent iteration without ConcurrentModificationException
  • Minimal performance impact for the test framework context

This change ensures that all methods like getRequestCount(), getExceptionCount(), getTraceCount(), etc. can safely iterate over the list while other threads are adding new monitoring data points.

Fixes #4327.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Sporadic test failure Fix ConcurrentModificationException in LiveMetricsVerifier by using CopyOnWriteArrayList Jul 15, 2025
Copilot AI requested a review from trask July 15, 2025 04:50
Copilot finished work on behalf of trask July 15, 2025 04:50
Copilot AI requested a review from trask July 15, 2025 04:55
Copilot finished work on behalf of trask July 15, 2025 04:55
@trask trask marked this pull request as ready for review July 15, 2025 15:25
@trask trask merged commit be7d61a into main Jul 15, 2025
140 checks passed
@trask trask deleted the copilot/fix-4327 branch July 15, 2025 16:39
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.

Sporadic test failure

4 participants