Skip to content

ConcurrentModificationException when flushing logs using Synchronicity.ASYNC #2207

@stalar

Description

@stalar

The code below results in a couple of ConcurrentModificationExceptions when run:

public class Repro {

  private static final String PROJECT_ID = "MYPROJECT";
  private static final String LOG_NAME = "MYLOGNAME";

  public static void main(String... args) throws InterruptedException {
    Logging logging = LoggingOptions.getDefaultInstance().getService();
    logging.setWriteSynchronicity(Synchronicity.ASYNC);

    MonitoredResource monitoredResource =
        MonitoredResourceUtil.getResource(PROJECT_ID, null);

    final LogEntry entry = LogEntry.newBuilder(Payload.StringPayload.of("Hello"))
        .setLogName(LOG_NAME)
        .setSeverity(Severity.ERROR)
        .setResource(monitoredResource)
        .build();

    Thread[] threads = new Thread[10];
    for (int i = 0; i < threads.length; i++) {
      threads[i] = new Thread(() -> {
        logging.write(Collections.singleton(entry));
      });
      threads[i].start();
    }

    for (int i = 0; i < threads.length; i++) {
      threads[i].join();
    }
  }
}

Here is the stacktrace:

java.util.ConcurrentModificationException
    at java.util.IdentityHashMap$KeySet.toArray(IdentityHashMap.java:1030)
    at java.util.IdentityHashMap$KeySet.toArray(IdentityHashMap.java:1015)
    at java.util.Collections$SetFromMap.toArray(Collections.java:5463)
    at java.util.ArrayList.addAll(ArrayList.java:577)
    at com.google.cloud.logging.LoggingImpl.flush(LoggingImpl.java:539)
    at com.google.cloud.logging.LoggingImpl.write(LoggingImpl.java:525)
    at com.spotify.logback.Repro.lambda$main$0(Repro.java:35)
    at java.lang.Thread.run(Thread.java:745)

It looks like there is a missing synchronized(writeLock) around this line: https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java#L583

Metadata

Metadata

Labels

api: loggingIssues related to the Cloud Logging API.priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions