pola-rs/polars · error · PolarsError
Cannot write to a finished stream
Error message
Cannot write to a finished stream
What it means
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.
Source
Thrown at crates/polars-arrow/src/io/ipc/write/stream.rs:93
ipc_message: schema_to_bytes(
schema,
self.ipc_fields.as_ref().unwrap(),
self.custom_schema_metadata.as_deref(),
),
arrow_data: vec![],
};
write_message(&mut self.writer, &encoded_message)?;
Ok(())
}
/// Writes [`RecordBatchT`] to the stream
pub fn write(
&mut self,
columns: &RecordBatchT<Box<dyn Array>>,
ipc_fields: Option<&[IpcField]>,
) -> PolarsResult<()> {
if self.finished {
let io_err = std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"Cannot write to a finished stream".to_string(),
);
return Err(PolarsError::from(io_err));
}
// we can't make it a closure because it borrows (and it can't borrow mut and non-mut below)
#[allow(clippy::or_fun_call)]
let fields = ipc_fields.unwrap_or(self.ipc_fields.as_ref().unwrap());
let (encoded_dictionaries, encoded_message) = encode_chunk(
columns,
fields,
&mut self.dictionary_tracker,
&self.write_options,
)?;
for encoded_dictionary in encoded_dictionaries {View on GitHub (pinned to 9b5d73fd00)
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.
Defensive patterns
Strategy: validation
When it happens
Trigger: 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.
Common situations: 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.
AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19).
Data as JSON: /api/errors/2cd2d467b151d70c.
Report an issue: GitHub.