{"record":{"id":"fc8cc41c3e1f26b5","repo":"github/copilot-sdk","slug":"client-not-connected-fc8cc4","errorCode":null,"errorMessage":"client not connected","messagePattern":"client not connected","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/client.go","lineNumber":1859,"sourceCode":"\treturn c.actualPort\n}\n\n// Ping sends a ping request to the server to verify connectivity.\n//\n// The message parameter is optional and will be echoed back in the response.\n// Returns a PingResponse containing the message and server timestamp, or an error.\n//\n// Example:\n//\n//\tresp, err := client.Ping(context.Background(), \"health check\")\n//\tif err != nil {\n//\t    log.Printf(\"Server unreachable: %v\", err)\n//\t} else {\n//\t    log.Printf(\"Server responded at %s\", resp.Timestamp)\n//\t}\nfunc (c *Client) Ping(ctx context.Context, message string) (*PingResponse, error) {\n\tif c.client == nil {\n\t\treturn nil, fmt.Errorf(\"client not connected\")\n\t}\n\n\tresult, err := c.client.Request(ctx, \"ping\", pingRequest{Message: message})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tvar response PingResponse\n\tif err := json.Unmarshal(result, &response); err != nil {\n\t\treturn nil, err\n\t}\n\treturn &response, nil\n}\n\n// GetStatus returns CLI status including version and protocol information\nfunc (c *Client) GetStatus(ctx context.Context) (*GetStatusResponse, error) {\n\tif c.client == nil {\n\t\treturn nil, fmt.Errorf(\"client not connected\")","sourceCodeStart":1841,"sourceCodeEnd":1877,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/go/client.go#L1841-L1877","documentation":"Ping returns this error when the Client's underlying JSON-RPC client (c.client) is nil, i.e. no connection to the agent backend has ever been established. Ping cannot send a request without a transport, so it fails fast instead of panicking on a nil client.","triggerScenarios":"Calling Ping on a Client that was created without a successful Connect/start (e.g. constructed via a constructor that defers connection, or after an explicit close/disconnect).","commonSituations":"Health-check goroutines starting before client.Connect completes; reusing a client after Close; error swallowed during initialization so the app kept a disconnected client.","solutions":["Ensure Connect (or the equivalent initialization call) succeeded before calling Ping","Check the error returned at construction time — a failed spawn of the agent binary leaves client nil","Re-create or reconnect the client if it was closed, then Ping again","Guard health-check loops so they skip Ping while the client is disconnected"],"exampleFix":"// before\nresp, err := client.Ping(ctx, \"health\")\n// after\nif err := client.Connect(ctx); err != nil {\n    return fmt.Errorf(\"connect: %w\", err)\n}\nresp, err := client.Ping(ctx, \"health\")","handlingStrategy":"try-catch","validationCode":"// guard before calling\nif client == nil {\n    return fmt.Errorf(\"client not initialized\")\n}","typeGuard":"func isConnected(c *acp.Client) bool {\n    return c != nil && !c.IsClosed() // check your wrapper's connection state\n}","tryCatchPattern":"resp, err := client.Ping(ctx, \"health\")\nif err != nil {\n    if strings.Contains(err.Error(), \"client not connected\") {\n        // reconnect then retry once\n        if cerr := client.Connect(ctx); cerr == nil {\n            resp, err = client.Ping(ctx, \"health\")\n        }\n    }\n    if err != nil { return err }\n}","preventionTips":["Always call Connect and check its error before any client method","Track client lifecycle in one place so health checks wait for readiness","Never reuse a client after Close — create a new one","Fail fast at startup if Connect fails instead of keeping a nil-client wrapper"],"tags":["connection","client-lifecycle","ping"],"backgroundTag":"client-not-connected","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}