Skip to content

Shutdown is not deterministic - race condition in ExecutorService shutdown #524

Closed

Description

@Mahoney

We're using the analytics client in our tests as so:

// before test
val analytics: Analytics = Analytics
  .builder(writeKey.value)
  .endpoint(wireMockUrl.toASCIIString())
  .build()

// during test
val message = TrackMessage
      .builder("my-event-name")

analytics.enqueue(message)

// before asserting anything
analytics.flush()
analytics.shutdown()

with a mock HTTP client that just gathers all requests.

We are seeing our tests be flaky because sometimes that message never hits the mock server.

Looking at AnalyticsClient.java I'm pretty sure it's possible for the call to AnalyticsClient.shutdown() to shutdown the networkExecutor before the looper has submitted the last batch to it; you never interrogate the Future<?> returned when you call looperExecutor.submit(new Looper()) so the RejectedExecutionException thrown when Looper calls networkExecutor.submit(BatchUploadTask) after the networkExecutor has been shutdown is silently swallowed.

Activity

  1. Mahoney commented on Oct 24, 2025

    @Mahoney
    Author

    I suspect the fundamental issue for us is we are running our tests at a high concurrency level and your thread factory sets the thread priority to minimum for all your executors, so the hardcoded 1 second wait for the looper ExecutorService to exit before shutting down the networkExecutor can be insufficient.

  2. added a commit that references this issue on Dec 2, 2025
  3. MichaelGHSeg commented on Mar 2, 2026

    @MichaelGHSeg
    Contributor

    This should be fixed in the december release. Let us know if there are any further issues. Thanks!

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