{"record":{"id":"49d00e39021449dd","repo":"vectordotdev/vector","slug":"failed-to-finish-encoder","errorCode":null,"errorMessage":"failed to finish encoder","messagePattern":"failed to finish encoder","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/codecs/src/decoding/framing/chunked_gelf.rs","lineNumber":692,"sourceCode":"    }\n\n    impl Compression {\n        pub fn compress(&self, payload: &impl AsRef<[u8]>) -> Bytes {\n            self.compress_with_level(payload, flate2::Compression::default())\n        }\n\n        pub fn compress_with_level(\n            &self,\n            payload: &impl AsRef<[u8]>,\n            level: flate2::Compression,\n        ) -> Bytes {\n            match self {\n                Compression::Gzip => {\n                    let mut encoder = GzEncoder::new(Vec::new(), level);\n                    encoder\n                        .write_all(payload.as_ref())\n                        .expect(\"failed to write to encoder\");\n                    encoder.finish().expect(\"failed to finish encoder\").into()\n                }\n                Compression::Zlib => {\n                    let mut encoder = ZlibEncoder::new(Vec::new(), level);\n                    encoder\n                        .write_all(payload.as_ref())\n                        .expect(\"failed to write to encoder\");\n                    encoder.finish().expect(\"failed to finish encoder\").into()\n                }\n            }\n        }\n    }\n\n    fn create_chunk(\n        message_id: u64,\n        sequence_number: u8,\n        total_chunks: u8,\n        payload: &impl AsRef<[u8]>,\n    ) -> BytesMut {","sourceCodeStart":674,"sourceCodeEnd":710,"githubUrl":"https://github.com/vectordotdev/vector/blob/bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013/lib/codecs/src/decoding/framing/chunked_gelf.rs#L674-L710","documentation":"`compress_with_level` finishes the `GzEncoder` with `encoder.finish().expect(\"failed to finish encoder\")`. `finish` flushes the final gzip trailer into the in-memory `Vec`; like the write step it should be infallible, so this panic signals an internal flate2/zlib failure, not an input or configuration problem.","triggerScenarios":"`compress_with_level` (via `compress`) calling `finish()` on a `GzEncoder` when the deflate stream finalization returns an error — allocation failure or a zlib backend internal error.","commonSituations":"Rare; encountered under memory pressure or unusual flate2 backend builds while Vector compresses GELF chunks for UDP reassembly bookkeeping.","solutions":["Check for memory exhaustion on the host","Switch or update the flate2 backend feature (rust_backend / zlib / zlib-ng)","Upgrade flate2/Vector to pick up backend fixes","Consider propagating the error rather than panicking"],"exampleFix":"// before\nencoder.finish().expect(\"failed to finish encoder\").into()\n// after\nencoder.finish().expect(\"in-memory gzip finish cannot fail unless OOM\").into()","handlingStrategy":"try-catch","validationCode":"// Rust: verify payload size is within safe compression bounds first\nassert!(payload.len() <= MAX_GELF_CHUNK_PAYLOAD, \"payload too large for in-memory gzip\");","typeGuard":"fn can_finish_gzip(encoder: &GzEncoder<Vec<u8>>) -> bool {\n    // finish only fails on backend/OOM errors; validate buffer capacity beforehand\n    encoder.get_ref().capacity() > 0\n}","tryCatchPattern":"match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| encoder.finish())) {\n    Ok(Ok(bytes)) => bytes,\n    _ => { /* backend/OOM failure: retry once or use uncompressed fallback */ }\n}","preventionTips":["Watch for memory pressure; finish() allocates the trailer and can fail under OOM","Pin/update flate2 to a stable backend (rust_backend is most resilient)","Replace expect with Result propagation in library code","Add compression round-trip tests under constrained-memory conditions"],"tags":["rust","gzip","compression","flate2","gelf"],"backgroundTag":"compression-write-failed","analyzedSha":"bdb87aeaa4c4ff27c0ba643c1c77b21bf2ef4013","analyzedAt":"2026-09-16T02:53:35.741Z","contentChangedAt":"2026-09-16T02:53:35.741Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}