Skip to content

fix: Shutdown() does not wait for ExecuteCallback goroutines to complete #76

Closed

Description

@youmna-rabie

Description

Shutdown() returns before all ExecuteCallback goroutines have finished. Any code that reads or modifies shared state immediately after Shutdown() returns can race with a still-running callback goroutine.

Steps to Reproduce

var result string

client := amplitude.NewClient(amplitude.NewConfig("api-key"))
// set ExecuteCallback that writes to `result`

client.Track(...)
client.Shutdown()

fmt.Println(result) // data race: callback goroutine may still be writing

Running with go test -race reproduces the race.

Root Cause

In amplitudePlugin.Shutdown(), callbackWg.Wait() is only called in the happy path. When messageChannel is nil (e.g. after the internal start goroutine panics and defers messageChannel = nil), the function returns early without waiting for in-flight callbacks.

Fix

  • Call callbackWg.Wait() in the early-return path so all code paths guarantee callbacks have completed before Shutdown() returns.
  • Fix the regression test: the original test inadvertently established a happens-before chain (write → close → receive → read) that prevented the race detector from ever firing on unfixed code. Swapping the order (close before write) removes the false synchronization.

PR

Fixes #75

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