Skip to content

fix(pack): a frame too long for its u32 length fails the export instead of wrapping - #2418

Merged
aaj3f merged 4 commits into
mainfrom
fix/pack-frame-length-checked
Nov 19, 2026
Merged

aaj3f merged 4 commits into
mainfrom
fix/pack-frame-length-checked

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2416.

encode_data_frame wrote the payload length with payload.len() as u32 and then appended the whole payload, so a CAS object of 4 GiB or more got a wrapped length while all of its bytes still went into the stream. The export said it worked, and the pack only failed later on restore, with an error (integrity check failed, or payload too large quoting a size that isn't the object's) that points nowhere near the cause. The header, error and manifest encoders had the same cast.

What changes

  • checked_frame_len (private, fluree-db-core/src/pack.rs): every frame length now goes through it, and a length that doesn't fit a u32 is the new PackError::FrameTooLarge { frame, size }, which names the object by CID (or says which frame it is) and its size, e.g. object <cid> is 4294967296 bytes, more than a pack frame can hold (4294967295 bytes).
  • encode_header_frame, encode_data_frame, encode_error_frame and encode_manifest_frame return PackResult<()> and write nothing to out on that error. The data frame's head (type byte, CID, length) is its own private function, encode_data_frame_head, which is what lets the bound be tested without a 4 GiB payload.
  • stream_pack_inner and stream_archive (fluree-db-api/src/pack.rs) propagate it like any other producer error. So a local fluree export --format ledger fails with archive generation failed: object … is … bytes, … and removes the partial .flpack. Over POST /pack, the client gets the same message in an Error frame (remote pack error: …).
  • A failed archive removes its file on every path. fluree export -o already removed the partial .flpack on a producer error, but fluree materialize --output pack and the Python binding's archive() returned the error and left a truncated file under the name asked for, which is the same "looks fine" artifact this PR is about, and FrameTooLarge is one more way to get there. The CLI's two local file paths now share archive_into_file (commands/export.rs), which closes the file and removes it on any error, and the Python binding (fluree-db-python/src/connection.rs) does the same when the archive or its final flush fails. A file that couldn't be created is never touched.
  • stream_pack's own error path is the one place a failed encode can't just propagate: if its error message were ever too long for a frame, it falls back to a short fixed message, so the client still gets an Error frame rather than a bare End that reads as success. I don't think a message can realistically get there, but a bare End would be the same "looks fine" failure this PR is about.
  • The CLI's remote archive splice (remote_client.rs) maps a failed manifest encode to InvalidRequest, since that manifest is built locally.
  • docs/operations/pack-archive-restore.md now says the 4 GiB limit exists and what export does at it.

No format change: v1 still can't describe a frame of 4 GiB or more, and the issue has the v2-preamble path if we ever want it to.

Breaking changes

For an embedder that writes pack streams itself: the four encode_*_frame functions above in fluree_db_core::pack now return PackResult<()> (add a ?, or handle PackError::FrameTooLarge), and PackError has a new variant, so an exhaustive match on it needs an arm. encode_end_frame and write_stream_preamble don't change.

Tests

  • test_data_frame_too_large_for_its_length_field_is_an_error: a data-frame head for u32::MAX + 1 bytes is FrameTooLarge, naming the CID and the size, and writes nothing.
  • test_data_frame_head_at_u32_max_writes_that_length: u32::MAX itself is written as is.
  • test_checked_frame_len_at_and_past_u32_max: the helper at both bounds, and the error's message.
  • a_failed_archive_removes_its_file (CLI) and test_a_failed_archive_leaves_no_file (Python): an archive that fails after its file is created (a ledger that doesn't exist, and one dropped under its handle) leaves no file behind.

The two past-the-limit tests are #[cfg(target_pointer_width = "64")], since u32::MAX as usize + 1 doesn't exist on a 32-bit target (the wasm32 job builds but doesn't run tests). With checked_frame_len put back to an unchecked len as u32, both fail and the other 31 pack tests pass. The existing callers in tests (pack's own, it_flpack_round_trip, the CLI's remote_client tests and pack_client's) just unwrap.

The issue's realistic path to 4 GiB (one predicate's xsd:decimal arena) isn't exercised end to end; the 1 GiB restore cap in #2417 is what you'd hit first there anyway.

Verification

On a Mac at 945e86899, only for the touched crates (CI runs the full gate):

  • cargo fmt (touched crates) and cargo clippy -p fluree-db-core -p fluree-db-nameservice-sync -p fluree-db-api -p fluree-db-cli --all-targets -- -D warnings: clean.
  • cargo test -p fluree-db-core --lib pack::: 33 passed. cargo test -p fluree-db-api --test grp_import flpack: 9 passed. cargo test -p fluree-db-api --lib pack::: 1 passed. cargo test -p fluree-db-cli --lib remote_client: 25 passed. cargo test -p fluree-db-nameservice-sync --lib pack_client: 4 passed.

On a Linux build box at 2bcf27ef9 (the cleanup commit): cargo fmt --all --check and cargo clippy -p fluree-db-cli -p fluree-db-python --all-features --all-targets -- -D warnings clean; cargo test -p fluree-db-cli --lib 290 passed; the wheel built with maturin build --locked, and pytest fluree-db-python/tests 397 passed, 7 skipped.

🤖 Generated with Claude Code

aaj3f and others added 2 commits November 16, 2026 13:34
…ad of wrapping

encode_data_frame wrote the payload length as `payload.len() as u32`
and then appended the whole payload, so an object of 4 GiB or more got a
wrapped length while every byte still went into the stream. The export
reported success and the pack failed only when read back: a short
payload that fails the CID check, or a "payload too large" error
quoting a size that isn't the object's.

Every frame length now goes through checked_frame_len, which returns
PackError::FrameTooLarge (naming the object's CID, or the frame, and
its size) when the length doesn't fit, and the encode_*_frame functions
return PackResult<()> and write nothing on that error. The pack and
archive producers propagate it, so export fails with that message and
the CLI removes the partial file as it does for any producer error. The
pack stream's error path falls back to a short message if its own error
were ever too long for a frame, so the client still gets an Error frame
rather than a bare End.

The data frame's head is split from its payload so the bound can be
tested at u32::MAX and one past it without a 4 GiB payload; both new
tests fail with the unchecked cast. No format change.

Fixes #2416

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… every path

`fluree export --format ledger -o` removed the partial `.flpack` when the
archive failed, but `fluree materialize --output pack` and the Python
`archive()` returned the error and left a truncated file under the name
asked for, where it would pass for a finished pack until a restore
failed on it. `FrameTooLarge` is one more way to get there.

The CLI's two local file paths now share `archive_into_file`, which
archives into the file it is handed, closes it, and removes it on any
error. The Python binding removes its file the same way when the archive
or the final flush fails. A file that could not be created is never
touched.

Tests: `a_failed_archive_removes_its_file` (CLI, an archive of a ledger
that doesn't exist) and `test_a_failed_archive_leaves_no_file` (Python, a
ledger dropped under its handle).

Refs #2416

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aaj3f
aaj3f marked this pull request as ready for review November 17, 2026 17:18
@aaj3f
aaj3f requested a review from bplatz November 17, 2026 17:18

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Please review the inline comments before merging.

Comment thread fluree-db-cli/src/commands/export.rs Outdated
/// On an error the partial file is removed: left under the name the user
/// asked for, it would pass for a finished `.flpack` until a restore failed
/// on it.
pub(crate) async fn archive_into_file(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Python's archive() (fluree-db-python/src/connection.rs:1030) repeats this cleanup. One Fluree::archive_ledger_to_path in fluree-db-api could serve export, materialize and Python, and writing a temp file then renaming on success would also stop a failed export from deleting an existing archive at that path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on both, and the second one is a real bug I'd missed, not just a nicety: File::create truncates whatever is at path before the archive even starts, so a failed fluree export mydb -o mydb.flpack over yesterday's good archive didn't just clean up its own partial file, it had already destroyed the good one.

In d9e1328 there's now one Fluree::archive_ledger_to_path in fluree-db-api. It writes to a hidden .partial sibling (same directory, so the rename stays on one filesystem), syncs it, and renames it over path only once the archive is complete. On an error it removes the .partial and leaves path exactly as it was. fluree export -o, fluree materialize --output pack and Python's archive() all call it now, and the CLI's archive_into_file is gone.

One edge I made a call on, which you may want to weigh in on: a path that exists but isn't a regular file is written in place, like before, and left alone on error. A rename can't stand in for writing to /dev/stdout or a FIFO, and over a symlink it would replace the link instead of writing through it. If you'd rather we refuse those outright, I'm happy to flip it.

Tests: archive_to_path_lands_only_a_finished_archive in it_flpack_round_trip covers a failed archive to a fresh path (nothing left, no .partial either), a failed archive over an existing file (kept byte for byte), and a finished one replacing it with the same bytes archive_ledger streams, which then restores. On the Python side, test_a_failed_archive_keeps_the_file_already_there checks the same through ledger.archive().

Comment thread docs/operations/pack-archive-restore.md Outdated
- **Storage-agnostic destination**: The restored ledger can live on file storage, S3, or any backend that implements the `Storage` trait — independent of where the archive's bytes came from.
- **Concurrent CAS writes**: A large archive holds tens of thousands of CAS objects (DBLP: ~74k). Restore decodes frames sequentially (cheap) but dispatches the per-object storage writes into a bounded pool so they overlap — otherwise wall-clock is the *sum* of per-object round-trips (minutes of pure latency on S3). The pool is bounded by both count and bytes-in-flight, so a burst of large dictionary packs can't balloon memory. This still doesn't make a 20 GB+ restore instantaneous — the archive is read as one sequential stream — so a genuinely huge restore belongs on a long-lived worker, not a short-timeout serverless function.
- **Per-frame size**: each data frame carries one whole CAS object. The indexer packs dictionaries to a 256 MiB target, so a single pack can land just over 256 MiB. Restore decodes with a 1 GiB per-frame ceiling (the frame length is a `u32`; every frame is SHA-256-verified) so these oversized-but-legitimate objects load. The conservative 256 MiB pull-path default would reject them — restore deliberately uses the larger cap.
- **Per-frame size**: each data frame carries one whole CAS object. The indexer packs dictionaries to a 256 MiB target, so a single pack can land just over 256 MiB. Restore decodes with a 1 GiB per-frame ceiling (the frame length is a `u32`; every frame is SHA-256-verified) so these oversized-but-legitimate objects load. The conservative 256 MiB pull-path default would reject them — restore deliberately uses the larger cap. Because the length is a `u32`, no frame can carry an object of 4 GiB or more; export fails with an error that names the object rather than writing a frame it couldn't describe.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: an object between 1 GiB and 4 GiB still exports cleanly and then fails restore (#2417). Worth saying so here.

Suggested change
- **Per-frame size**: each data frame carries one whole CAS object. The indexer packs dictionaries to a 256 MiB target, so a single pack can land just over 256 MiB. Restore decodes with a 1 GiB per-frame ceiling (the frame length is a `u32`; every frame is SHA-256-verified) so these oversized-but-legitimate objects load. The conservative 256 MiB pull-path default would reject them — restore deliberately uses the larger cap. Because the length is a `u32`, no frame can carry an object of 4 GiB or more; export fails with an error that names the object rather than writing a frame it couldn't describe.
- **Per-frame size**: each data frame carries one whole CAS object. The indexer packs dictionaries to a 256 MiB target, so a single pack can land just over 256 MiB. Restore decodes with a 1 GiB per-frame ceiling (the frame length is a `u32`; every frame is SHA-256-verified) so these oversized-but-legitimate objects load. The conservative 256 MiB pull-path default would reject them — restore deliberately uses the larger cap. Because the length is a `u32`, no frame can carry an object of 4 GiB or more; export fails with an error that names the object rather than writing a frame it couldn't describe. An object over the 1 GiB restore ceiling but under 4 GiB still exports, and its restore fails with `payload too large`.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, that's exactly the gap #2417 is about, and it's better said here than discovered on a restore. Took your suggestion as-is in d9e1328. I checked it against the code before committing: the restore ceiling is RESTORE_MAX_FRAME_PAYLOAD (1 GiB, fluree-db-api/src/commit_transfer.rs), and the decoder's error is PackError::PayloadTooLarge, which renders as payload too large: … (max …), so the wording matches what a user would actually see.

In the same commit I also added a short paragraph to docs/cli/export.md about the write-to-.partial-then-rename behavior from the other thread, since it changes what -o does to a file that's already there.

@bplatz bplatz added bug Something isn't working as expected breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section labels Nov 18, 2026
fluree export -o, materialize --output pack and Python's archive() each
created the file at the requested path, streamed into it, and removed it
on error. File::create truncates, so a failed export over an existing
archive had already destroyed it before the cleanup ran, and the cleanup
lived in two places.

Fluree::archive_ledger_to_path now serves all three: it writes to a
hidden .partial sibling, syncs it, and renames it over the path once the
archive is complete. A failed archive removes the .partial and leaves
the path as it was. A path that exists but isn't a regular file
(/dev/stdout, a FIFO, a symlink) is written in place, as before.

Docs: say that an object between the 1 GiB restore ceiling and 4 GiB
exports but fails restore (#2417), and how export -o writes its file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aaj3f
aaj3f merged commit 56d4b65 into main Nov 19, 2026
21 checks passed
@aaj3f
aaj3f deleted the fix/pack-frame-length-checked branch November 19, 2026 17:25
bplatz added a commit that referenced this pull request Nov 19, 2026
Brings #2418, #2419 and #2447, each of which applies to next.

- #2418 (frame lengths checked): every frame encoder returns PackResult
  and writes nothing on FrameTooLarge, next's own included. The erasure
  and authorization frames go through the checked JSON encoder with the
  manifest, the erasure artifact frame checks its length, and the
  erasure transfer's frames, its archive manifest and the retention
  journal manifest propagate the error. The stream's error path and
  next's retention-section error path share main's fallback, an Error
  frame with a short message, in encode_error_frame_or_short. The CLI's
  remote splice keeps next's erased-history manifest. main's archive
  test creates its ledger, as a commit needs on next.
- #2419 (the ?var = <const> memo keyed by the constant): applies as is.
  Its test seeds through next's async genesis_ledger.
- #2447 (the parity gate reads "1"^^xsd:boolean as true): applies as is.
@aaj3f aaj3f mentioned this pull request Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.flpack writer wraps a frame length at 4 GiB (payload.len() as u32), so the export succeeds and the pack can't be restored

2 participants