Skip to content

Data race in TestOpenAIInjectedTools test mock #59

@blinkagent

Description

@blinkagent

Description

The race detector is catching a data race in the mockRecorderClient used by TestOpenAIInjectedTools. This appears to be a timing-dependent issue that doesn't always manifest.

Race Detector Output

==================
WARNING: DATA RACE
Read at 0x00c000308c20 by goroutine 57:
  github.com/coder/aibridge_test.TestOpenAIInjectedTools.func1()
      /home/runner/work/aibridge/aibridge/bridge_integration_test.go:848 +0x104
  testing.tRunner()
      /opt/hostedtoolcache/go/1.24.6/x64/src/testing/testing.go:1792 +0x225

Previous write at 0x00c000308c20 by goroutine 729:
  github.com/coder/aibridge_test.(*mockRecorderClient).RecordToolUsage()
      /home/runner/work/aibridge/aibridge/bridge_integration_test.go:1432 +0x184
  github.com/coder/aibridge.(*AsyncRecorder).RecordToolUsage.func1()
      /home/runner/work/aibridge/aibridge/recorder.go:176 +0x15c

Location

  • Test: TestOpenAIInjectedTools in bridge_integration_test.go:848
  • Mock: mockRecorderClient.RecordToolUsage() in bridge_integration_test.go:1432

Analysis

The race is between:

  1. The test goroutine reading from the mock's recorded data
  2. The AsyncRecorder writing to the mock in a background goroutine

The mockRecorderClient likely needs synchronization (mutex) to protect concurrent access to its slices/maps that track recorded calls.

Reproduction

make test-race

Impact

  • Flaky test failures in CI
  • Not a production code issue (only affects test mocks)
  • May cause false negatives if the race is hidden by timing

Fix Suggestions

  1. Add a sync.Mutex to mockRecorderClient
  2. Protect all slice/map operations with the mutex
  3. Consider using a thread-safe data structure or channels

CI Run

Detected in: https://github.com/coder/aibridge/actions/runs/19423680670

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions