Repository navigation
fix(log): claim the archive name and verify it before deleting logs (#255) - #274
Merged
Merged
Conversation
…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
force-pushed
the
fix/255-rotate-logs-destroys-archive
branch
from
July 31, 2026 15:45
8590a2c to
5cc238c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #255.
The bug, confirmed
rotate_logsnamed the archive after a month,tar -czfopens withO_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: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_monthreally isdate -v-7d— the month a week ago, not the month of the logs — and-mtime +7has 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.logcarries 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 targetlogs-2026-06.tar.gzand 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 withset -Cin 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
rmwas reading the first as the second. The archive is now listed back withtar -tzfand 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, loggedbranch: same.rotate-failedbreadcrumb, same escalation after three, same reason in the line, sodoctor.shreports them with no change to it. Nothing indoctor.shorsession-start-hook.shwas 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-partNadds 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 overlogs-*.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 passedintests/test_log_rotation.py; full suite1208 passed, 42 skippedlocally. bash 3.2 / macOS: no bash-4 syntax, verified withbash -nand against bsdtar.Outside this PR
while IFS= read -r foverfindoutput 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.