mcp: re-enable race test after fixing data races (#851)

1. (non-test) Don't queue notifications if there are no connected
sessions.
2. Change `reconnectInitialDelay` to be atomic, as it is modified in
tests.
3. Protect map access with mutex in `TestServerTransportCleanup`.
This commit is contained in:
Maciej Kisiel 2026-03-23 08:35:27 +01:00 • committed by GitHub
commit 755b9ed4df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -26,6 +26,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/modelcontextprotocol/go-sdk/auth"
@ -1515,9 +1516,13 @@ var (
// reconnectInitialDelay is the base delay for the first reconnect attempt.
//
// Mutable for testing.
reconnectInitialDelay = 1 * time.Second
reconnectInitialDelay atomic.Int64
)
func init() {
reconnectInitialDelay.Store(int64(1 * time.Second))
}
// Connect implements the [Transport] interface.
//
// The resulting [Connection] writes messages via POST requests to the
@ -2196,7 +2201,7 @@ func calculateReconnectDelay(attempt int) time.Duration {
return 0
}
// Calculate the exponential backoff using the grow factor.
backoffDuration := time.Duration(float64(reconnectInitialDelay) * math.Pow(reconnectGrowFactor, float64(attempt-1)))
backoffDuration := time.Duration(float64(reconnectInitialDelay.Load()) * math.Pow(reconnectGrowFactor, float64(attempt-1)))
// Cap the backoffDuration at maxDelay.
backoffDuration = min(backoffDuration, reconnectMaxDelay)