Skip to content

MP4: a write error panics in AtomWriterCompanion::drop instead of returning Err #738

Closed

Description

@sfchun

Version: lofty 0.25.4 (still present on main).

Problem

impl Drop for AtomWriterCompanion (lofty/src/mp4/write.rs) asserts:

assert!(
    self.finished || std::thread::panicking(),
    "`AtomWriterCompanion` was not finished"
);

In lofty/src/mp4/ilst/write.rs, write_to calls atom_writer.start_write() and then runs several fallible steps with ? before reaching write_handle.finish(). Examples are the seek, build_ilst(...).map_err(TagEncodingError::from)? and the atom_tree / nested_atom lookups mapped through handle_atom_parse_error. When any of them returns Err, the companion is dropped unfinished. The Drop impl then panics instead of returning the error to the caller of save_to / save_to_path.

  • With panic = "unwind", a recoverable LoftyError becomes a panic.
  • With panic = "abort", which many desktop applications use, writing a tag to a malformed or unusual MP4 aborts the whole process.

Expected

The write returns the Err. Possible fixes:

  • make the check a debug_assert!, or
  • make Drop a no-op and let finish() be the only commit point (the companion's buffered contents are simply discarded on an early return).

Context

We currently work around this downstream by pre-validating MP4 files before every tag write, which duplicates parsing that lofty already does.

Related

A second MP4 write issue that we also hit, atom_tree returning index 0 when no ilst exists (so a meta with hdlr and no ilst loses its hdlr on write), is already listed as item 023 in #703 and is still present on main. +1 for that one as well.

Activity

  1. added
    bug
    on Oct 6, 2026
  2. added this to the 0.25.5 milestone on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions