{"record":{"id":"359ed7788d9c0b1e","repo":"hashicorp/nomad","slug":"failed-to-encode-request-v","errorCode":null,"errorMessage":"Failed to encode request: %v","messagePattern":"Failed to encode request: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/rpc.go","lineNumber":792,"sourceCode":"\tvar ack structs.StreamingRpcAck\n\tif err := decoder.Decode(&ack); err != nil {\n\t\tconn.Close()\n\t\treturn nil, err\n\t}\n\n\tif ack.Error != \"\" {\n\t\tconn.Close()\n\t\treturn nil, errors.New(ack.Error)\n\t}\n\n\treturn conn, nil\n}\n\n// raftApplyFuture is used to encode a message, run it through raft, and return the Raft future.\nfunc (s *Server) raftApplyFuture(t structs.MessageType, msg any) (raft.ApplyFuture, error) {\n\tbuf, err := structs.Encode(t, msg)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Failed to encode request: %v\", err)\n\t}\n\n\t// Warn if the command is very large\n\tif n := len(buf); n > raftWarnSize {\n\t\ts.logger.Warn(\"attempting to apply large raft entry\", \"raft_type\", t, \"bytes\", n)\n\t}\n\n\tfuture := s.raft.Apply(buf, enqueueLimit)\n\treturn future, nil\n}\n\n// raftApplyFn is the function signature for applying a msg to Raft\ntype raftApplyFn func(t structs.MessageType, msg any) (any, uint64, error)\n\n// raftApply is used to encode a message, run it through raft, and return the\n// FSM response along with any errors. If the FSM.Apply response is an error it\n// will be returned as the error return value with a nil response.\nfunc (s *Server) raftApply(t structs.MessageType, msg any) (any, uint64, error) {","sourceCodeStart":774,"sourceCodeEnd":810,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/rpc.go#L774-L810","documentation":"raftApplyFuture encodes a message (type + payload) with structs.Encode before writing it to the raft log. If the message cannot be messagepack-encoded, raft is never touched and this error is returned. It almost always indicates a malformed or unencodable payload rather than a cluster problem.","triggerScenarios":"raftApply/applyPlan submitting a message whose contents cannot be encoded — unsupported types, corrupted/nil nested structs, or version skew where a struct contains fields the encoder of this server cannot handle.","commonSituations":"Version-skewed clusters where a newer client submits structs with unknown fields; forked code adding unencodable types; corrupted request objects from buggy callers of raftApply.","solutions":["Check cluster version skew and upgrade servers to matching Nomad versions","Inspect server logs for the wrapped encode error to identify the offending struct","Fix the caller building the malformed message (e.g. nil/unsupported fields)","Reproduce with a minimal payload to isolate which field fails messagepack encoding"],"exampleFix":"// before\nreq := &structs.JobRegisterRequest{Job: nil}\n_, err := srv.raftApply(structs.JobRegisterRequestType, req)\n// after\nreq := &structs.JobRegisterRequest{Job: validJob, WriteRequest: structs.WriteRequest{Region: region}}\n_, err := srv.raftApply(structs.JobRegisterRequestType, req)","handlingStrategy":"validation","validationCode":"buf, err := structs.Encode(t, msg)\nif err != nil {\n    return fmt.Errorf(\"refusing raft apply, unencodable payload: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"future, err := srv.raftApplyFuture(t, msg)\nif err != nil && strings.Contains(err.Error(), \"Failed to encode request\") {\n    return fmt.Errorf(\"payload for %s not encodable; check for nil/unsupported fields: %w\", t, err)\n}","preventionTips":["Keep server/client versions aligned to avoid unknown struct fields","Reject nil or partially initialized request structs before raft apply","Add encode smoke tests for custom message types"],"tags":["nomad","raft","encoding","messagepack","internal-api"],"backgroundTag":"request-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"}