{"record":{"id":"f4de5c3222262623","repo":"JuliusBrussee/caveman","slug":"native-runtime-socket-already-active","errorCode":null,"errorMessage":"native runtime: socket already active","messagePattern":"native runtime: socket already active","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/nativeruntime/server_unix.go","lineNumber":46,"sourceCode":"\n// ServeUnix exposes one-request-per-connection JSON over a user-only Unix\n// socket. Runtime errors close or fail-open the individual call; they never stop\n// the coding agent or the provider proxy.\nfunc ServeUnix(ctx context.Context, path string, runtime *Runtime) error {\n\tif runtime == nil || runtime.store == nil {\n\t\treturn errors.New(\"native runtime: store is required\")\n\t}\n\tif err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {\n\t\treturn fmt.Errorf(\"native runtime mkdir: %w\", err)\n\t}\n\tif err := os.Chmod(filepath.Dir(path), 0o700); err != nil {\n\t\treturn fmt.Errorf(\"native runtime chmod dir: %w\", err)\n\t}\n\tif _, err := os.Stat(path); err == nil {\n\t\tconn, dialErr := net.DialTimeout(\"unix\", path, 50*time.Millisecond)\n\t\tif dialErr == nil {\n\t\t\t_ = conn.Close()\n\t\t\treturn errors.New(\"native runtime: socket already active\")\n\t\t}\n\t\tif err := os.Remove(path); err != nil {\n\t\t\treturn fmt.Errorf(\"native runtime remove stale socket: %w\", err)\n\t\t}\n\t} else if !os.IsNotExist(err) {\n\t\treturn fmt.Errorf(\"native runtime inspect socket: %w\", err)\n\t}\n\tlistener, err := net.Listen(\"unix\", path)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"native runtime listen: %w\", err)\n\t}\n\tdefer listener.Close()\n\tdefer os.Remove(path)\n\tif err := os.Chmod(path, 0o600); err != nil {\n\t\treturn fmt.Errorf(\"native runtime chmod socket: %w\", err)\n\t}\n\tgo func() {\n\t\t<-ctx.Done()","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/nativeruntime/server_unix.go#L28-L64","documentation":"Before binding the Unix socket, ServeUnix probes an existing socket file with a 50ms dial. If another live process answers, it refuses to start rather than hijacking or double-binding; if the probe fails (stale socket from a crashed process), the file is removed and binding proceeds.","triggerScenarios":"os.Stat(path) succeeds and net.DialTimeout(\"unix\", path, 50*time.Millisecond) succeeds, i.e. a socket file exists at the runtime socket path and another process is actively listening on it.","commonSituations":"Starting a second instance of the coding agent while the first still runs; a supervisor restarting the service without stopping the old process; an unrelated process bound to the same default socket path.","solutions":["Stop the already-running runtime process that owns the socket, then restart.","Use a distinct socket path (per-profile or per-session) to allow concurrent instances.","If you own the socket and it is falsely 'active', kill the stale holder (lsof/ss on the path) and retry.","Treat this error as expected in startup scripts: check for an existing healthy instance and attach to it instead of spawning a new one."],"exampleFix":"// before\nif err := nativeruntime.ServeUnix(ctx, sockPath, rt); err != nil { return err }\n// after\nif err := nativeruntime.ServeUnix(ctx, sockPath, rt); err != nil {\n    if strings.Contains(err.Error(), \"socket already active\") {\n        return nil // another runtime is serving; fine\n    }\n    return err\n}","handlingStrategy":"try-catch","validationCode":"if _, err := os.Stat(sockPath); err == nil {\n    if c, derr := net.DialTimeout(\"unix\", sockPath, 50*time.Millisecond); derr == nil {\n        c.Close()\n        return errors.New(\"runtime already serving\")\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := nativeruntime.ServeUnix(ctx, path, rt); err != nil {\n    if strings.Contains(err.Error(), \"socket already active\") {\n        return nil // attach to existing instance\n    }\n    return err\n}","preventionTips":["Use per-instance/per-profile socket paths","Ensure supervisors stop old processes before restart","Clean stale sockets only after a failed dial, like the library does"],"tags":["unix-socket","port-conflict","native-runtime"],"backgroundTag":"address-already-in-use","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}