{"record":{"id":"a73648852d99c773","repo":"micro/go-micro","slug":"cannot-init-while-connected","errorCode":null,"errorMessage":"cannot init while connected","messagePattern":"cannot init while connected","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"broker/http.go","lineNumber":462,"sourceCode":"\tif ok {\n\t\trc.Stop()\n\t}\n\n\t// exit and return err\n\tch := make(chan error)\n\th.exit <- ch\n\terr := <-ch\n\n\t// set not running\n\th.running = false\n\treturn err\n}\n\nfunc (h *httpBroker) Init(opts ...Option) error {\n\th.RLock()\n\tif h.running {\n\t\th.RUnlock()\n\t\treturn errors.New(\"cannot init while connected\")\n\t}\n\th.RUnlock()\n\n\th.Lock()\n\tdefer h.Unlock()\n\n\tfor _, o := range opts {\n\t\to(&h.opts)\n\t}\n\n\tif len(h.opts.Addrs) > 0 && len(h.opts.Addrs[0]) > 0 {\n\t\th.address = h.opts.Addrs[0]\n\t}\n\n\tif len(h.id) == 0 {\n\t\th.id = \"go.micro.http.broker-\" + uuid.New().String()\n\t}\n","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/broker/http.go#L444-L480","documentation":"The httpBroker returns the error \"cannot init while connected\" from httpBroker.Init when Init is called after the broker has already started (h.running is true). Init is meant to apply options before the broker connects; reconfiguring a live broker is not supported, so the call is rejected.","triggerScenarios":"Calling broker.Init(...) after broker.Connect() has succeeded; a service framework/plugin re-invoking Init during a reload while the broker is running; calling Init twice in setup code where the first call already ran.","commonSituations":"Application hot-reload handlers calling Init on the shared broker instance; wiring code that configures the broker per-request instead of once at startup; tests reusing a global broker across cases without disconnecting.","solutions":["Call broker.Init with all options before broker.Connect; move option application earlier in startup","Disconnect the running broker first (broker.Disconnect), Init again, then reconnect if reconfiguration is truly needed","Ensure Init is invoked exactly once (guard with sync.Once or a startup flag)","Construct a fresh broker instance instead of reinitializing the connected one"],"exampleFix":"// before: init after connect panics the config into an error\nbroker.Init(broker.Addrs(addr))\nbroker.Connect()\nbroker.Init(broker.Addrs(otherAddr)) // \"cannot init while connected\"\n// after: apply all options before connecting\nbroker.Init(broker.Addrs(otherAddr))\nbroker.Connect()","handlingStrategy":"validation","validationCode":"// gate Init behind a once-guard so it can never run after Connect\nvar initOnce sync.Once\nfunc setupBroker(opts ...broker.Option) error {\n    var err error\n    initOnce.Do(func() { err = broker.Init(opts...) })\n    return err\n}\nsetupBroker(broker.Addrs(addr))\nbroker.Connect()","typeGuard":"// no exported sentinel; guard by broker state\nfunc brokerReady(b broker.Broker) bool {\n    return b != nil && b.Address() != \"\"\n}","tryCatchPattern":"if err := broker.Init(opts...); err != nil {\n    if strings.Contains(err.Error(), \"cannot init while connected\") {\n        // wrong lifecycle order: disconnect, re-init, reconnect\n        broker.Disconnect()\n        return broker.Init(opts...)\n    }\n    return err\n}","preventionTips":["Always Init before Connect; configure all options at startup only","Use sync.Once or explicit lifecycle flags for broker setup","Never call Init from hot-reload or per-request code paths"],"tags":["broker","lifecycle","go-micro"],"backgroundTag":"already-initialized","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}