{"record":{"id":"9a3eeae8e502cdde","repo":"nats-io/nats-server","slug":"failed-to-create-connection","errorCode":null,"errorMessage":"failed to create connection","messagePattern":"failed to create connection","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/server.go","lineNumber":2893,"sourceCode":"\t\treturn s.listener, s.listenerErr\n\t}\n\n\treturn natsListen(\"tcp\", hp)\n}\n\n// InProcessConn returns an in-process connection to the server,\n// avoiding the need to use a TCP listener for local connectivity\n// within the same process. This can be used regardless of the\n// state of the DontListen option.\nfunc (s *Server) InProcessConn() (net.Conn, error) {\n\tpl, pr := net.Pipe()\n\tif !s.startGoRoutine(func() {\n\t\ts.createClientInProcess(pl)\n\t\ts.grWG.Done()\n\t}) {\n\t\tpl.Close()\n\t\tpr.Close()\n\t\treturn nil, fmt.Errorf(\"failed to create connection\")\n\t}\n\treturn pr, nil\n}\n\nfunc (s *Server) acceptConnections(l net.Listener, acceptName string, createFunc func(conn net.Conn), errFunc func(err error) bool) {\n\ttmpDelay := ACCEPT_MIN_SLEEP\n\n\tfor {\n\t\tconn, err := l.Accept()\n\t\tif err != nil {\n\t\t\tif errFunc != nil && errFunc(err) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif tmpDelay = s.acceptError(acceptName, err, tmpDelay); tmpDelay < 0 {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tcontinue\n\t\t}","sourceCodeStart":2875,"sourceCodeEnd":2911,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/server.go#L2875-L2911","documentation":"When the server needs an in-process client connection (createJwtAccount / internal client setup), it creates a socketpair and starts the goroutine driving the client side via s.startGoRoutine. If the server is shutting down or the goroutine cannot be started, startGoRoutine returns false, both ends are closed, and this error is returned to the caller.","triggerScenarios":"Calling code that requests an in-process connection while the server is quitting (s.isRunning false / quitting flag set), so startGoRoutine refuses to launch createClientInProcess and the pipe is torn down.","commonSituations":"Management API or tests requesting an internal connection concurrently with server Shutdown; server restart racing with connection creation; tests not waiting for server readiness before requesting internal connections.","solutions":["Retry the operation after checking the server is running (s.IsRunning() / wait for readiness signal)","Ensure Shutdown is not racing the call — coordinate lifecycle with the startupComplete signal","In tests, start the server and wait for it to be ready before requesting in-process connections"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Only request in-process connections while the server is running\nif !srv.IsRunning() || srv.IsQuitting() {\n    return nil, errors.New(\"server not accepting internal connections\")\n}","typeGuard":null,"tryCatchPattern":"pr, err := srv.createInternalAdminClient()\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to create connection\") {\n        time.Sleep(50 * time.Millisecond)\n        pr, err = srv.createInternalAdminClient() // retry once after shutdown check\n    }\n    if err != nil { return err }\n}","preventionTips":["Check srv.IsRunning() before requesting internal connections","Avoid racing Shutdown with internal connection creation in tests","Wait for ReadyForConnections before exercising in-process APIs"],"tags":["nats-server","connection","shutdown","goroutine"],"backgroundTag":"server-shutdown-race","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}