# http：在已关闭的连接上写入

- **ID:** `go/http-write-after-shutdown`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试向已关闭的客户端连接写入响应，通常是由于超时或客户端断开连接。

## 版本兼容性

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

## 解决方案

1. **** (85% 成功率)
   ```
   if err := json.NewEncoder(w).Encode(data); err != nil {
    log.Printf("Error writing response: %v", err)
}
   ```
2. **** (80% 成功率)
   ```
   Use context cancellation to detect client disconnects early.
   ```

## 无效尝试

- **** — The write will fail silently, and the client will never receive the response. (70% 失败率)
- **** — The issue is not buffer size but connection state. (90% 失败率)
