{"record":{"id":"410f4529b145866b","repo":"github/copilot-sdk","slug":"failed-to-close-socket-w","errorCode":null,"errorMessage":"failed to close socket: %w","messagePattern":"failed to close socket: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"go/client.go","lineNumber":640,"sourceCode":"\t// immediately and only wait to reap it.\n\tif c.process != nil && !c.isExternalServer {\n\t\tif err := c.killProcessAndWait(); err != nil {\n\t\t\terrs = append(errs, err)\n\t\t}\n\t}\n\tc.process = nil\n\n\t// Tear down the in-process FFI host (closes the connection and shuts down the\n\t// native runtime). No child process to reap in this mode.\n\tif c.ffiHost != nil {\n\t\tc.ffiHost.Dispose()\n\t\tc.ffiHost = nil\n\t}\n\n\t// Close external TCP connection if exists\n\tif c.isExternalServer && c.conn != nil {\n\t\tif err := c.conn.Close(); err != nil {\n\t\t\terrs = append(errs, fmt.Errorf(\"failed to close socket: %w\", err))\n\t\t}\n\t\tc.conn = nil\n\t}\n\n\t// Then close JSON-RPC client (readLoop can now exit)\n\tif c.client != nil {\n\t\tc.client.Stop()\n\t\tc.client = nil\n\t}\n\n\t// Clear models cache\n\tc.modelsCacheMux.Lock()\n\tc.modelsCache = nil\n\tc.modelsCacheMux.Unlock()\n\n\tc.state = stateDisconnected\n\tif !c.isExternalServer {\n\t\tc.actualPort = 0","sourceCodeStart":622,"sourceCodeEnd":658,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/go/client.go#L622-L658","documentation":"When the client was created against an external server over TCP, Stop closes that socket; a Close failure is wrapped with this message and appended to Stop's error list. It indicates the TCP connection could not be closed cleanly.","triggerScenarios":"Calling Stop on a client configured with isExternalServer whose c.conn.Close() returns an error (e.g. already closed, reset by peer).","commonSituations":"External runtime already dropped the connection; double Stop calls; firewall/network teardown mid-close.","solutions":["Check the wrapped cause; 'use of closed connection' errors are harmless — treat Stop as complete.","Avoid calling Stop twice on the same client.","Verify the external server is reachable and behaving during teardown.","If persistent, inspect network/firewall configuration."],"exampleFix":"// before\nerr := client.Stop(ctx); // may surface socket close error\n// after\nif err != nil && strings.Contains(err.Error(), \"failed to close socket\") {\n    log.Printf(\"socket close issue during stop (often harmless): %v\", err)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := client.Stop(ctx); err != nil && strings.Contains(err.Error(), \"failed to close socket\") { /* usually benign */ }","preventionTips":["Call Stop exactly once per client","Verify external server stability","Distinguish benign close errors from real failures"],"tags":["go","network","tcp"],"backgroundTag":"broken-pipe","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}