{"record":{"id":"e2486f793d282648","repo":"grpc/grpc-go","slug":"failed-to-write-the-http-request-v","errorCode":null,"errorMessage":"failed to write the HTTP request: %v","messagePattern":"failed to write the HTTP request: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/transport/proxy.go","lineNumber":75,"sourceCode":"func doHTTPConnectHandshake(ctx context.Context, conn net.Conn, grpcUA string, opts proxyattributes.Options) (_ net.Conn, err error) {\n\tdefer func() {\n\t\tif err != nil {\n\t\t\tconn.Close()\n\t\t}\n\t}()\n\n\treq := &http.Request{\n\t\tMethod: http.MethodConnect,\n\t\tURL:    &url.URL{Host: opts.ConnectAddr},\n\t\tHeader: map[string][]string{\"User-Agent\": {grpcUA}},\n\t}\n\tif user := opts.User; user != nil {\n\t\tu := user.Username()\n\t\tp, _ := user.Password()\n\t\treq.Header.Add(proxyAuthHeaderKey, \"Basic \"+basicAuth(u, p))\n\t}\n\tif err := sendHTTPRequest(ctx, req, conn); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to write the HTTP request: %v\", err)\n\t}\n\n\tr := bufio.NewReader(conn)\n\tresp, err := http.ReadResponse(r, req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"reading server HTTP response: %v\", err)\n\t}\n\tdefer resp.Body.Close()\n\tif resp.StatusCode != http.StatusOK {\n\t\tdump, err := httputil.DumpResponse(resp, true)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to do connect handshake, status code: %s\", resp.Status)\n\t\t}\n\t\treturn nil, fmt.Errorf(\"failed to do connect handshake, response: %q\", dump)\n\t}\n\t// The buffer could contain extra bytes from the target server, so we can't\n\t// discard it. However, in many cases where the server waits for the client\n\t// to send the first message (e.g. when TLS is being used), the buffer will","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/grpc/grpc-go/blob/03255a9237b6eb32710f6bc4f2de9a675b99fe36/internal/transport/proxy.go#L57-L93","documentation":"This error occurs in doHTTPConnectHandshake when sendHTTPRequest fails to write the HTTP CONNECT request to the proxy connection. The underlying conn.Write returned an error, indicating the TCP connection to the proxy was broken or the write was interrupted.","triggerScenarios":"Calling grpc.Dial with a proxy configured (HTTPS_PROXY/HTTPS_PROXY env var or WithContextDialer proxy), and the TCP connection to the proxy drops before or during the CONNECT request write — e.g., proxy process crashed, network partition, or the dial context was cancelled.","commonSituations":"Corporate proxy restarts/failures, flaky network between client and forward proxy, proxy rejecting connections at the TCP level (connection reset), or context cancellation (deadline exceeded) during proxy handshake.","solutions":["Verify the proxy address is reachable and the proxy process is running.","Check network connectivity/firewall rules between the client and the proxy host:port.","Retry the connection (transient proxy/network issues often resolve on retry).","If the context deadline is too short, increase the dial timeout with grpc.WithTransportCredentials + context.WithTimeout."],"exampleFix":"// before (broken): no retry on transient proxy write failure\nconn, err := grpc.Dial(target, grpc.WithBlock())\n\n// after (valid): use a dial timeout and retry/backoff policy\nconn, err := grpc.Dial(target,\n  grpc.WithConnectParams(grpc.ConnectParams{\n    Backoff:           backoff.Config{BaseDelay: 1*time.Second, MaxDelay: 10*time.Second},\n    MinConnectTimeout: 5 * time.Second,\n  }),\n)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// gRPC client connection with retry/backoff handles transient proxy write failures\nconn, err := grpc.Dial(target,\n    grpc.WithConnectParams(grpc.ConnectParams{\n        Backoff:           backoff.Config{BaseDelay: 1*time.Second, MaxDelay: 30*time.Second, Multiplier: 1.6},\n        MinConnectTimeout: 5 * time.Second,\n    }),\n)\nif err != nil {\n    log.Printf(\"failed to connect via proxy: %v\", err)\n}","preventionTips":["Configure grpc.ConnectParams with appropriate backoff for proxy environments.","Monitor proxy health and configure failover if possible.","Set reasonable dial timeouts to avoid hanging on unreachable proxies."],"tags":["transport","proxy","http-connect","network","connection"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}