micro/go-micro · error

connection is nil

Error message

connection is nil

What it means

rbroker.Publish checks r.conn before delegating to the connection's Publish. If the broker has no connection object (Connect never called, or Disconnect cleared it), publishing is impossible and this error is returned. Stamps the Micro-Topic header first, but the nil-conn guard fires before the actual send.

Source

Thrown at broker/rabbitmq/rabbitmq.go:234

		if value, ok := options.Context.Value(userID{}).(string); ok {
			m.UserId = value
		}

		if value, ok := options.Context.Value(appID{}).(string); ok {
			m.AppId = value
		}
	}

	for k, v := range msg.Header {
		m.Headers[k] = v
	}

	if r.getWithoutExchange() {
		m.Headers["Micro-Topic"] = topic
	}

	if r.conn == nil {
		return errors.New("connection is nil")
	}

	return r.conn.Publish(r.conn.exchange.Name, topic, m)
}

func (r *rbroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
	var ackSuccess bool

	if r.conn == nil {
		return nil, errors.New("not connected")
	}

	opt := broker.SubscribeOptions{
		AutoAck: true,
	}

	for _, o := range opts {
		o(&opt)

View on GitHub (pinned to 24529f1404)

Solutions

  1. Call broker.Connect() before publishing and check its error.
  2. Guard application lifecycle so Disconnect runs only after all publishers finish.
  3. Wrap publish calls with a state check or reconnect-and-retry helper.

Example fix

// before
b.Publish(topic, msg) // "connection is nil"
// after
if err := b.Connect(); err != nil { return err }
if err := b.Publish(topic, msg); err != nil { return err }
Defensive patterns

Strategy: validation

Validate before calling

if err := b.Connect(); err != nil { return err }
// only now is Publish safe

Try / catch

if err := b.Publish(topic, msg); err != nil {
    if strings.Contains(err.Error(), "connection is nil") {
        if cerr := b.Connect(); cerr != nil { return cerr }
        return b.Publish(topic, msg)
    }
    return err
}

Prevention

When it happens

Trigger: Calling broker.Publish() before broker.Connect(), or after Disconnect(); using a zero-value rbroker instance; a reconnection flow that momentarily nils the conn.

Common situations: Publishers started before the broker connection is established at app startup; publishing after a shutdown handler ran Disconnect; misconfigured initialization order in dependency injection.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/f8e1fc0543fdba23. Report an issue: GitHub.