{"record":{"id":"def34d44357b2332","repo":"micro/go-micro","slug":"channel-is-nil","errorCode":null,"errorMessage":"channel is nil","messagePattern":"channel is nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"broker/rabbitmq/channel.go","lineNumber":64,"sourceCode":"\tif err != nil {\n\t\treturn err\n\t}\n\n\tif confirmPublish {\n\t\tr.confirmPublish = r.channel.NotifyPublish(make(chan amqp.Confirmation, 1))\n\n\t\terr = r.channel.Confirm(false)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (r *rabbitMQChannel) Close() error {\n\tif r.channel == nil {\n\t\treturn errors.New(\"channel is nil\")\n\t}\n\treturn r.channel.Close()\n}\n\nfunc (r *rabbitMQChannel) Publish(exchange, key string, message amqp.Publishing) error {\n\tif r.channel == nil {\n\t\treturn errors.New(\"channel is nil\")\n\t}\n\n\tif r.confirmPublish != nil {\n\t\tr.mtx.Lock()\n\t\tdefer r.mtx.Unlock()\n\t}\n\n\terr := r.channel.Publish(exchange, key, false, false, message)\n\tif err != nil {\n\t\treturn err\n\t}","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/broker/rabbitmq/channel.go#L46-L82","documentation":"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.","triggerScenarios":"Calling Close() on a rabbitMQChannel whose underlying *amqp.Channel is nil — channel never opened, or closed/recycled elsewhere first.","commonSituations":"Deferred cleanup after a failed Connect; double-Close in shutdown handlers; using a channel after the RabbitMQ connection dropped and the channel was discarded.","solutions":["Skip treating this as fatal on shutdown — the resource is already gone; log and continue.","Track channel state in your code and only call Close once.","Reconnect the broker (broker.Connect) before creating new channels if the connection dropped."],"exampleFix":"// before\nch.Close() // panics-ish: \"channel is nil\"\n// after\nif err := ch.Close(); err != nil && err.Error() != \"channel is nil\" {\n    log.Printf(\"close failed: %v\", err)\n}","handlingStrategy":"validation","validationCode":"// only Close a channel you successfully opened and haven't closed\nif !channelOpened { skipClose() }","typeGuard":null,"tryCatchPattern":"if err := ch.Close(); err != nil && err.Error() != \"channel is nil\" {\n    log.Printf(\"channel close failed: %v\", err)\n}","preventionTips":["Call Close exactly once per channel using sync.Once or a closed flag.","Don't Close channels after a failed Connect.","Treat 'channel is nil' during shutdown as already-cleaned-up, not fatal."],"tags":["rabbitmq","amqp","nil-channel","lifecycle"],"backgroundTag":"nil-resource-close","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}