{"record":{"id":"8f0fe09747b16260","repo":"hashicorp/nomad","slug":"failed-to-encode-passed-object-v","errorCode":null,"errorMessage":"failed to encode passed object: %v","messagePattern":"failed to encode passed object: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/boltdd/boltdd.go","lineNumber":315,"sourceCode":"}\n\n// newBucket creates a new view into a bucket backed by a boltdb\n// transaction.\nfunc newBucket(b *bucketMeta, bb *bbolt.Bucket) *Bucket {\n\treturn &Bucket{\n\t\tbm:         b,\n\t\tboltBucket: bb,\n\t}\n}\n\n// Put into boltdb iff it has changed since the last write.\nfunc (b *Bucket) Put(key []byte, val interface{}) error {\n\t// buffer for writing serialized state to\n\tvar buf bytes.Buffer\n\n\t// Serialize the object\n\tif err := codec.NewEncoder(&buf, structs.MsgpackHandle).Encode(val); err != nil {\n\t\treturn fmt.Errorf(\"failed to encode passed object: %v\", err)\n\t}\n\n\t// Hash for skipping unnecessary writes\n\thashKey := string(key)\n\thashVal := blake2b.Sum256(buf.Bytes())\n\n\t// lastHash value or nil if it hasn't been hashed yet\n\tlastHash := b.bm.getHash(hashKey)\n\n\t// If the hashes are equal, skip the write\n\tif bytes.Equal(hashVal[:], lastHash) {\n\t\treturn nil\n\t}\n\n\t// New value: write it to the underlying boltdb\n\tif err := b.boltBucket.Put(key, buf.Bytes()); err != nil {\n\t\treturn fmt.Errorf(\"failed to write data at key %s: %v\", key, err)\n\t}","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/boltdd/boltdd.go#L297-L333","documentation":"boltdd.Bucket.Put wraps any failure of the msgpack encoder (codec.NewEncoder with structs.MsgpackHandle) that serializes the caller-supplied value before it is written to the underlying bolt bucket. The library cannot serialize the value, so nothing is written and the underlying encode error is surfaced verbatim. It indicates the passed value (or something reachable from it) is not msgpack-encodable.","triggerScenarios":"Calling Bucket.Put(key, val) where val's type cannot be encoded by go-msgpack: channels, funcs, or values containing them; types with no exported fields and no custom encoding; a nil pointer of an unregistered interface/oneof type; a custom MarshalMsgpack/encoding implementation returning an error.","commonSituations":"Passing a struct containing a func or channel field (e.g. a callback or stop channel) added during refactoring; passing types meant for JSON that msgpack rejects; upgrading hashicorp/go-msgpack and hitting stricter encoding behavior; accidentally passing a pointer to interface instead of a concrete value.","solutions":["Read the wrapped %v cause to identify the unencodable field or type in val.","Remove or tag (codec:'-' / msgpack omit) unencodable fields such as funcs, channels, or sync primitives from the struct.","Implement a custom encoding (MarshalMsgpack or codec handles) for types the encoder cannot handle.","Ensure the value passed is a concrete non-nil value whose type is registered if interfaces are involved."],"exampleFix":"// before\nb.Put([]byte(\"cfg\"), cfg) // cfg contains stopCh chan struct{}\n// after\ntype config struct {\n    Addr string\n    stopCh chan struct{} `codec:\"-\"` // excluded from encoding\n}\nb.Put([]byte(\"cfg\"), cfg)","handlingStrategy":"validation","validationCode":"func canMsgpackEncode(v interface{}) error {\n    var buf bytes.Buffer\n    return codec.NewEncoder(&buf, structs.MsgpackHandle).Encode(v)\n}\nif err := canMsgpackEncode(val); err != nil {\n    return fmt.Errorf(\"value not encodable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := b.Put(key, val); err != nil {\n    if strings.HasPrefix(err.Error(), \"failed to encode passed object\") {\n        // log offending type: fmt.Sprintf(\"%T\", val)\n        return err\n    }\n    return err\n}","preventionTips":["Keep persisted structs free of funcs, channels, and sync primitives (or tag them codec:\"-\").","Add a unit test that round-trips every persisted struct through Put/Get.","Keep persisted types concrete and non-nil; avoid pointers-to-interface.","Pin hashicorp/go-msgpack versions and review its changelog before upgrades."],"tags":["serialization","msgpack","encoding","storage"],"backgroundTag":"msgpack-encode-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}