{"record":{"id":"20ff2f7b4a8bd52b","repo":"pola-rs/polars","slug":"failed-to-encode-spill-file-for-context-id-e","errorCode":null,"errorMessage":"failed to encode spill file for '{context_id}': {e}","messagePattern":"failed to encode spill file for '(.+?)': (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/polars-ooc/src/spill_frame.rs","lineNumber":41,"sourceCode":"        self.estimated_size()\n    }\n\n    async fn spill(&self, context_id: &str) -> Self::Spilled {\n        let mut df = self.clone();\n        let context_id = context_id.to_owned();\n\n        // Encode in the current task (on computational executor).\n        let mut buf = Vec::new();\n\n        let mut writer = IpcStreamWriter::new(&mut buf).with_compat_level(CompatLevel::newest());\n        let clvl = polars_config::config().ooc_spill_compression_level();\n        if clvl > 0 {\n            let zstd_lvl = ZstdLevel::try_new(clvl.try_into().unwrap()).unwrap();\n            writer = writer.with_compression(Some(IpcCompression::ZSTD(zstd_lvl)));\n        }\n        writer\n            .finish(&mut df)\n            .unwrap_or_else(|e| panic!(\"failed to encode spill file for '{context_id}': {e}\",));\n\n        // Do file creation / writing on tokio.\n        let file = ASYNC\n            .spawn(async move {\n                let size = buf.len() as u64;\n                let spill_file = SpillFile::new(&context_id, \"ipc\", size);\n                if BYTES_SPILLED_TO_DISK.fetch_add(size).saturating_add(size)\n                    > polars_config::config().ooc_disk_budget_bytes()\n                {\n                    spill_file.creation_aborted();\n                    polars_error::abort::polars_abort_ooc_out_of_disk();\n                }\n                match tokio::fs::write(spill_file.path(), buf).await {\n                    Ok(()) => spill_file,\n                    Err(e) => {\n                        let msg = format!(\n                            \"failed to create spill file '{}': {e}\",\n                            spill_file.path().display()","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-ooc/src/spill_frame.rs#L23-L59","documentation":"During out-of-core spill, the DataFrame is encoded to an in-memory IPC (Arrow IPC with optional zstd) buffer via writer.finish(). If encoding fails, the code panics with 'failed to encode spill file' plus the context id and error. Spilling is treated as infallible by design, so any IPC write error aborts the process.","triggerScenarios":"Calling spill() (crates/polars-ooc/src/spill_frame.rs:41) when IpcWriter::finish fails — typically due to an invalid/unsupported compression level configured via the OOC compression setting (ZstdLevel::try_new edge cases), IPC-unsupported column types/dtypes in the frame, or an internal writer error.","commonSituations":"Out-of-core streaming queries on frames containing dtypes the IPC writer cannot serialize; misconfigured compression level env/config for zstd; corrupted execution state under memory pressure.","solutions":["Check/correct the configured OOC compression level (clvl) — an out-of-range zstd level is the most likely cause; set it to a valid 1–22 value or 0 to disable compression.","Cast or drop columns with dtypes the IPC writer cannot encode before enabling OOC spill.","Upgrade polars: newer versions may support the dtype or handle the writer error gracefully.","Reduce memory pressure so OOC spill isn't needed (chunk sizes, streaming parameters), or report the inner error to polars if it looks like a bug."],"exampleFix":"// before\nPOLARS_OOC_COMPRESSION_LEVEL=99  // invalid zstd level\n// after\nPOLARS_OOC_COMPRESSION_LEVEL=3   // valid zstd level (1-22), or 0 to disable","handlingStrategy":"validation","validationCode":"// validate config and dtypes before enabling OOC spill\nlet clvl: i64 = parse_config(\"POLARS_OOC_COMPRESSION_LEVEL\");\nassert!((0..=22).contains(&clvl), \"zstd level must be 0-22\");\n// ensure all columns use IPC-encodable dtypes before spilling","typeGuard":null,"tryCatchPattern":"// spill panics on encode failure; protect the driver loop\nstd::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run_ooc_query())).map_err(|_|\n    \"spill encode failed: check compression level config and column dtypes\")","preventionTips":["Keep OOC compression level within 1–22 (or 0 for none).","Restrict spilled frames to IPC-supported dtypes; cast exotic columns up front.","Keep polars updated for IPC writer fixes.","Log the full panic message including the inner error to diagnose the writer failure."],"tags":["ipc","serialization","ooc","compression","panic"],"backgroundTag":"json-serialization-failed","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}