{"record":{"id":"6fa83b48601c583b","repo":"cilium/cilium","slug":"unable-to-gob-encode-w","errorCode":null,"errorMessage":"unable to gob encode: %w","messagePattern":"unable to gob encode: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/monitor/agent/agent.go","lineNumber":175,"sourceCode":"\t// marshal notifications into JSON format for legacy listeners\n\tif typ == api.MessageTypeAgent {\n\t\tmsg, ok := event.(api.AgentNotifyMessage)\n\t\tif !ok {\n\t\t\treturn errors.New(\"unexpected event type for MessageTypeAgent\")\n\t\t}\n\t\tvar err error\n\t\tevent, err = msg.ToJSON()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to JSON encode agent notification: %w\", err)\n\t\t}\n\t}\n\n\tvar buf bytes.Buffer\n\tif err := buf.WriteByte(byte(typ)); err != nil {\n\t\treturn fmt.Errorf(\"unable to initialize buffer: %w\", err)\n\t}\n\tif err := gob.NewEncoder(&buf).Encode(event); err != nil {\n\t\treturn fmt.Errorf(\"unable to gob encode: %w\", err)\n\t}\n\n\tp := payload.Payload{Data: buf.Bytes(), CPU: 0, Lost: 0, Type: payload.EventSample}\n\ta.sendToListeners(&p)\n\n\treturn nil\n}\n\n// hasSubscribersLocked returns true if there are listeners or consumers\n// subscribed to the agent right now.\n// Note: it is critical to hold the lock for this operation.\nfunc (a *agent) hasSubscribersLocked() bool {\n\treturn len(a.listeners)+len(a.consumers) != 0\n}\n\n// hasListeners returns true if there are listeners subscribed to the\n// agent right now.\nfunc (a *agent) hasListeners() bool {","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/monitor/agent/agent.go#L157-L193","documentation":"For event types other than MessageTypeAgent, SendEvent gob-encodes the event into the buffer so listener clients receive gob-encoded payloads. If gob.NewEncoder(&buf).Encode(event) fails, this error wraps the gob error. Gob encoding fails mainly when the value contains types gob cannot handle: unexported fields, channels, funcs, or types not registered with gob for interface values.","triggerScenarios":"Calling SendEvent(typ, event) with a non-MessageTypeAgent type where event contains unexported fields, chan/func/unsafe.Pointer fields, or an interface value whose concrete type was not registered via gob.Register — causing Encode to error.","commonSituations":"Adding a struct with unexported fields to an event payload, sending interface-typed events across a new plugin/binary without gob.Register on both sides, or changing a payload type in a way that breaks gob compatibility between writer and reader.","solutions":["Read the wrapped gob error — it names the offending field or missing type registration.","Register interface concrete types with gob.Register (or gob.RegisterName) before sending.","Ensure event structs only contain exported, gob-encodable fields; convert chan/func fields to encodable representations.","Keep payload struct definitions identical (field names/types) between sender and listener to avoid decode-time mismatches, and add a gob round-trip test per event type."],"exampleFix":"// before\ntype event struct {\n    ts   uint64 // unexported: gob cannot encode\n    Name string\n}\n// after\ntype event struct {\n    Ts   uint64\n    Name string\n}\nfunc init() { gob.Register(event{}) }","handlingStrategy":"validation","validationCode":"var buf bytes.Buffer\nif err := gob.NewEncoder(&buf).Encode(event); err != nil {\n    logger.Error(\"event not gob-encodable\", \"err\", err)\n    return\n} // pre-flight encode before handing to SendEvent","typeGuard":"func gobEncodable(v any) bool {\n    var buf bytes.Buffer\n    return gob.NewEncoder(&buf).Encode(v) == nil\n}","tryCatchPattern":"err := agent.SendEvent(typ, event)\nif err != nil {\n    var gobErr error\n    if errors.As(err, &gobErr) && strings.Contains(err.Error(), \"gob\") {\n        logger.Error(\"gob encode failed; check gob.Register for event type\", \"err\", err)\n    }\n}","preventionTips":["gob.Register every concrete type sent through interface-typed events.","Use only exported fields in event structs; convert chan/func fields to data.","Add a gob encode/decode round-trip test per event type.","Keep event struct definitions in sync between agent and listeners."],"tags":["go","gob","serialization","encoding"],"backgroundTag":"gob-encoding-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}