{"record":{"id":"289313d226a61eb6","repo":"apache/beam","slug":"wrong-decoding-sequence-found","errorCode":null,"errorMessage":"wrong decoding sequence found.","messagePattern":"wrong decoding sequence found\\.","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/combiners/src/main/java/org/apache/beam/sdk/extensions/combiners/Histogram.java","lineNumber":534,"sourceCode":"      INT_CODER.encode(numBuckets, outStream);\n\n      INT_CODER.encode(numBuckets - numEmptyBucketsAtTheEnd, outStream);\n      for (int i = 0; i < numBuckets - numEmptyBucketsAtTheEnd; i++) {\n        LONG_CODER.encode(value.counts[i], outStream);\n      }\n    }\n\n    @Override\n    public HistogramAccumulator decode(InputStream inStream) throws IOException {\n      int coder = inStream.read();\n      int numBuckets = INT_CODER.decode(inStream);\n\n      if (coder == CoderType.NON_EMPTY_BUCKETS_CODER.ordinal()) {\n        return decodeNonEmptyBuckets(numBuckets, inStream);\n      } else if (coder == CoderType.ALL_BUCKETS_CODER.ordinal()) {\n        return decodeAllBuckets(numBuckets, inStream);\n      } else {\n        throw new CoderException(\"wrong decoding sequence found.\");\n      }\n    }\n\n    private HistogramAccumulator decodeNonEmptyBuckets(int numBuckets, InputStream inStream)\n        throws IOException {\n      HistogramAccumulator histogramAccumulator = new HistogramAccumulator(numBuckets);\n      int nonEmptyBucketCounts = INT_CODER.decode(inStream);\n\n      for (int i = 0; i < nonEmptyBucketCounts; i++) {\n        histogramAccumulator.counts[INT_CODER.decode(inStream)] = LONG_CODER.decode(inStream);\n      }\n\n      return histogramAccumulator;\n    }\n\n    private HistogramAccumulator decodeAllBuckets(int numBuckets, InputStream inStream)\n        throws IOException {\n      HistogramAccumulator histogramAccumulator = new HistogramAccumulator(numBuckets);","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/combiners/src/main/java/org/apache/beam/sdk/extensions/combiners/Histogram.java#L516-L552","documentation":"The Histogram coder encodes an accumulator with a one-byte CoderType discriminator (NON_EMPTY_BUCKETS_CODER or ALL_BUCKETS_CODER). During decode, any discriminator value outside the known ordinals means the byte stream does not match this coder's encoding, so CoderException('wrong decoding sequence found.') is thrown. This protects against decoding data written by a different coder or corrupted stream.","triggerScenarios":"Decoding bytes that were not produced by HistogramCoder; deserializing persisted accumulator state written by an older/newer Beam version whose encoding changed; a stream positioned incorrectly (offset/truncation) so the discriminator byte is garbage.","commonSituations":"Job upgrades: state/checkpoints or test fixtures encoded with a previous HistogramCoder format are decoded after a library update; manually crafted byte arrays in unit tests; mixing accumulators from different histogram definitions.","solutions":["Ensure the same Beam version encodes and decodes the histogram state; re-encode any persisted accumulators after upgrading.","Verify the InputStream is positioned at the start of a HistogramCoder-encoded payload (no offset/truncation).","Confirm the coder used to write the bytes is HistogramCoder, not another coder for the same type.","Wrap decode in try/catch(CoderException) and rebuild the histogram from source data as a fallback."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (bytes == null || bytes.length == 0 || (bytes[0] != NON_EMPTY_BUCKETS_CODER && bytes[0] != ALL_BUCKETS_CODER)) {\n  throw new IOException(\"payload is not a HistogramCoder-encoded accumulator\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  acc = coder.decode(inStream, Coder.Context.OUTER);\n} catch (CoderException e) {\n  LOG.warn(\"corrupt/foreign histogram encoding, rebuilding\", e);\n  acc = new HistogramAccumulator(numBuckets);\n}","preventionTips":["Keep the Beam version identical between writer and reader of encoded state.","Re-encode persisted accumulators after upgrading Beam.","Never handcraft encoded bytes in tests; use the coder's round-trip encode()."],"tags":["java","coder","deserialization","corrupt-data"],"backgroundTag":"unexpected-response-shape","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}