Skip to content

Stop snapshots taken in the same millisecond overwriting each other - #2

Merged
abrignoni merged 1 commit into
mainfrom
fix/snapshot-name-collision
Sep 2, 2026
Merged

abrignoni merged 1 commit into
mainfrom
fix/snapshot-name-collision

Conversation

@abrignoni

Copy link
Copy Markdown
Owner

backup.snapshot() named each file from the wall clock to the millisecond with no uniqueness check:

ts = _dt.datetime.now().strftime("%Y%m%d-%H%M%S-%f")[:-3]   # ms precision

Twenty-five of those generated in a tight loop produce one distinct string. Two snapshots landing in the same millisecond resolved to the same filename and the second overwrote the first, so a snapshot someone took could be silently replaced by the next one.

It is reachable rather than theoretical. The auto-snapshot timer and a manual save can land together, and on hardware that finishes a backup in under a millisecond any repeated snapshot hits it. This is why test_backup_prune_keeps_recent fails on a fast machine (16 snapshots where it expects 20) and passes on the CI runner, which is slow enough that names do not collide.

The fix

Stamps now come from a source that steps past the last value it handed out, and the destination is claimed with O_CREAT | O_EXCL so a second GLEAPP process working the same case cannot be handed a name this one just took. A failed backup removes the file it claimed rather than leaving an empty one behind for list_snapshots to report as real.

The stamp is floored to the millisecond before it is compared. Comparing raw readings instead lets a clock that advanced by less than a millisecond pass the check and still format to the string already used. The retry loop survives that, but only by spending attempts, and a clock advancing more slowly than the loop runs would spend the whole budget inside one millisecond and fail the snapshot outright.

The name format is unchanged, so _NAME_RE still recovers labels and the auto flag. Verified by round trip: 6 snapshots, 6 distinct names, labels and auto flags intact.

Tests

Two, because they catch different things. Both are driven by a clock that creeps 100 microseconds per read, which puts ten reads in every millisecond on any machine, so neither passes or fails by hardware.

Run against three versions of the module:

original main comparing raw readings this PR
test_snapshot_stamps_never_repeat_under_a_creeping_clock fail fail pass
test_snapshots_in_one_millisecond_do_not_overwrite fail pass pass

The middle column is the point. An earlier version of this fix passed the integration test and was still wrong, because the retry loop hid the repeating stamp. The unit test on the stamp source is what catches it.

Full suite is 64 passed, up from 62 with one of those failing locally. Lint reports no new warnings.

snapshot() named each file from the wall clock to the millisecond with no
uniqueness check, so two snapshots inside one millisecond resolved to one
name and the second overwrote the first. Twenty-five stamps read straight
from the clock in a tight loop can be a single distinct value.

It is reachable rather than theoretical. The auto-snapshot timer and a
manual save can land together, and on hardware that finishes a backup in
under a millisecond any repeated snapshot hits it. A snapshot someone took
could be silently replaced by the next one.

Names now come from a stamp source that steps past the last value it handed
out, and the destination is claimed with O_CREAT | O_EXCL so a second
process working the same case cannot be given a name this one just took. A
failed backup removes the file it claimed rather than leaving an empty one
for list_snapshots to report.

The stamp is floored to the millisecond before it is compared. Tracking the
raw reading instead would let a clock that advanced by less than a
millisecond pass the check and still format to the string already used,
which the retry loop survives only by spending attempts.

The name format is unchanged, so _NAME_RE still recovers labels and the auto
flag.

Two tests, each failing without the part it covers: one on the stamp source
under a clock that creeps 100 microseconds per read, one taking real
snapshots through the same clock. test_backup_prune_keeps_recent was failing
on faster machines for this reason and now passes.

Co-Authored-By: Claude Fable 5 <[email protected]>
@abrignoni
abrignoni merged commit b2d722e into main Sep 2, 2026
8 checks passed
@abrignoni
abrignoni deleted the fix/snapshot-name-collision branch September 2, 2026 02:32
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.

1 participant