micro/go-micro · error
cannot init while connected
Error message
cannot init while connected
What it means
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.
Source
Thrown at broker/http.go:462
if ok {
rc.Stop()
}
// exit and return err
ch := make(chan error)
h.exit <- ch
err := <-ch
// set not running
h.running = false
return err
}
func (h *httpBroker) Init(opts ...Option) error {
h.RLock()
if h.running {
h.RUnlock()
return errors.New("cannot init while connected")
}
h.RUnlock()
h.Lock()
defer h.Unlock()
for _, o := range opts {
o(&h.opts)
}
if len(h.opts.Addrs) > 0 && len(h.opts.Addrs[0]) > 0 {
h.address = h.opts.Addrs[0]
}
if len(h.id) == 0 {
h.id = "go.micro.http.broker-" + uuid.New().String()
}
View on GitHub (pinned to 24529f1404)
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
Example fix
// before: init after connect panics the config into an error broker.Init(broker.Addrs(addr)) broker.Connect() broker.Init(broker.Addrs(otherAddr)) // "cannot init while connected" // after: apply all options before connecting broker.Init(broker.Addrs(otherAddr)) broker.Connect()
Defensive patterns
Strategy: validation
Validate before calling
// gate Init behind a once-guard so it can never run after Connect
var initOnce sync.Once
func setupBroker(opts ...broker.Option) error {
var err error
initOnce.Do(func() { err = broker.Init(opts...) })
return err
}
setupBroker(broker.Addrs(addr))
broker.Connect() Type guard
// no exported sentinel; guard by broker state
func brokerReady(b broker.Broker) bool {
return b != nil && b.Address() != ""
} Try / catch
if err := broker.Init(opts...); err != nil {
if strings.Contains(err.Error(), "cannot init while connected") {
// wrong lifecycle order: disconnect, re-init, reconnect
broker.Disconnect()
return broker.Init(opts...)
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- not connected
- agent: checkpointed run is terminal with status
- ErrForbidden
- channel is nil
- connection is nil
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/a73648852d99c773.
Report an issue: GitHub.