fix: eliminate race condition in shutdown by setting gracefulShutdown flag earlier (#2628)
When shutdown was initiated, there was a race condition where new prediction
requests could slip through before the gracefulShutdown flag was set, causing
them to be rejected with 409 (Conflict) instead of 503 (Service Unavailable).
The issue occurred because:
1. Service.Shutdown() closed the shutdown channel
2. A goroutine picked this up and called handler.Stop()
3. handler.Stop() set the gracefulShutdown flag
4. New predictions arriving during step 2-3 would pass the flag check
The fix:
- Extract flag-setting logic into handler.BeginShutdown()
- Call BeginShutdown() immediately after shutdown signal, before Stop()
- This ensures new predictions are rejected with 503 as soon as shutdown starts
Fixes TestShutdownEndpointWaitsForInflightPredictions which was expecting 503
but receiving 409 due to this race condition.
Co-authored-by: Mark Phelps <mphelps@cloudflare.com>