# http: 服务器关闭：上下文截止时间已超过

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

## 根因

在优雅关闭期间，服务器未能在给定的上下文截止时间内完成处理活动请求。

## 版本兼容性

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

## 解决方案

1. **** (85% 成功率)
   ```
   Set a reasonable timeout and handle error: ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second); defer cancel(); if err := srv.Shutdown(ctx); err != nil { log.Fatal(err) }
   ```
2. **** (90% 成功率)
   ```
   Track active connections and wait for them before calling Shutdown: use sync.WaitGroup to wait for handlers to finish.
   ```

## 无效尝试

- **** — Delays shutdown indefinitely and may not solve if requests hang forever. (60% 失败率)
- **** — Loses active connections and may corrupt data. (80% 失败率)
