Shopify / Shopify/toxiproxy

cmd/server: SIGINT during initialization can cause a data race in ApiServer

Open
#667 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
12.4k
Forks
510
PR merge metrics
No merged PRs in 30d

Description

I ran:

; go run -race ./cmd/server
<first log line>
<ctrl-c (SIGINT)>

and then saw:

; go run -trimpath -race ./cmd/server
# github.com/Shopify/toxiproxy/v2/cmd/server
ld: warning: '/private/var/folders/db/svmm3t1x3yn4d1skpbq3ddv00000gn/T/go-link-1052203798/000013.o' has malformed LC_DYSYMTAB, expected 98 undefined symbols to start at index 1626, found 95 undefined symbols starting at index 1626
{"level":"info","version":"git","caller":"server.go:78","time":"2025-09-04T02:46:27Z","message":"Starting Toxiproxy"}
{"level":"info","address":"localhost:8474","caller":"api.go:57","time":"2025-09-04T02:46:27Z","message":"Starting Toxiproxy HTTP server"}
^C{"level":"info","caller":"server.go:104","time":"2025-09-04T02:46:38Z","message":"Shutdown started"}
==================
WARNING: DATA RACE
Read at 0x00c00028a6d8 by main goroutine:
  github.com/Shopify/toxiproxy/v2.(*ApiServer).Shutdown()
      github.com/Shopify/toxiproxy/v2/api.go:76 +0x3c
  main.run()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:105 +0xba0
  main.main()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:54 +0x20

Previous write at 0x00c00028a6d8 by goroutine 11:
  github.com/Shopify/toxiproxy/v2.(*ApiServer).Listen()
      github.com/Shopify/toxiproxy/v2/api.go:59 +0x1c0
  main.run.func1()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:95 +0x48
  main.run.gowrap1()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:99 +0x94

Goroutine 11 (running) created at:
  main.run()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:94 +0xac8
  main.main()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:54 +0x20
==================
==================
WARNING: DATA RACE
Write at 0x00c000406090 by main goroutine:
  ??()
      -:0 +0x1031e61d8
  sync/atomic.StoreUint32()
      <autogenerated>:1 +0x14
  net/http.(*Server).Shutdown()
      net/http/server.go:3180 +0x48
  github.com/Shopify/toxiproxy/v2.(*ApiServer).Shutdown()
      github.com/Shopify/toxiproxy/v2/api.go:83 +0x98
  main.run()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:105 +0xba0
  main.main()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:54 +0x20

Previous write at 0x00c000406090 by goroutine 11:
  github.com/Shopify/toxiproxy/v2.(*ApiServer).Listen()
      github.com/Shopify/toxiproxy/v2/api.go:59 +0xa4
  main.run.func1()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:95 +0x48
  main.run.gowrap1()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:99 +0x94

Goroutine 11 (running) created at:
  main.run()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:94 +0xac8
  main.main()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:54 +0x20
==================
==================
WARNING: DATA RACE
Write at 0x00c0004060b8 by main goroutine:
  ??()
      -:0 +0x1031eb900
  sync/atomic.CompareAndSwapInt32()
      <autogenerated>:1 +0x14
  sync.(*Mutex).Lock()
      sync/mutex.go:46 +0x2c
  net/http.(*Server).Shutdown()
      net/http/server.go:3182 +0x58
  github.com/Shopify/toxiproxy/v2.(*ApiServer).Shutdown()
      github.com/Shopify/toxiproxy/v2/api.go:83 +0x98
  main.run()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:105 +0xba0
  main.main()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:54 +0x20

Previous write at 0x00c0004060b8 by goroutine 11:
  github.com/Shopify/toxiproxy/v2.(*ApiServer).Listen()
      github.com/Shopify/toxiproxy/v2/api.go:59 +0xa4
  main.run.func1()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:95 +0x48
  main.run.gowrap1()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:99 +0x94

Goroutine 11 (running) created at:
  main.run()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:94 +0xac8
  main.main()
      github.com/Shopify/toxiproxy/v2/cmd/server/server.go:54 +0x20
==================
Found 3 data race(s)
exit status 66

This is caused due to unsynchronized access to the underlying *http.Server held on *ApiServer.
A follow-up PR coming up with a fix.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with api.go at the ApiServer.Listen and Shutdown paths, then inspect cmd/server/server.go where initialization and SIGINT shutdown run concurrently. Reproduce the issue with go run -race ./cmd/server and verify that interrupting during initialization no longer reports races.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.