{"record":{"id":"ca1f64b41a96c994","repo":"micro/go-micro","slug":"errpoolclosed","errorCode":"ErrPoolClosed","errorMessage":"connection pool is closed","messagePattern":"connection pool is closed","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"broker/nats/pool.go","lineNumber":15,"sourceCode":"package nats\n\nimport (\n\t\"errors\"\n\t\"sync\"\n\t\"time\"\n\n\tnatsp \"github.com/nats-io/nats.go\"\n)\n\nvar (\n\t// ErrPoolExhausted is returned when no connections are available in the pool\n\tErrPoolExhausted = errors.New(\"connection pool exhausted\")\n\t// ErrPoolClosed is returned when trying to use a closed pool\n\tErrPoolClosed = errors.New(\"connection pool is closed\")\n)\n\n// connectionPool manages a pool of NATS connections\ntype connectionPool struct {\n\tmu          sync.RWMutex\n\tconnections chan *pooledConnection\n\tfactory     func() (*natsp.Conn, error)\n\tsize        int\n\tidleTimeout time.Duration\n\tclosed      bool\n}\n\n// pooledConnection wraps a NATS connection with metadata\ntype pooledConnection struct {\n\tconn      *natsp.Conn\n\tcreatedAt time.Time\n\tlastUsed  time.Time\n\tmu        sync.Mutex","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/broker/nats/pool.go#L1-L33","documentation":"ErrPoolClosed is a sentinel error returned by pool.Get(), TestConnectionPool_Close, and TestTransportConnectionPool_Close when an operation attempts to acquire a connection from a pool that has already been closed. Once Close() is called the pool stops handing out connections permanently.","triggerScenarios":"Calling Subscribe (which does pool.Get()) after broker.Disconnect/Close has closed the connection pool; reusing a broker instance after shutdown.","commonSituations":"Lifecycle ordering bugs — a shutdown handler closes the broker while background workers still try to subscribe/publish; reusing a cached broker client after application teardown.","solutions":["Call broker.Connect again to recreate the pool before subscribing after a Disconnect.","Fix lifecycle ordering: stop all consumers before closing the broker.","Check the broker's connection state before use in long-running services."],"exampleFix":"// before\nb.Disconnect()\nsub, _ := b.Subscribe(\"events\", handler) // ErrPoolClosed\n// after\nb.Disconnect()\nif err := b.Connect(); err != nil { return err }\nsub, _ := b.Subscribe(\"events\", handler)","handlingStrategy":"validation","validationCode":"if err == natsbroker.ErrPoolClosed {\n    // pool was closed; reconnect before subscribing\n}","typeGuard":null,"tryCatchPattern":"sub, err := b.Subscribe(topic, handler)\nif errors.Is(err, natsbroker.ErrPoolClosed) {\n    if cerr := b.Connect(); cerr != nil { return cerr }\n    sub, err = b.Subscribe(topic, handler)\n}","preventionTips":["Enforce lifecycle ordering: close the pool only after all consumers stop.","Track a 'running' flag and reject work after Disconnect.","Never reuse a broker instance after teardown without reconnecting."],"tags":["nats","connection-pool","closed-resource","sentinel-error"],"backgroundTag":"connection-pool-closed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}