# 错误：http：服务器已关闭

- **ID:** `go/net-http-server-shutdown-graceful-timeout`
- **领域:** go
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

HTTP服务器在处理请求时关闭，导致正在进行的请求失败并返回“服务器已关闭”。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Use Shutdown with a context timeout and wait for connections to drain** (95% 成功率)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil { log.Fatal(err) }
   ```

## 无效尝试

- **Using Shutdown without waiting for active connections** — Active connections are terminated abruptly; use Shutdown with context. (70% 失败率)
