go network_error ai_generated true

错误: 上下文已取消

error: context canceled

ID: go/context-canceled-propagation

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-03-30首次发现

版本兼容性

版本状态引入弃用备注
1.7+ active — — —

根因分析

父上下文被取消(或超时),而子操作仍在进行。错误来自 HTTP 客户端、数据库查询或检查 ctx.Err() 的自定义代码。

English

A parent context was canceled (or timed out) while a child operation was in progress. The error surfaces from HTTP clients, DB queries, or custom code checking ctx.Err().

generic

解决方案

  1. 92% 成功率
    if err := ctx.Err(); err != nil {
        return err
    }
  2. 85% 成功率
    retryCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    resp, err := client.Do(req.WithContext(retryCtx))

无效尝试

常见但无效的做法:

  1. 95% 失败

    The context remains canceled; retries fail instantly without making a request.

  2. 80% 失败

    Ignores the caller's shutdown intent; leaks work after the request is abandoned.