# http: Server closed

- **ID:** `go/http-server-shutdown-timeout`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Server.Shutdown() is called without a proper context timeout, or the server is closed while active requests are still running, causing the server to return 'http: Server closed' error.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
    log.Fatal("Server forced to shutdown:", err)
}
   ```
2. **** (85% success)
   ```
   srv.RegisterOnShutdown(func() {
    // clean up resources
})
   ```

## Dead Ends

- **** — Without a timeout, Shutdown() blocks indefinitely, and the server may never close, causing resource leaks. (70% fail)
- **** — os.Exit() bypasses deferred functions and prevents proper server shutdown, leading to incomplete cleanup. (90% fail)
