{"record":{"id":"2cd2d467b151d70c","repo":"pola-rs/polars","slug":"cannot-write-to-a-finished-stream","errorCode":null,"errorMessage":"Cannot write to a finished stream","messagePattern":"Cannot write to a finished stream","errorType":"exception","errorClass":"PolarsError","httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/io/ipc/write/stream.rs","lineNumber":93,"sourceCode":"            ipc_message: schema_to_bytes(\n                schema,\n                self.ipc_fields.as_ref().unwrap(),\n                self.custom_schema_metadata.as_deref(),\n            ),\n            arrow_data: vec![],\n        };\n        write_message(&mut self.writer, &encoded_message)?;\n        Ok(())\n    }\n\n    /// Writes [`RecordBatchT`] to the stream\n    pub fn write(\n        &mut self,\n        columns: &RecordBatchT<Box<dyn Array>>,\n        ipc_fields: Option<&[IpcField]>,\n    ) -> PolarsResult<()> {\n        if self.finished {\n            let io_err = std::io::Error::new(\n                std::io::ErrorKind::UnexpectedEof,\n                \"Cannot write to a finished stream\".to_string(),\n            );\n            return Err(PolarsError::from(io_err));\n        }\n\n        // we can't make it a closure because it borrows (and it can't borrow mut and non-mut below)\n        #[allow(clippy::or_fun_call)]\n        let fields = ipc_fields.unwrap_or(self.ipc_fields.as_ref().unwrap());\n\n        let (encoded_dictionaries, encoded_message) = encode_chunk(\n            columns,\n            fields,\n            &mut self.dictionary_tracker,\n            &self.write_options,\n        )?;\n\n        for encoded_dictionary in encoded_dictionaries {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-arrow/src/io/ipc/write/stream.rs#L75-L111","documentation":"Guard in IpcStreamWriter::write: write() was called after the stream was already terminated (finish() sets self.finished; writing a footer ends the stream). Writing further record batches into a finished stream would corrupt the file. Create a new writer instead.","triggerScenarios":"This panic/expect fires when execution reaches an unguarded state described by: \"Cannot write to a finished stream\". Typical triggers: unsupported dtype or feature-gated code path reached at runtime, invalid user input or environment variable value, calling API methods in the wrong order or on mismatched types, or data (lengths, offsets, ranges) violating the function's preconditions.","commonSituations":"Encountered when input data or configuration does not meet the preconditions of the throwing code path (\"Cannot write to a finished stream\"). Common cases: missing Cargo feature flags, mismatched dtypes/lengths between arrays or series, out-of-range temporal values, malformed environment variables, or operations on unsupported/complex nested types.","solutions":["Do not write to the IPC StreamWriter after `finish()` has been called; create a new writer for additional output.","Reorder the code so all batches are written before finishing the stream."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}