micro/go-micro · warning

channel is nil

Error message

channel is nil

What it means

rabbitMQChannel.Close() checks that the underlying AMQP channel pointer is non-nil before delegating to channel.Close(). If the channel was never created (no successful connect/open) or was already torn down, Close returns "channel is nil" instead of panicking on a nil pointer.

Source

Thrown at broker/rabbitmq/channel.go:64

	if err != nil {
		return err
	}

	if confirmPublish {
		r.confirmPublish = r.channel.NotifyPublish(make(chan amqp.Confirmation, 1))

		err = r.channel.Confirm(false)
		if err != nil {
			return err
		}
	}

	return nil
}

func (r *rabbitMQChannel) Close() error {
	if r.channel == nil {
		return errors.New("channel is nil")
	}
	return r.channel.Close()
}

func (r *rabbitMQChannel) Publish(exchange, key string, message amqp.Publishing) error {
	if r.channel == nil {
		return errors.New("channel is nil")
	}

	if r.confirmPublish != nil {
		r.mtx.Lock()
		defer r.mtx.Unlock()
	}

	err := r.channel.Publish(exchange, key, false, false, message)
	if err != nil {
		return err
	}

View on GitHub (pinned to 24529f1404)

Solutions

  1. Skip treating this as fatal on shutdown — the resource is already gone; log and continue.
  2. Track channel state in your code and only call Close once.
  3. Reconnect the broker (broker.Connect) before creating new channels if the connection dropped.

Example fix

// before
ch.Close() // panics-ish: "channel is nil"
// after
if err := ch.Close(); err != nil && err.Error() != "channel is nil" {
    log.Printf("close failed: %v", err)
}
Defensive patterns

Strategy: validation

Validate before calling

// only Close a channel you successfully opened and haven't closed
if !channelOpened { skipClose() }

Try / catch

if err := ch.Close(); err != nil && err.Error() != "channel is nil" {
    log.Printf("channel close failed: %v", err)
}

Prevention

When it happens

Trigger: Calling Close() on a rabbitMQChannel whose underlying *amqp.Channel is nil — channel never opened, or closed/recycled elsewhere first.

Common situations: Deferred cleanup after a failed Connect; double-Close in shutdown handlers; using a channel after the RabbitMQ connection dropped and the channel was discarded.

Related errors


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