Skip to content

Commit e9668eb

Browse files
committed
fix: Prevent concurrent map writes in retries endpoint
Noticed a few times running generator testing locally the Docker container would crash. From the logs: ``` 2025-01-28 20:42:47 fatal error: fatal error: concurrent map writes 2025-01-28 20:42:47 concurrent map writes 2025-01-28 20:42:47 2025-01-28 20:42:47 goroutine 2978 [running]: 2025-01-28 20:42:47 github.com/speakeasy-api/speakeasy-api-test-service/internal/retries.HandleRetries({0x9a13a0, 0xc0000ffa40}, 0xc000473a40) 2025-01-28 20:42:47 /app/internal/retries/service.go:52 +0x16b ``` This change wraps map writes with a mutex.
1 parent b37a5da commit e9668eb

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

internal/retries/service.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"encoding/json"
55
"net/http"
66
"strconv"
7+
"sync"
78
)
89

9-
var callCounts = map[string]int{}
10+
var (
11+
callCounts = map[string]int{}
12+
callCountsMutex sync.Mutex
13+
)
1014

1115
type retriesResponse struct {
1216
Retries int `json:"retries"`
@@ -45,11 +49,13 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
4549
return
4650
}
4751

52+
callCountsMutex.Lock()
4853
_, ok := callCounts[requestID]
4954
if !ok {
5055
callCounts[requestID] = 0
5156
}
5257
callCounts[requestID]++
58+
callCountsMutex.Unlock()
5359

5460
if callCounts[requestID] < numRetries {
5561
if retryAfter > 0 {
@@ -72,5 +78,7 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
7278
}
7379
_, _ = w.Write(data)
7480

81+
callCountsMutex.Lock()
7582
delete(callCounts, requestID)
83+
callCountsMutex.Unlock()
7684
}

0 commit comments

Comments
 (0)