Skip to content

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

@Mahoney

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.

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