{"record":{"id":"3f8ac11f1d553de0","repo":"micro/go-micro","slug":"message-passed-in-is-nil-3f8ac1","errorCode":null,"errorMessage":"message passed in is nil","messagePattern":"message passed in is nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/nats/nats.go","lineNumber":233,"sourceCode":"\t\treturn n.pool.Put(n.pooledConn)\n\t}\n\n\t// Otherwise, close the connection directly\n\tn.conn.Close()\n\treturn nil\n}\n\nfunc (n *ntportSocket) Local() string {\n\treturn n.local\n}\n\nfunc (n *ntportSocket) Remote() string {\n\treturn n.remote\n}\n\nfunc (n *ntportSocket) Recv(m *transport.Message) error {\n\tif m == nil {\n\t\treturn errors.New(\"message passed in is nil\")\n\t}\n\n\tvar r *nats.Msg\n\tvar ok bool\n\n\t// if there's a deadline we use it\n\tif n.opts.Timeout > time.Duration(0) {\n\t\tselect {\n\t\tcase r, ok = <-n.r:\n\t\tcase <-time.After(n.opts.Timeout):\n\t\t\treturn errors.New(\"deadline exceeded\")\n\t\t}\n\t} else {\n\t\tr, ok = <-n.r\n\t}\n\n\tif !ok {\n\t\treturn io.EOF","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/transport/nats/nats.go#L215-L251","documentation":"Recv returns this error when the caller passes a nil *transport.Message. The NATS socket implementation requires a non-nil message struct to fill with incoming data; it validates the argument before blocking on the receive channel. This is a caller-side programming bug, not a network problem.","triggerScenarios":"Calling socket.Recv(nil) on an *ntportSocket obtained from a NATS transport listener or client, e.g. reusing a message variable that was never initialized with &transport.Message{Header: ..., Body: ...}.","commonSituations":"Declarations like `var msg *transport.Message` instead of `msg := &transport.Message{}`; a helper returning nil on some code path; refactoring that removed the allocation but left the Recv call.","solutions":["Allocate the message before calling Recv: use m := &transport.Message{Header: map[string]string{}, Body: []byte{}}","Check for nil before passing: if m == nil { m = &transport.Message{} }","Audit code paths that construct the message to ensure none return nil"],"exampleFix":"// before\nvar msg *transport.Message\nif err := sock.Recv(msg); err != nil { ... }\n// after\nmsg := &transport.Message{Header: map[string]string{}}\nif err := sock.Recv(msg); err != nil { ... }","handlingStrategy":"validation","validationCode":"func ensureMsg(m *transport.Message) *transport.Message {\n\tif m == nil {\n\t\treturn &transport.Message{Header: map[string]string{}}\n\t}\n\treturn m\n}\n// if err := sock.Recv(ensureMsg(msg)); err != nil { ... }","typeGuard":"func isNilMessage(m *transport.Message) bool { return m == nil }","tryCatchPattern":"if err := sock.Recv(msg); err != nil {\n\tif err.Error() == \"message passed in is nil\" {\n\t\treturn fmt.Errorf(\"recv: message must be allocated: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Always initialize transport.Message with &transport.Message{} before Recv","Never declare message vars as nil pointers","Wrap Recv in a helper that allocates the message internally","Run go vet / staticcheck to catch obvious nil deref patterns"],"tags":["nats","transport","nil-argument","api-misuse"],"backgroundTag":"nil-pointer-argument","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}