Skip to content

fix(log): claim the archive name and verify it before deleting logs (#255) - #274

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/255-rotate-logs-destroys-archive
Jul 31, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/255-rotate-logs-destroys-archive

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #255.

The bug, confirmed

rotate_logs named the archive after a month, tar -czf opens with O_TRUNC, and the function deletes the originals once tar returns 0. So the second rotation of a calendar month replaced the first one's archive with its own contents, and the logs that had been inside it were already gone from disk. Nothing survived anywhere, and nothing said so — tar returns 0 whether it created the month's archive or overwrote it.

Red first, on main's behaviour:

E  AssertionError: ['memory-2026-06-21.log', 'memory-2026-06-22.log'] exist nowhere:
   deleted from disk by the first rotation and then overwritten in the archive by the second (#255)
E  AssertionError: logs-2026-07.tar.gz was rewritten by a later rotation
E  AssertionError: ['memory-2026-07-12.log', 'memory-2026-07-13.log'] were deleted although
   the archive does not contain them — tar exited 0 and that was taken as proof

One correction to the issue's analysis

The issue treats "make the name exact" as a live option. It is not, but not for the reason the pre-flight suggested either.

archive_month really is date -v-7d — the month a week ago, not the month of the logs — and -mtime +7 has no lower bound, so an archive can legitimately hold logs from any number of months under one month's label. That much is worse than the issue says.

But an exact name was available: memory-YYYY-MM-DD.log carries its own date, so the aged set could be grouped by month and each group named exactly. That does not fix anything. Logs from one June age out on different days, so a rotation in July and another a week later both target logs-2026-06.tar.gz and the second still truncates the first. Month granularity is revisited by construction — exactness is not the axis. The name has to be claimed, not computed.

The fix

Claim, never compute. The first free name of logs-YYYY-MM.tar.gz, logs-YYYY-MM-part2.tar.gz, … reserved atomically with set -C in a subshell, so the redirection fails if the file appeared between the test and the create and two rotations racing cannot select the same name. tar then overwrites the empty placeholder this call made — its own, by construction, which is why the failure paths may remove it without asking what was in it. An archive already on disk is never opened for writing.

The cost is real: a busy month becomes several tarballs. Paid deliberately, because the alternative that keeps one archive per month is extract-merge-recreate, and its worst moment is a crash or a full disk midway through recreating — a truncated archive whose contents were deleted from disk weeks earlier. This design's worst moment is a crash between a verified archive and the deletion of its originals: the next rotation archives them again under a new name. Duplicated, never lost.

Deletion is gated on the archive, not on the exit status. That is the general defect behind this instance — tar returning 0 is evidence that tar had no complaint, not evidence that anything is inside the archive, and the rm was reading the first as the second. The archive is now listed back with tar -tzf and the logs are removed only if it names every one of them. If any is missing, the archive this call created is removed, the originals stay, and the existing third state carries the reason.

The three states are extended, not replaced. Both new failure modes — no free name, and an archive that does not list back — land in the existing 1, logged branch: same .rotate-failed breadcrumb, same escalation after three, same reason in the line, so doctor.sh reports them with no change to it. Nothing in doctor.sh or session-start-hook.sh was touched. And the existing principle survives: accumulation is visible and recoverable; deletion is neither — every new path prefers keeping the logs.

The archive name stays relative (0.12.0 / #252): no directory prefix, so no tar can read it as host:path, and -partN adds no slash and no colon. The existing argv assertion still guards it.

Tests

Three new, each red for its own reason before the fix:

  • test_a_second_rotation_in_the_same_month_keeps_the_first_batch — the reproduction, asserted as a post-condition on the data: after two rotations, every log is either still in the directory or readable out of some archive in it. Deliberately a union over logs-*.tar.gz, so it tests survival rather than a naming scheme, and it asserts the first batch was really deleted-and-archived before going on, or it would pass vacuously.
  • test_rotation_never_truncates_an_existing_archive — bytes of every pre-existing archive, compared across a rotation.
  • test_originals_survive_a_tar_that_exits_zero_without_archiving — a real tar invoked on one of three members: a truthful exit 0 over an incomplete archive. This is the one that fails on any fix that only changes the name.

12 passed in tests/test_log_rotation.py; full suite 1208 passed, 42 skipped locally. bash 3.2 / macOS: no bash-4 syntax, verified with bash -n and against bsdtar.

Outside this PR

while IFS= read -r f over find output means a log filename containing a newline breaks the aged-set handling. Not reachable through anything this plugin writes (memory-YYYY-MM-DD.log), so it is noted rather than fixed here.

…255)

rotate_logs named the archive after a month and tar -czf opens with
O_TRUNC, so the second rotation of a calendar month wrote over the first
one's archive. The logs inside it had been deleted from disk when it was
created, so nothing survived anywhere, and tar returns 0 either way.

Exact naming does not fix this and the reason is worth stating: -mtime +7
selects every log that has aged out since the last successful rotation,
so one archive legitimately spans months — but even grouping by the date
in each filename collides, because logs from one June age out on
different days and two rotations then want the same June archive. Month
granularity is revisited by construction. The name is now claimed, not
computed: the first free logs-YYYY-MM[-partN].tar.gz, reserved atomically
under set -C so a racing rotation cannot pick the same one.

Deletion is no longer gated on tar's exit status either — that was the
general defect behind this instance. The archive is listed back with
tar -tzf and the originals go only if it names every one of them;
otherwise the archive this call created is removed, the logs stay, and
the existing third state reports why. Extract-merge-recreate was rejected
on its worst moment: a crash midway leaves a truncated archive whose
contents were deleted weeks ago. This design's worst moment duplicates,
never loses.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt force-pushed the fix/255-rotate-logs-destroys-archive branch from 8590a2c to 5cc238c Compare July 31, 2026 15:45
@fdaviddpt
fdaviddpt merged commit ab40bbf into main Jul 31, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/255-rotate-logs-destroys-archive branch July 31, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rotate_logs destroys previously archived logs: two rotations in one month write the same tarball, and the originals are already gone

1 participant