pola-rs/polars · error

File to have ended

Error message

File to have ended

What it means

into_inner_and_metadata returns the writer plus ThriftFileMetadata, but the metadata is Some only after end() ran; calling it before end() makes metadata.expect("File to have ended") panic, i.e. the file writer was still mid-stream.

Solutions

  1. Call writer.end() (and propagate its Result) before into_inner_and_metadata
  2. If abandoning the file due to an error, drop the writer instead of extracting metadata
  3. Ensure finalize runs even on early returns (scopeguard/cleanup)

Example fix

// before
let (w, meta) = writer.into_inner_and_metadata(); // panics
// after
writer.end(&mut dynamics)?;
let (w, meta) = writer.into_inner_and_metadata();
Defensive patterns

Strategy: validation

Validate before calling

// Rust: only extract after successful finalize
let (writer, meta) = match writer.end(&mut dynamics) {
    Ok(()) => writer.into_inner_and_metadata(),
    Err(e) => return Err(e),
};

Prevention

When it happens

Trigger: Calling into_inner_and_metadata() on a FileWriter before finish/end() has serialized the footer metadata.

Common situations: Early-return paths in sink implementations skipping the finalize step; error handling that abandons the writer then tries to reclaim the inner writer via this method; forgetting the explicit end() call in custom write loops.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/20510d12e8359cf8. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-parquet/src/parquet/write/file.rs:262

            None,
        );

        let len = end_file(&mut self.writer, &metadata)?;
        self.state = State::Finished;
        self.metadata = Some(metadata);
        Ok(self.offset + len)
    }

    /// Returns the underlying writer.
    pub fn into_inner(self) -> W {
        self.writer
    }

    /// Returns the underlying writer and [`ThriftFileMetadata`]
    /// # Panics
    /// This function panics if [`Self::end`] has not yet been called
    pub fn into_inner_and_metadata(self) -> (W, ThriftFileMetadata) {
        (self.writer, self.metadata.expect("File to have ended"))
    }
}

View on GitHub (pinned to fe841f959e)