go network_error ai_generated true

rpc error: code = Canceled desc = context canceled

ID: go/grpc-canceled-by-client

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-03-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/grpc 1.48+ active — — —

Root Cause

The client canceled the RPC (via context cancel, HTTP/2 RST_STREAM, or parent context timeout) before the server finished.

generic

中文

客户端在服务端完成前取消了 RPC(通过 context cancel、HTTP/2 RST_STREAM 或父上下文超时)。

Workarounds

  1. 90% success
    Check ctx.Err() in the server handler and exit early:
    select {
    case <-ctx.Done():
        return nil, status.FromContextError(ctx.Err()).Err()
    default:
    }
    // continue processing
  2. 85% success
    Distinguish client cancel from server-side timeout by inspecting the parent context; treat codes.Canceled as INFO in logs and metrics.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    If the caller intentionally canceled (e.g., user closed the page), retrying wastes resources.

  2. 85% fail

    Canceled is a normal client behavior; alerting creates noise.