{"record":{"id":"f8e1fc0543fdba23","repo":"micro/go-micro","slug":"connection-is-nil","errorCode":null,"errorMessage":"connection is nil","messagePattern":"connection is nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"broker/rabbitmq/rabbitmq.go","lineNumber":234,"sourceCode":"\t\tif value, ok := options.Context.Value(userID{}).(string); ok {\n\t\t\tm.UserId = value\n\t\t}\n\n\t\tif value, ok := options.Context.Value(appID{}).(string); ok {\n\t\t\tm.AppId = value\n\t\t}\n\t}\n\n\tfor k, v := range msg.Header {\n\t\tm.Headers[k] = v\n\t}\n\n\tif r.getWithoutExchange() {\n\t\tm.Headers[\"Micro-Topic\"] = topic\n\t}\n\n\tif r.conn == nil {\n\t\treturn errors.New(\"connection is nil\")\n\t}\n\n\treturn r.conn.Publish(r.conn.exchange.Name, topic, m)\n}\n\nfunc (r *rbroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {\n\tvar ackSuccess bool\n\n\tif r.conn == nil {\n\t\treturn nil, errors.New(\"not connected\")\n\t}\n\n\topt := broker.SubscribeOptions{\n\t\tAutoAck: true,\n\t}\n\n\tfor _, o := range opts {\n\t\to(&opt)","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/broker/rabbitmq/rabbitmq.go#L216-L252","documentation":"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.","triggerScenarios":"Calling broker.Publish() before broker.Connect(), or after Disconnect(); using a zero-value rbroker instance; a reconnection flow that momentarily nils the conn.","commonSituations":"Publishers started before the broker connection is established at app startup; publishing after a shutdown handler ran Disconnect; misconfigured initialization order in dependency injection.","solutions":["Call broker.Connect() before publishing and check its error.","Guard application lifecycle so Disconnect runs only after all publishers finish.","Wrap publish calls with a state check or reconnect-and-retry helper."],"exampleFix":"// before\nb.Publish(topic, msg) // \"connection is nil\"\n// after\nif err := b.Connect(); err != nil { return err }\nif err := b.Publish(topic, msg); err != nil { return err }","handlingStrategy":"validation","validationCode":"if err := b.Connect(); err != nil { return err }\n// only now is Publish safe","typeGuard":null,"tryCatchPattern":"if err := b.Publish(topic, msg); err != nil {\n    if strings.Contains(err.Error(), \"connection is nil\") {\n        if cerr := b.Connect(); cerr != nil { return cerr }\n        return b.Publish(topic, msg)\n    }\n    return err\n}","preventionTips":["Always Connect before the first Publish; make Connect part of app startup.","Prevent Disconnect until all publishers have finished.","Centralize publish through a wrapper that reconnects on nil-conn errors."],"tags":["rabbitmq","publish","not-connected","lifecycle"],"backgroundTag":"not-connected","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}