{"record":{"id":"394c3ee2e32c0ecd","repo":"hashicorp/nomad","slug":"failed-to-decode-data-into-passed-object-v","errorCode":null,"errorMessage":"failed to decode data into passed object: %v","messagePattern":"failed to decode data into passed object: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/boltdd/boltdd.go","lineNumber":353,"sourceCode":"\t// New value written, store hash (bucket path map was created above)\n\tb.bm.setHash(hashKey, hashVal[:])\n\n\treturn nil\n\n}\n\n// Get value by key from boltdb or return an ErrNotFound error if key not\n// found.\nfunc (b *Bucket) Get(key []byte, obj interface{}) error {\n\t// Get the raw data from the underlying boltdb\n\tdata := b.boltBucket.Get(key)\n\tif data == nil {\n\t\treturn NotFound(string(key))\n\t}\n\n\t// Deserialize the object\n\tif err := codec.NewDecoderBytes(data, structs.MsgpackHandle).Decode(obj); err != nil {\n\t\treturn fmt.Errorf(\"failed to decode data into passed object: %v\", err)\n\t}\n\n\treturn nil\n}\n\n// Iterate iterates each key in Bucket b that starts with prefix. fn is called on\n// the key and msg-pack decoded value. If prefix is empty or nil, all keys in the\n// bucket are iterated.\n//\n// b must already exist.\nfunc Iterate[T any](b *Bucket, prefix []byte, fn func([]byte, T)) error {\n\tc := b.boltBucket.Cursor()\n\tfor k, data := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, data = c.Next() {\n\t\tvar obj T\n\t\tif err := codec.NewDecoderBytes(data, structs.MsgpackHandle).Decode(&obj); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to decode data into passed object: %v\", err)\n\t\t}\n\t\tfn(k, obj)","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/boltdd/boltdd.go#L335-L371","documentation":"boltdd.Bucket.Get wraps a failure of the msgpack decoder when unmarshaling the stored bytes into the caller-supplied obj pointer. The key existed (otherwise a NotFound error is returned) but its stored representation cannot be decoded into the target type. Typically the stored schema and the Go struct have drifted.","triggerScenarios":"Calling Get(key, &obj) where the bytes at key were written by an older version with a different struct layout (renamed/retyped fields, changed msgpack field ordering); the stored bytes were written with a different codec/handle; obj is not a pointer or is an incompatible type for the encoded shape.","commonSituations":"Consul/serf state files written by an older binary read after an upgrade; changing a field's Go type (e.g. string to time.Duration) in a persisted struct; reusing a bucket key for a different type; passing a non-pointer or wrong-typed obj to Get.","solutions":["Check the wrapped %v decode error for the offending field index/type mismatch.","Verify obj is a non-nil pointer to a type matching the struct originally written with Put.","If the schema changed, migrate old records (decode with the old struct version, re-encode with the new one) or keep msgpack field order/types stable.","Use a versioned payload struct (wrap data in {Version, Body}) so future changes can be handled explicitly."],"exampleFix":"// before\nvar meta NodeMeta\nbucket.Get(key, &meta) // stored layout has field 3 as int64, meta has time.Duration\n// after\nvar meta nodeMetaV1 // legacy struct matching on-disk layout\nif err := bucket.Get(key, &meta); err == nil {\n    meta2 := upgrade(meta) // migrate to current struct\n}","handlingStrategy":"try-catch","validationCode":"// ensure obj is a non-nil pointer of the expected type before calling Get\nrv := reflect.ValueOf(obj)\nif rv.Kind() != reflect.Ptr || rv.IsNil() {\n    return errors.New(\"Get requires a non-nil pointer\")\n}","typeGuard":"func isReadableInto(obj interface{}) bool {\n    if obj == nil { return false }\n    rv := reflect.ValueOf(obj)\n    return rv.Kind() == reflect.Ptr && !rv.IsNil() && rv.Elem().CanSet()\n}","tryCatchPattern":"var meta NodeMeta\nif err := bucket.Get(key, &meta); err != nil {\n    if IsNotFound(err) {\n        // key absent: create default\n    } else if strings.Contains(err.Error(), \"failed to decode\") {\n        // schema drift: log stored bytes hash, run migration path\n    }\n    return err\n}","preventionTips":["Never change the Go type of an existing persisted field; add new fields only (append-only schema).","Wrap persisted payloads in a versioned envelope so old records can be decoded by old structs.","Round-trip test persisted structs (Put then Get) in CI.","Always pass a non-nil pointer of the same struct type used at Put time."],"tags":["serialization","msgpack","decoding","schema-mismatch"],"backgroundTag":"msgpack-decode-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"}