mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
Adds focused tests for the #674/#682 rollback refusals, uncertain-publish identity checks, held-staging cleanup races and digest manifest validation: persistence_contracts.py coverage goes from 97.14/96.07 to 99.80/99.44 (line/branch), above the 97.80/96.88 base. The release comparison base for a synced branch head (a merge commit that is neither main nor a tag) is now its merge base with main instead of HEAD^, so changes brought in from main are not counted as the branch's regressions; main and tag pushes still use the first parent. No production code changes. Refs #679
3017 lines
121 KiB
Python
3017 lines
121 KiB
Python
"""Boundary contracts for deterministic and transactional checkpoint state."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
from threading import Barrier
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
import obliteratus.persistence_contracts as persistence
|
|
|
|
|
|
def emulated_rename_no_replace(source, destination):
|
|
"""Deterministic stand-in for the atomic no-replace rename on hosts without it.
|
|
|
|
The existence check and rename are two steps, so this is not atomic. That is
|
|
fine for these tests, which drive every interleaving deterministically.
|
|
"""
|
|
import errno
|
|
|
|
if os.path.lexists(destination):
|
|
raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), str(destination))
|
|
os.rename(source, destination)
|
|
|
|
|
|
def host_supports_no_replace_rename(directory: Path) -> bool:
|
|
"""Probe whether ``directory``'s filesystem supports the real primitive."""
|
|
source = directory / ".no-replace-probe-source"
|
|
target = directory / ".no-replace-probe-target"
|
|
source.write_bytes(b"")
|
|
try:
|
|
persistence._rename_no_replace(source, target)
|
|
except persistence.NoReplaceRenameUnsupported:
|
|
return False
|
|
finally:
|
|
source.unlink(missing_ok=True)
|
|
target.unlink(missing_ok=True)
|
|
return True
|
|
|
|
|
|
@pytest.fixture
|
|
def portable_no_replace_rename(tmp_path_factory, monkeypatch):
|
|
"""Use the real primitive where the test filesystem supports it, else emulate it."""
|
|
if not host_supports_no_replace_rename(tmp_path_factory.mktemp("no-replace-probe")):
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", emulated_rename_no_replace)
|
|
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def _write_valid_local_checkpoint(
|
|
checkpoint_dir: Path,
|
|
metadata_json: str = '{"schema": 1}',
|
|
) -> None:
|
|
(checkpoint_dir / "abliteration_metadata.json").write_text(
|
|
metadata_json,
|
|
encoding="utf-8",
|
|
)
|
|
(checkpoint_dir / "config.json").write_text("{}", encoding="utf-8")
|
|
(checkpoint_dir / "tokenizer_config.json").write_text("{}", encoding="utf-8")
|
|
(checkpoint_dir / "model.safetensors").write_bytes(b"weights")
|
|
|
|
|
|
def test_reloadable_checkpoint_resolves_absolute_and_relative_paths(tmp_path, monkeypatch):
|
|
checkpoint = tmp_path / "completed"
|
|
checkpoint.mkdir()
|
|
_write_valid_local_checkpoint(checkpoint)
|
|
|
|
assert persistence.validate_reloadable_checkpoint(checkpoint) == checkpoint.resolve()
|
|
monkeypatch.chdir(tmp_path)
|
|
assert persistence.validate_reloadable_checkpoint("completed") == checkpoint.resolve()
|
|
|
|
|
|
def test_reloadable_checkpoint_rejects_absent_incomplete_and_corrupt_paths(tmp_path):
|
|
with pytest.raises(FileNotFoundError, match="not a directory"):
|
|
persistence.validate_reloadable_checkpoint(tmp_path / "absent")
|
|
|
|
incomplete = tmp_path / "incomplete"
|
|
incomplete.mkdir()
|
|
(incomplete / "abliteration_metadata.json").write_text("{}", encoding="utf-8")
|
|
with pytest.raises(ValueError, match="model config is missing"):
|
|
persistence.validate_reloadable_checkpoint(incomplete)
|
|
|
|
corrupt = tmp_path / "corrupt"
|
|
corrupt.mkdir()
|
|
(corrupt / "abliteration_metadata.json").write_text("not-json", encoding="utf-8")
|
|
with pytest.raises(ValueError, match="metadata is corrupt"):
|
|
persistence.validate_reloadable_checkpoint(corrupt)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("state_dict", "expected"),
|
|
[
|
|
({}, 0),
|
|
({"float": torch.ones(3, dtype=torch.float32)}, 12),
|
|
(
|
|
{
|
|
"half": torch.ones(5, dtype=torch.float16),
|
|
"index": torch.ones(2, dtype=torch.int64),
|
|
},
|
|
26,
|
|
),
|
|
],
|
|
)
|
|
def test_state_dict_size_bytes_is_exact(state_dict, expected):
|
|
assert persistence.state_dict_size_bytes(state_dict) == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("payload", "required"),
|
|
[(0, 0), (1, 2), (9, 10), (10, 11), (101, 112)],
|
|
)
|
|
def test_required_checkpoint_bytes_rounds_headroom_up(payload, required):
|
|
assert persistence.required_checkpoint_bytes(payload) == required
|
|
|
|
|
|
def test_required_checkpoint_bytes_rejects_negative_payload():
|
|
with pytest.raises(ValueError) as exc_info:
|
|
persistence.required_checkpoint_bytes(-1)
|
|
|
|
assert str(exc_info.value) == "checkpoint payload size cannot be negative"
|
|
|
|
|
|
def test_checkpoint_capacity_accepts_exact_boundary_and_rejects_one_byte_less():
|
|
persistence.ensure_checkpoint_capacity(free_bytes=11, payload_bytes=10)
|
|
|
|
with pytest.raises(
|
|
OSError,
|
|
match=r"Insufficient disk space: 0\.0 GB free, need ~0\.0 GB",
|
|
):
|
|
persistence.ensure_checkpoint_capacity(free_bytes=10, payload_bytes=10)
|
|
|
|
|
|
def test_checkpoint_capacity_error_reports_decimal_gigabytes_exactly():
|
|
payload_bytes = 1_000_000_000_000_000_000
|
|
|
|
with pytest.raises(OSError) as exc_info:
|
|
persistence.ensure_checkpoint_capacity(
|
|
free_bytes=payload_bytes,
|
|
payload_bytes=payload_bytes,
|
|
)
|
|
|
|
assert str(exc_info.value) == (
|
|
"Insufficient disk space: 1000000000.0 GB free, "
|
|
"need ~1000000000.0 GB. "
|
|
"Try a different --output-dir on a larger filesystem."
|
|
)
|
|
|
|
|
|
def test_metadata_serialization_is_stable_strict_json():
|
|
first = persistence.serialize_checkpoint_metadata({"z": 1, "a": [True, None]})
|
|
second = persistence.serialize_checkpoint_metadata({"a": [True, None], "z": 1})
|
|
|
|
assert first == second
|
|
assert json.loads(first) == {"a": [True, None], "z": 1}
|
|
assert first == (
|
|
'{\n "a": [\n true,\n null\n ],\n "z": 1\n}'
|
|
)
|
|
|
|
|
|
def test_metadata_serializer_passes_strict_format_options(monkeypatch):
|
|
real_dumps = json.dumps
|
|
observed = {}
|
|
|
|
def record_dumps(metadata, **kwargs):
|
|
observed.update(kwargs)
|
|
return real_dumps(metadata, **kwargs)
|
|
|
|
monkeypatch.setattr(persistence.json, "dumps", record_dumps)
|
|
|
|
assert json.loads(persistence.serialize_checkpoint_metadata({"schema": 1})) == {
|
|
"schema": 1,
|
|
}
|
|
assert observed == {"indent": 2, "sort_keys": True, "allow_nan": False}
|
|
|
|
|
|
@pytest.mark.parametrize("value", [object(), float("nan"), float("inf")])
|
|
def test_metadata_serialization_rejects_nonportable_values(value):
|
|
with pytest.raises((TypeError, ValueError)):
|
|
persistence.serialize_checkpoint_metadata({"invalid": value})
|
|
|
|
|
|
@pytest.mark.parametrize("weights_name", ["model.safetensors", "pytorch_model.bin"])
|
|
def test_validate_local_checkpoint_accepts_complete_direct_weights(tmp_path, weights_name):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
if weights_name != "model.safetensors":
|
|
(tmp_path / "model.safetensors").rename(tmp_path / weights_name)
|
|
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
|
|
def test_validate_local_checkpoint_accepts_complete_sharded_weights(tmp_path):
|
|
metadata_json = '{"schema": 1}'
|
|
_write_valid_local_checkpoint(tmp_path, metadata_json)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
(tmp_path / "model.safetensors.index.json").write_text(
|
|
json.dumps(
|
|
{
|
|
"metadata": {"total_size": 2},
|
|
"weight_map": {
|
|
"layer.0": "model-00001-of-00002.safetensors",
|
|
"layer.1": "model-00002-of-00002.safetensors",
|
|
},
|
|
},
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
(tmp_path / "model-00001-of-00002.safetensors").write_bytes(b"a")
|
|
(tmp_path / "model-00002-of-00002.safetensors").write_bytes(b"b")
|
|
|
|
persistence.validate_local_checkpoint(tmp_path, metadata_json)
|
|
|
|
|
|
def test_validate_local_checkpoint_accepts_pytorch_sharded_weights(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
(tmp_path / "pytorch_model.bin.index.json").write_text(
|
|
json.dumps({"weight_map": {"layer": "pytorch_model-00001-of-00001.bin"}}),
|
|
encoding="utf-8",
|
|
)
|
|
(tmp_path / "pytorch_model-00001-of-00001.bin").write_bytes(b"weights")
|
|
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["file", "symlink"])
|
|
def test_validate_local_checkpoint_rejects_non_directory_staging(tmp_path, kind):
|
|
staging = tmp_path / "staging"
|
|
if kind == "file":
|
|
staging.write_bytes(b"not a directory")
|
|
else:
|
|
target = tmp_path / "target"
|
|
target.mkdir()
|
|
staging.symlink_to(target, target_is_directory=True)
|
|
|
|
with pytest.raises(ValueError, match="staging path is not a directory"):
|
|
persistence.validate_local_checkpoint(staging, '{"schema": 1}')
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("mutate", "message"),
|
|
[
|
|
(
|
|
lambda path: (path / "abliteration_metadata.json").write_text(
|
|
"{",
|
|
encoding="utf-8",
|
|
),
|
|
"metadata is corrupt",
|
|
),
|
|
(
|
|
lambda path: (path / "abliteration_metadata.json").write_text(
|
|
'{"schema": 2}',
|
|
encoding="utf-8",
|
|
),
|
|
"metadata does not match",
|
|
),
|
|
(
|
|
lambda path: (path / "config.json").write_text("[]", encoding="utf-8"),
|
|
"model config must contain a JSON object",
|
|
),
|
|
(
|
|
lambda path: (path / "tokenizer_config.json").write_bytes(b""),
|
|
"tokenizer config is missing or empty",
|
|
),
|
|
(
|
|
lambda path: (path / "model.safetensors").write_bytes(b""),
|
|
"weights is missing or empty",
|
|
),
|
|
],
|
|
)
|
|
def test_validate_local_checkpoint_rejects_corrupt_or_truncated_artifacts(
|
|
tmp_path,
|
|
mutate,
|
|
message,
|
|
):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
mutate(tmp_path)
|
|
|
|
with pytest.raises(ValueError, match=message):
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
|
|
def test_validate_local_checkpoint_reports_exact_metadata_mismatch(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path, '{"schema": 2}')
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
assert str(exc_info.value) == (
|
|
"Checkpoint metadata does not match the prepared transaction"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"weight_map",
|
|
[
|
|
{},
|
|
{"layer": "missing.safetensors"},
|
|
{"layer": "../outside.safetensors"},
|
|
{"layer": ["not", "a", "path"]},
|
|
],
|
|
)
|
|
def test_validate_local_checkpoint_rejects_invalid_weight_indexes(tmp_path, weight_map):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
(tmp_path / "model.safetensors.index.json").write_text(
|
|
json.dumps({"weight_map": weight_map}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="weight index|weight shard"):
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
|
|
def test_validate_local_checkpoint_reports_corrupt_weight_index_exactly(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
index_path = tmp_path / "model.safetensors.index.json"
|
|
index_path.write_text("{", encoding="utf-8")
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
assert str(exc_info.value) == f"Checkpoint weight index is corrupt: {index_path}"
|
|
|
|
|
|
def test_validate_local_checkpoint_reports_missing_weight_shard_exactly(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
index_path = tmp_path / "model.safetensors.index.json"
|
|
index_path.write_text(
|
|
json.dumps({"weight_map": {"layer": "missing.safetensors"}}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
missing = tmp_path / "missing.safetensors"
|
|
assert str(exc_info.value) == f"Checkpoint weight shard is missing or empty: {missing}"
|
|
|
|
|
|
def test_validate_local_checkpoint_rejects_existing_unsafe_weight_shard(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
outside = tmp_path.parent / "outside.safetensors"
|
|
outside.write_bytes(b"outside")
|
|
index_path = tmp_path / "model.safetensors.index.json"
|
|
index_path.write_text(
|
|
json.dumps({"weight_map": {"layer": "../outside.safetensors"}}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
assert str(exc_info.value) == (
|
|
f"Checkpoint weight index contains an unsafe shard path: {index_path}"
|
|
)
|
|
|
|
|
|
def test_validate_local_checkpoint_rejects_checkpoint_without_weights(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
|
|
with pytest.raises(ValueError, match="has no model weights"):
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
|
|
def test_validate_local_checkpoint_rejects_link_backed_weights(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "model.safetensors").unlink()
|
|
outside = tmp_path / "outside.safetensors"
|
|
outside.write_bytes(b"outside")
|
|
(tmp_path / "model.safetensors").symlink_to(outside)
|
|
|
|
with pytest.raises(ValueError, match="weights is missing or empty"):
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
|
|
def test_validate_local_checkpoint_rejects_link_backed_json(tmp_path):
|
|
_write_valid_local_checkpoint(tmp_path)
|
|
(tmp_path / "config.json").unlink()
|
|
outside = tmp_path / "outside.json"
|
|
outside.write_text("{}", encoding="utf-8")
|
|
(tmp_path / "config.json").symlink_to(outside)
|
|
|
|
with pytest.raises(ValueError, match="model config is missing or empty"):
|
|
persistence.validate_local_checkpoint(tmp_path, '{"schema": 1}')
|
|
|
|
|
|
def test_remove_checkpoint_path_handles_file_directory_symlink_and_missing(tmp_path):
|
|
file_path = tmp_path / "file"
|
|
file_path.write_text("data", encoding="utf-8")
|
|
directory = tmp_path / "directory"
|
|
directory.mkdir()
|
|
(directory / "nested").write_text("data", encoding="utf-8")
|
|
target = tmp_path / "target"
|
|
target.mkdir()
|
|
(target / "sentinel").write_text("preserve", encoding="utf-8")
|
|
symlink = tmp_path / "link"
|
|
symlink.symlink_to(target, target_is_directory=True)
|
|
|
|
for path in (file_path, directory, symlink, tmp_path / "missing"):
|
|
persistence._remove_checkpoint_path(path)
|
|
|
|
assert not file_path.exists()
|
|
assert not directory.exists()
|
|
assert not symlink.exists()
|
|
assert (target / "sentinel").read_text(encoding="utf-8") == "preserve"
|
|
|
|
|
|
def test_remove_checkpoint_path_requests_race_safe_file_unlink():
|
|
path = MagicMock()
|
|
path.is_symlink.return_value = True
|
|
|
|
persistence._remove_checkpoint_path(path)
|
|
|
|
path.unlink.assert_called_once_with(missing_ok=True)
|
|
path.is_file.assert_not_called()
|
|
|
|
|
|
def test_remove_checkpoint_path_requests_observable_directory_cleanup(monkeypatch):
|
|
path = MagicMock()
|
|
path.is_symlink.return_value = False
|
|
path.is_file.return_value = False
|
|
path.exists.return_value = True
|
|
remove_tree = MagicMock()
|
|
monkeypatch.setattr(persistence.shutil, "rmtree", remove_tree)
|
|
|
|
persistence._remove_checkpoint_path(path)
|
|
|
|
remove_tree.assert_called_once_with(path)
|
|
|
|
|
|
def test_remove_checkpoint_path_tolerates_directory_disappearing_during_cleanup(
|
|
monkeypatch,
|
|
):
|
|
path = MagicMock()
|
|
path.is_symlink.return_value = False
|
|
path.is_file.return_value = False
|
|
path.exists.return_value = True
|
|
remove_tree = MagicMock(side_effect=FileNotFoundError)
|
|
monkeypatch.setattr(persistence.shutil, "rmtree", remove_tree)
|
|
|
|
persistence._remove_checkpoint_path(path)
|
|
|
|
remove_tree.assert_called_once_with(path)
|
|
|
|
|
|
def test_atomic_checkpoint_creates_parent_and_promotes_new_destination(tmp_path):
|
|
destination = tmp_path / "nested" / "checkpoint"
|
|
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
assert staging.parent == destination.parent
|
|
assert staging.name.startswith(".checkpoint.staging-")
|
|
(staging / "model.bin").write_bytes(b"complete")
|
|
|
|
assert (destination / "model.bin").read_bytes() == b"complete"
|
|
assert list(destination.parent.glob(".checkpoint.*-*")) == []
|
|
|
|
|
|
@pytest.mark.parametrize("partial_cleanup", [False, True])
|
|
def test_atomic_checkpoint_strict_backup_cleanup_preserves_replacement_on_uncertainty(
|
|
tmp_path,
|
|
monkeypatch,
|
|
partial_cleanup,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
original_remove = persistence._remove_checkpoint_path
|
|
|
|
def fail_backup_cleanup(path):
|
|
if ".backup-" in Path(path).name:
|
|
if partial_cleanup:
|
|
(Path(path) / "model.bin").unlink()
|
|
raise OSError("backup cleanup failed")
|
|
original_remove(path)
|
|
|
|
monkeypatch.setattr(persistence, "_remove_checkpoint_path", fail_backup_cleanup)
|
|
with pytest.raises(RuntimeError, match="backup cleanup is uncertain"):
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination,
|
|
strict_backup_cleanup=True,
|
|
) as staging:
|
|
(staging / "model.bin").write_bytes(b"replacement")
|
|
|
|
assert (destination / "model.bin").read_bytes() == b"replacement"
|
|
assert list(tmp_path.glob(".checkpoint.staging-*")) == []
|
|
backups = list(tmp_path.glob(".checkpoint.backup-*"))
|
|
assert len(backups) == 1
|
|
if partial_cleanup:
|
|
assert not (backups[0] / "model.bin").exists()
|
|
else:
|
|
assert (backups[0] / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_atomic_checkpoint_strict_cleanup_sync_failure_preserves_replacement(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
original_remove = persistence._remove_checkpoint_path
|
|
cleanup_finished = False
|
|
|
|
def remove_backup(path):
|
|
nonlocal cleanup_finished
|
|
original_remove(path)
|
|
if ".backup-" in Path(path).name:
|
|
cleanup_finished = True
|
|
|
|
def fail_cleanup_sync(path):
|
|
if cleanup_finished:
|
|
raise OSError("backup cleanup sync failed")
|
|
|
|
monkeypatch.setattr(persistence, "_remove_checkpoint_path", remove_backup)
|
|
monkeypatch.setattr(persistence, "_sync_directory", fail_cleanup_sync)
|
|
with pytest.raises(RuntimeError, match="backup cleanup is uncertain"):
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination, strict_backup_cleanup=True,
|
|
) as staging:
|
|
(staging / "model.bin").write_bytes(b"replacement")
|
|
assert (destination / "model.bin").read_bytes() == b"replacement"
|
|
assert list(tmp_path.glob(".checkpoint.backup-*")) == []
|
|
|
|
|
|
def test_safetensors_index_payload_preserves_payload_and_mapping_order():
|
|
weight_map = {"z": "second.safetensors", "a": "first.safetensors"}
|
|
payload = persistence.safetensors_index_payload(weight_map, 42)
|
|
assert payload == {"metadata": {"total_size": 42}, "weight_map": weight_map}
|
|
assert list(payload["weight_map"]) == ["z", "a"]
|
|
weight_map.clear()
|
|
assert len(payload["weight_map"]) == 2
|
|
|
|
|
|
def test_atomic_checkpoint_cleans_staging_when_new_destination_promotion_fails(
|
|
tmp_path,
|
|
monkeypatch,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
|
|
def fail_promotion(source, target):
|
|
raise OSError(f"cannot promote {Path(source).name} to {Path(target).name}")
|
|
|
|
monkeypatch.setattr(persistence.os, "replace", fail_promotion)
|
|
with pytest.raises(OSError, match="cannot promote"):
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"partial")
|
|
|
|
assert not destination.exists()
|
|
assert list(tmp_path.glob(".checkpoint.staging-*")) == []
|
|
|
|
|
|
def test_atomic_checkpoint_replaces_symlink_without_touching_target(tmp_path):
|
|
target = tmp_path / "target"
|
|
target.mkdir()
|
|
sentinel = target / "sentinel"
|
|
sentinel.write_text("preserve", encoding="utf-8")
|
|
destination = tmp_path / "checkpoint"
|
|
destination.symlink_to(target, target_is_directory=True)
|
|
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"replacement")
|
|
|
|
assert not destination.is_symlink()
|
|
assert (destination / "model.bin").read_bytes() == b"replacement"
|
|
assert sentinel.read_text(encoding="utf-8") == "preserve"
|
|
|
|
|
|
def test_atomic_checkpoint_uses_os_replace_not_copy(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
calls = []
|
|
real_replace = os.replace
|
|
|
|
def record_replace(source, target):
|
|
calls.append((Path(source), Path(target)))
|
|
return real_replace(source, target)
|
|
|
|
monkeypatch.setattr(persistence.os, "replace", record_replace)
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"saved")
|
|
|
|
assert len(calls) == 1
|
|
assert calls[0][1] == destination
|
|
assert ".staging-" in calls[0][0].name
|
|
|
|
|
|
def test_atomic_checkpoint_flushes_files_before_promotion(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
events = []
|
|
real_sync_file = persistence._sync_file
|
|
real_replace = os.replace
|
|
|
|
def record_sync(path):
|
|
events.append(("sync", Path(path).name))
|
|
return real_sync_file(path)
|
|
|
|
def record_replace(source, target):
|
|
events.append(("replace", Path(source).name))
|
|
return real_replace(source, target)
|
|
|
|
monkeypatch.setattr(persistence, "_sync_file", record_sync)
|
|
monkeypatch.setattr(persistence.os, "replace", record_replace)
|
|
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"saved")
|
|
|
|
assert events[0] == ("sync", "model.bin")
|
|
assert events[1][0] == "replace"
|
|
|
|
|
|
def test_atomic_checkpoint_flushes_nested_directories(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
synced_directories = []
|
|
real_sync_directory = persistence._sync_directory
|
|
|
|
def record_directory_sync(path):
|
|
synced_directories.append(Path(path).name)
|
|
return real_sync_directory(path)
|
|
|
|
monkeypatch.setattr(persistence, "_sync_directory", record_directory_sync)
|
|
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
nested = staging / "nested"
|
|
nested.mkdir()
|
|
(nested / "payload").write_bytes(b"saved")
|
|
|
|
assert "nested" in synced_directories
|
|
|
|
|
|
def test_file_sync_flags_use_writable_binary_handle_on_windows():
|
|
binary_flag = 0x8000
|
|
expected = os.O_RDWR | binary_flag
|
|
|
|
assert persistence._file_sync_flags("nt", binary_flag) == expected
|
|
assert expected != os.O_RDONLY
|
|
|
|
|
|
def test_file_sync_flags_use_read_handle_on_posix():
|
|
assert persistence._file_sync_flags("posix", 0x8000) == os.O_RDONLY
|
|
|
|
|
|
def test_sync_file_uses_platform_flags_and_closes_descriptor(tmp_path, monkeypatch):
|
|
artifact = tmp_path / "artifact.bin"
|
|
artifact.write_bytes(b"checkpoint")
|
|
open_file = MagicMock(return_value=17)
|
|
sync_file = MagicMock()
|
|
close_file = MagicMock()
|
|
monkeypatch.setattr(persistence, "_FILE_SYNC_FLAGS", 123)
|
|
monkeypatch.setattr(persistence.os, "open", open_file)
|
|
monkeypatch.setattr(persistence.os, "fsync", sync_file)
|
|
monkeypatch.setattr(persistence.os, "close", close_file)
|
|
|
|
persistence._sync_file(artifact)
|
|
|
|
open_file.assert_called_once_with(artifact, 123)
|
|
sync_file.assert_called_once_with(17)
|
|
close_file.assert_called_once_with(17)
|
|
|
|
|
|
def test_sync_file_closes_descriptor_after_fsync_failure(tmp_path, monkeypatch):
|
|
artifact = tmp_path / "artifact.bin"
|
|
artifact.write_bytes(b"checkpoint")
|
|
close_file = MagicMock()
|
|
monkeypatch.setattr(persistence.os, "open", MagicMock(return_value=17))
|
|
monkeypatch.setattr(
|
|
persistence.os,
|
|
"fsync",
|
|
MagicMock(side_effect=OSError("simulated fsync failure")),
|
|
)
|
|
monkeypatch.setattr(persistence.os, "close", close_file)
|
|
|
|
with pytest.raises(OSError, match="simulated fsync failure"):
|
|
persistence._sync_file(artifact)
|
|
|
|
close_file.assert_called_once_with(17)
|
|
|
|
|
|
def test_sync_directory_uses_platform_flags_and_closes_descriptor(
|
|
tmp_path,
|
|
monkeypatch,
|
|
):
|
|
open_file = MagicMock(return_value=17)
|
|
sync_file = MagicMock()
|
|
close_file = MagicMock()
|
|
monkeypatch.setattr(persistence, "_DIRECTORY_SYNC_FLAGS", 123)
|
|
monkeypatch.setattr(persistence.os, "open", open_file)
|
|
monkeypatch.setattr(persistence.os, "fsync", sync_file)
|
|
monkeypatch.setattr(persistence.os, "close", close_file)
|
|
|
|
persistence._sync_directory(tmp_path)
|
|
|
|
open_file.assert_called_once_with(tmp_path, 123)
|
|
sync_file.assert_called_once_with(17)
|
|
close_file.assert_called_once_with(17)
|
|
|
|
|
|
def test_sync_directory_skips_unsupported_platform(tmp_path, monkeypatch):
|
|
open_file = MagicMock()
|
|
monkeypatch.setattr(persistence, "_DIRECTORY_SYNC_FLAGS", None)
|
|
monkeypatch.setattr(persistence.os, "open", open_file)
|
|
|
|
persistence._sync_directory(tmp_path)
|
|
|
|
open_file.assert_not_called()
|
|
|
|
|
|
def test_sync_directory_closes_descriptor_after_fsync_failure(tmp_path, monkeypatch):
|
|
close_file = MagicMock()
|
|
monkeypatch.setattr(persistence, "_DIRECTORY_SYNC_FLAGS", 123)
|
|
monkeypatch.setattr(persistence.os, "open", MagicMock(return_value=17))
|
|
monkeypatch.setattr(
|
|
persistence.os,
|
|
"fsync",
|
|
MagicMock(side_effect=OSError("simulated fsync failure")),
|
|
)
|
|
monkeypatch.setattr(persistence.os, "close", close_file)
|
|
|
|
with pytest.raises(OSError, match="simulated fsync failure"):
|
|
persistence._sync_directory(tmp_path)
|
|
|
|
close_file.assert_called_once_with(17)
|
|
|
|
|
|
def test_sync_checkpoint_tree_requests_bottom_up_nonfollowing_walk(
|
|
tmp_path,
|
|
monkeypatch,
|
|
):
|
|
real_walk = os.walk
|
|
observed = {}
|
|
|
|
def record_walk(path, **kwargs):
|
|
observed.update(kwargs)
|
|
return real_walk(path, **kwargs)
|
|
|
|
monkeypatch.setattr(persistence.os, "walk", record_walk)
|
|
|
|
persistence._sync_checkpoint_tree(tmp_path)
|
|
|
|
assert observed == {"topdown": False, "followlinks": False}
|
|
|
|
|
|
@pytest.mark.parametrize("link_kind", ["file", "directory"])
|
|
def test_atomic_checkpoint_rejects_symlinks_inside_staging(tmp_path, link_kind):
|
|
destination = tmp_path / "checkpoint"
|
|
outside = tmp_path / "outside"
|
|
if link_kind == "file":
|
|
outside.write_bytes(b"outside")
|
|
else:
|
|
outside.mkdir()
|
|
|
|
with pytest.raises(OSError, match="not a regular file|contains a symlink"):
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
link = staging / "link"
|
|
link.symlink_to(outside, target_is_directory=link_kind == "directory")
|
|
|
|
assert not destination.exists()
|
|
assert outside.exists()
|
|
|
|
|
|
def test_atomic_checkpoint_serializes_concurrent_complete_writers(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "generation").write_text("old", encoding="utf-8")
|
|
ready = Barrier(2)
|
|
|
|
def write_generation(generation):
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "generation").write_text(generation, encoding="utf-8")
|
|
(staging / "payload-a").write_text(generation, encoding="utf-8")
|
|
(staging / "payload-b").write_text(generation, encoding="utf-8")
|
|
ready.wait(timeout=5)
|
|
|
|
with ThreadPoolExecutor(max_workers=2) as pool:
|
|
futures = [pool.submit(write_generation, generation) for generation in ("a", "b")]
|
|
for future in futures:
|
|
future.result(timeout=10)
|
|
|
|
generation = (destination / "generation").read_text(encoding="utf-8")
|
|
assert generation in {"a", "b"}
|
|
assert (destination / "payload-a").read_text(encoding="utf-8") == generation
|
|
assert (destination / "payload-b").read_text(encoding="utf-8") == generation
|
|
assert list(tmp_path.glob(".checkpoint.staging-*")) == []
|
|
assert list(tmp_path.glob(".checkpoint.backup-*")) == []
|
|
|
|
|
|
def test_atomic_checkpoint_preserves_artifacts_owned_by_other_transactions(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
unrelated_staging = tmp_path / ".checkpoint.staging-operator-preserve"
|
|
unrelated_backup = tmp_path / ".checkpoint.backup-operator-preserve"
|
|
unrelated_staging.mkdir()
|
|
unrelated_backup.mkdir()
|
|
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"saved")
|
|
|
|
assert unrelated_staging.is_dir()
|
|
assert unrelated_backup.is_dir()
|
|
|
|
|
|
def test_rollback_rejects_ambiguous_destination_and_staging(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
backup = tmp_path / ".checkpoint.backup-owned"
|
|
destination.mkdir()
|
|
staging.mkdir()
|
|
backup.mkdir()
|
|
|
|
# Now a CheckpointRollbackRefused (still a RuntimeError) naming every path (#645).
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as exc_info:
|
|
persistence._rollback_checkpoint_commit(
|
|
destination,
|
|
staging,
|
|
backup,
|
|
had_destination=True,
|
|
)
|
|
|
|
assert isinstance(exc_info.value, RuntimeError)
|
|
assert str(exc_info.value) == (
|
|
f"Checkpoint rollback left {destination} in place: {staging} also exists, so the "
|
|
"promoted checkpoint could not be moved aside without risking another writer's "
|
|
f"entry. Nothing was moved or removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
assert destination.is_dir() and staging.is_dir() and backup.is_dir()
|
|
|
|
|
|
def test_rollback_with_no_backup_preserves_preexisting_destination(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
sentinel = destination / "sentinel"
|
|
sentinel.write_text("preserve", encoding="utf-8")
|
|
staging = tmp_path / ".checkpoint.staging-missing"
|
|
|
|
# The commit failed before the destination was moved to a backup or promotion
|
|
# was attempted, so the destination is still the previous checkpoint.
|
|
persistence._rollback_checkpoint_commit(
|
|
destination,
|
|
staging,
|
|
None,
|
|
had_destination=True,
|
|
promotion_attempted=False,
|
|
)
|
|
|
|
assert sentinel.read_text(encoding="utf-8") == "preserve"
|
|
assert not staging.exists()
|
|
|
|
|
|
def test_atomic_checkpoint_without_replacement_keeps_the_concurrent_winner(
|
|
tmp_path, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
|
|
with pytest.raises(FileExistsError, match="already exists"):
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination, replace_existing=False,
|
|
) as first:
|
|
(first / "model.bin").write_bytes(b"first")
|
|
# Deterministic interleaving: a second writer starts after the
|
|
# first and reaches the commit lock before it.
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination, replace_existing=False,
|
|
) as second:
|
|
(second / "model.bin").write_bytes(b"second")
|
|
assert (destination / "model.bin").read_bytes() == b"second"
|
|
|
|
assert (destination / "model.bin").read_bytes() == b"second"
|
|
assert not first.exists()
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == [".checkpoint.lock", "checkpoint"]
|
|
|
|
|
|
def test_atomic_checkpoint_without_replacement_refuses_existing_destination(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
|
|
with pytest.raises(FileExistsError, match="already exists"):
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination, replace_existing=False,
|
|
) as staging:
|
|
(staging / "model.bin").write_bytes(b"replacement")
|
|
|
|
assert (destination / "model.bin").read_bytes() == b"prior"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == [".checkpoint.lock", "checkpoint"]
|
|
|
|
|
|
def test_atomic_checkpoint_default_still_replaces_existing_destination(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"replacement")
|
|
|
|
assert (destination / "model.bin").read_bytes() == b"replacement"
|
|
|
|
|
|
def test_exclusive_text_files_publish_complete_files_or_nothing(tmp_path):
|
|
first = tmp_path / "out" / "config.json"
|
|
second = tmp_path / "out" / "report.json"
|
|
first.parent.mkdir()
|
|
first.write_text("competitor\n", encoding="utf-8")
|
|
|
|
# The first file conflicts, so nothing is published.
|
|
with pytest.raises(FileExistsError) as raised:
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
assert str(raised.value) == f"Output already exists: {first}"
|
|
assert not second.exists()
|
|
assert first.read_text(encoding="utf-8") == "competitor\n"
|
|
assert sorted(path.name for path in first.parent.iterdir()) == ["config.json"]
|
|
|
|
first.unlink()
|
|
persistence.write_new_text_files({first: "report\n", second: "config\n"})
|
|
assert first.read_text(encoding="utf-8") == "report\n"
|
|
assert second.read_text(encoding="utf-8") == "config\n"
|
|
with pytest.raises(FileExistsError, match="already exists"):
|
|
persistence.write_new_text_files({first: "again\n"})
|
|
assert first.read_text(encoding="utf-8") == "report\n"
|
|
|
|
|
|
def test_atomic_checkpoint_without_replacement_never_replaces_an_injected_empty_directory(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
real_path_exists = persistence._path_exists
|
|
|
|
def writer_bypassing_the_lock_creates_it_after_the_check(path):
|
|
exists = real_path_exists(path)
|
|
if path == destination and not exists:
|
|
destination.mkdir() # an empty directory that os.replace would overwrite
|
|
return exists
|
|
|
|
monkeypatch.setattr(
|
|
persistence, "_path_exists", writer_bypassing_the_lock_creates_it_after_the_check,
|
|
)
|
|
with pytest.raises(FileExistsError, match="already exists"):
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination, replace_existing=False,
|
|
) as staging:
|
|
(staging / "model.bin").write_bytes(b"late")
|
|
|
|
assert destination.is_dir() and list(destination.iterdir()) == []
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == [".checkpoint.lock", "checkpoint"]
|
|
|
|
|
|
def test_atomic_checkpoint_without_replacement_fails_closed_without_no_replace_rename(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
|
|
def unsupported(_source, _destination):
|
|
raise persistence.NoReplaceRenameUnsupported("no atomic no-replace rename here")
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", unsupported)
|
|
with pytest.raises(persistence.NoReplaceRenameUnsupported):
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination, replace_existing=False,
|
|
) as staging:
|
|
(staging / "model.bin").write_bytes(b"data")
|
|
assert not destination.exists()
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == [".checkpoint.lock"]
|
|
|
|
|
|
def test_exclusive_text_files_never_remove_a_published_file(tmp_path):
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
second.write_text("competitor report\n", encoding="utf-8")
|
|
|
|
with pytest.raises(FileExistsError) as raised:
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
# Ownership of a published path cannot be proven without a race, so the
|
|
# published file stays and the error says the set may be incomplete.
|
|
assert str(raised.value) == (
|
|
f"Output already exists: {second}; already published and left in place "
|
|
f"(the set may be incomplete): {first}"
|
|
)
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
assert second.read_text(encoding="utf-8") == "competitor report\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json", "report.json"]
|
|
|
|
|
|
def test_exclusive_text_files_report_published_files_when_a_later_step_fails(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
real_publish = persistence._publish_file_no_replace
|
|
|
|
def fail_on_the_second(temporary, destination):
|
|
if destination == second:
|
|
raise OSError("injected publish failure")
|
|
return real_publish(temporary, destination)
|
|
|
|
monkeypatch.setattr(persistence, "_publish_file_no_replace", fail_on_the_second)
|
|
with pytest.raises(OSError, match="injected publish failure") as raised:
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
# A failure that is not a definite refusal may have followed a link or
|
|
# rename that took effect (#665), so the second file is named as well.
|
|
assert raised.value.__notes__ == [
|
|
f"already published and left in place (the set may be incomplete): {first}; "
|
|
f"{second} may have been published and was left in place",
|
|
]
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
# Only the never-published private temporary file was removed.
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json"]
|
|
|
|
|
|
def test_exclusive_text_files_leave_a_replaced_published_path_alone(tmp_path, monkeypatch):
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
second.write_text("competitor report\n", encoding="utf-8")
|
|
real_sync = persistence._sync_directory
|
|
replaced = []
|
|
|
|
def another_writer_replaces_the_first_file(path):
|
|
real_sync(path)
|
|
if first.exists() and not replaced:
|
|
replacement = tmp_path / "other-writer.tmp"
|
|
replacement.write_text("other writer\n", encoding="utf-8")
|
|
os.replace(replacement, first)
|
|
replaced.append(first)
|
|
|
|
monkeypatch.setattr(persistence, "_sync_directory", another_writer_replaces_the_first_file)
|
|
with pytest.raises(FileExistsError, match="already exists"):
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
assert replaced == [first]
|
|
assert first.read_text(encoding="utf-8") == "other writer\n"
|
|
assert second.read_text(encoding="utf-8") == "competitor report\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json", "report.json"]
|
|
|
|
|
|
def test_exclusive_text_files_publish_by_no_replace_rename_when_links_are_unsupported(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
import errno
|
|
|
|
def link_unsupported(_source, _destination):
|
|
raise OSError(errno.EPERM, "hard links are not supported")
|
|
|
|
monkeypatch.setattr(persistence.os, "link", link_unsupported)
|
|
opened_in_place = []
|
|
real_open = Path.open
|
|
|
|
def tracking_open(self, *args, **kwargs):
|
|
if self.name in {"config.json", "report.json"}:
|
|
opened_in_place.append(self.name)
|
|
return real_open(self, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(Path, "open", tracking_open)
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
assert opened_in_place == []
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
assert second.read_text(encoding="utf-8") == "report\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json", "report.json"]
|
|
|
|
|
|
def test_exclusive_text_files_fail_closed_without_an_atomic_no_replace_primitive(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
import errno
|
|
|
|
def link_unsupported(_source, _destination):
|
|
raise OSError(errno.EPERM, "hard links are not supported")
|
|
|
|
def rename_unsupported(_source, _destination):
|
|
raise persistence.NoReplaceRenameUnsupported("no atomic no-replace rename here")
|
|
|
|
monkeypatch.setattr(persistence.os, "link", link_unsupported)
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename_unsupported)
|
|
first = tmp_path / "config.json"
|
|
with pytest.raises(persistence.NoReplaceRenameUnsupported) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
# A definite refusal published nothing, so nothing is named as maybe published.
|
|
assert getattr(raised.value, "__notes__", []) == []
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def _link_then_report_eio(monkeypatch, *, link=True):
|
|
import errno
|
|
|
|
real_link = os.link
|
|
|
|
def link_then_fail(source, destination):
|
|
if link:
|
|
real_link(source, destination) # the link takes effect ...
|
|
raise OSError(errno.EIO, "injected I/O error after the link") # ... yet reports EIO
|
|
|
|
monkeypatch.setattr(persistence.os, "link", link_then_fail)
|
|
|
|
|
|
def _identity_cannot_be_told(monkeypatch):
|
|
"""A volume without file IDs: no entry's identity can be compared."""
|
|
monkeypatch.setattr(persistence, "_usable_identity", lambda _status: None)
|
|
|
|
|
|
def _forbid_rename(monkeypatch):
|
|
def rename(_source, _destination):
|
|
raise AssertionError("the fallback rename must not run")
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename)
|
|
|
|
|
|
def test_exclusive_text_files_count_a_link_that_took_effect_despite_eio_as_published(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# #675 E3: a hard link that took effect but reported EIO is identified as
|
|
# this write's own link by the private temporary file's identity, so the
|
|
# file is published on every platform and no rename is attempted.
|
|
_link_then_report_eio(monkeypatch)
|
|
_forbid_rename(monkeypatch)
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
assert second.read_text(encoding="utf-8") == "report\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json", "report.json"]
|
|
|
|
|
|
def test_exclusive_text_files_report_a_foreign_entry_after_eio_as_an_existing_output(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# The link failed with EIO without linking, and another writer's file is
|
|
# there: a different identity is a definite conflict, not an uncertainty.
|
|
_link_then_report_eio(monkeypatch, link=False)
|
|
_forbid_rename(monkeypatch)
|
|
first = tmp_path / "config.json"
|
|
first.write_text("other writer\n", encoding="utf-8")
|
|
|
|
with pytest.raises(FileExistsError) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
|
|
assert str(raised.value) == f"Output already exists: {first}"
|
|
assert getattr(raised.value, "__notes__", []) == []
|
|
assert first.read_text(encoding="utf-8") == "other writer\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json"]
|
|
|
|
|
|
def test_exclusive_text_files_report_an_uncertain_link_not_an_existing_output(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
# #675 E3: where the identity cannot be told, the fallback rename decides.
|
|
# POSIX no-replace renames refuse the write's own link (EEXIST), which must
|
|
# not be reported as a definite "Output already exists". Windows renames
|
|
# onto a hard link of the same file succeed, so the write publishes there.
|
|
_link_then_report_eio(monkeypatch)
|
|
_identity_cannot_be_told(monkeypatch)
|
|
first = tmp_path / "config.json"
|
|
|
|
if os.name == "nt":
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json"]
|
|
return
|
|
with pytest.raises(OSError) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
|
|
assert not isinstance(raised.value, FileExistsError)
|
|
assert "Output already exists" not in str(raised.value)
|
|
assert str(raised.value) == (
|
|
f"Publishing {first} is uncertain: the hard link reported an error "
|
|
"([Errno 5] injected I/O error after the link) but may have taken effect, and "
|
|
f"the fallback rename found {first} present, so it may already have been "
|
|
"published by this write. It was left in place; inspect it."
|
|
)
|
|
assert isinstance(raised.value.__cause__, FileExistsError)
|
|
assert raised.value.__notes__ == [f"{first} may have been published and was left in place"]
|
|
# The output is reported, never removed; only the private temporary file is.
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json"]
|
|
|
|
|
|
def test_exclusive_text_files_do_not_call_an_uncertain_link_unpublished(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# #675 E3: the same uncertain link where no no-replace rename exists must
|
|
# not claim the file could not be published, or name nothing as published.
|
|
_link_then_report_eio(monkeypatch)
|
|
_identity_cannot_be_told(monkeypatch)
|
|
|
|
def rename_unsupported(_source, _destination):
|
|
raise persistence.NoReplaceRenameUnsupported("no atomic no-replace rename here")
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename_unsupported)
|
|
first = tmp_path / "config.json"
|
|
|
|
with pytest.raises(OSError) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
|
|
assert not isinstance(raised.value, persistence.NoReplaceRenameUnsupported)
|
|
assert str(raised.value) == (
|
|
f"Publishing {first} is uncertain: the hard link reported an error "
|
|
"([Errno 5] injected I/O error after the link) but may have taken effect, and "
|
|
"no atomic no-replace rename is available to retry it. It was left as it is; "
|
|
f"inspect {first}. {persistence._NO_REPLACE_ADVICE}"
|
|
)
|
|
assert isinstance(raised.value.__cause__, persistence.NoReplaceRenameUnsupported)
|
|
assert raised.value.__notes__ == [f"{first} may have been published and was left in place"]
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
|
|
|
|
def test_exclusive_text_files_report_an_existing_output_after_a_definite_link_failure(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
import errno
|
|
|
|
def link_unsupported(_source, _destination):
|
|
raise OSError(errno.EPERM, "hard links are not supported")
|
|
|
|
monkeypatch.setattr(persistence.os, "link", link_unsupported)
|
|
first = tmp_path / "config.json"
|
|
first.write_text("other writer\n", encoding="utf-8")
|
|
|
|
with pytest.raises(FileExistsError) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
|
|
assert str(raised.value) == f"Output already exists: {first}"
|
|
assert getattr(raised.value, "__notes__", []) == []
|
|
assert first.read_text(encoding="utf-8") == "other writer\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json"]
|
|
|
|
|
|
class _WindowsLinkError(OSError):
|
|
"""An ``EINVAL`` carrying a Windows error code, as ``os.link`` raises on FAT."""
|
|
|
|
def __init__(self, winerror):
|
|
super().__init__(22, "Incorrect function")
|
|
self.winerror = winerror
|
|
|
|
|
|
@pytest.mark.parametrize("winerror", [1, 50])
|
|
def test_windows_hard_links_unsupported_is_a_definite_link_failure(
|
|
tmp_path, monkeypatch, winerror,
|
|
):
|
|
def link_unsupported(_source, _destination):
|
|
raise _WindowsLinkError(winerror)
|
|
|
|
def rename_unsupported(_source, _destination):
|
|
raise persistence.NoReplaceRenameUnsupported("no atomic no-replace rename here")
|
|
|
|
monkeypatch.setattr(persistence.os, "link", link_unsupported)
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename_unsupported)
|
|
first = tmp_path / "config.json"
|
|
with pytest.raises(persistence.NoReplaceRenameUnsupported) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
assert "so nothing was published" in str(raised.value)
|
|
assert getattr(raised.value, "__notes__", []) == []
|
|
assert list(tmp_path.iterdir()) == []
|
|
# A bare EINVAL without such a code stays uncertain (fail closed).
|
|
assert not persistence._link_failure_is_definite(OSError(22, "Invalid argument"))
|
|
|
|
|
|
def test_exclusive_text_files_name_a_definite_link_failure_when_rename_is_unsupported(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
import errno
|
|
|
|
def link_unsupported(_source, _destination):
|
|
raise OSError(errno.EPERM, "hard links are not supported")
|
|
|
|
def rename_unsupported(_source, _destination):
|
|
raise persistence.NoReplaceRenameUnsupported("no atomic no-replace rename here")
|
|
|
|
monkeypatch.setattr(persistence.os, "link", link_unsupported)
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename_unsupported)
|
|
first = tmp_path / "config.json"
|
|
with pytest.raises(persistence.NoReplaceRenameUnsupported) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
assert str(raised.value) == (
|
|
f"cannot publish {first} atomically: hard links are unavailable here "
|
|
"([Errno 1] hard links are not supported) and no atomic no-replace rename is "
|
|
f"available, so nothing was published. {persistence._NO_REPLACE_ADVICE}"
|
|
)
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def test_no_replace_rename_refuses_existing_files_and_directories(tmp_path):
|
|
for kind in ("file", "directory"):
|
|
source = tmp_path / f"{kind}-source"
|
|
target = tmp_path / f"{kind}-target"
|
|
if kind == "file":
|
|
source.write_text("new", encoding="utf-8")
|
|
target.write_text("old", encoding="utf-8")
|
|
else:
|
|
source.mkdir()
|
|
target.mkdir()
|
|
try:
|
|
with pytest.raises(FileExistsError):
|
|
persistence._rename_no_replace(source, target)
|
|
except persistence.NoReplaceRenameUnsupported:
|
|
pytest.skip("platform has no atomic no-replace rename")
|
|
assert source.exists() and target.exists()
|
|
fresh = tmp_path / f"{kind}-fresh"
|
|
persistence._rename_no_replace(source, fresh)
|
|
assert fresh.exists() and not source.exists()
|
|
|
|
|
|
def _published_checkpoint_sync_failure(destination, after_promotion):
|
|
"""Fail the parent sync that follows promotion, after running ``after_promotion``."""
|
|
real_sync = persistence._sync_directory
|
|
state = {"failed": False}
|
|
|
|
def sync(path):
|
|
model = destination / "model.bin"
|
|
promoted = model.is_file() and model.read_bytes() == b"ours"
|
|
if path == destination.parent and promoted and not state["failed"]:
|
|
state["failed"] = True
|
|
after_promotion()
|
|
raise OSError("injected parent directory sync failure")
|
|
real_sync(path)
|
|
|
|
return sync, state
|
|
|
|
|
|
def _another_writer_swaps(destination, moved_ours):
|
|
os.rename(destination, moved_ours)
|
|
destination.mkdir()
|
|
(destination / "winner.txt").write_text("other writer", encoding="utf-8")
|
|
|
|
|
|
def test_published_no_replace_checkpoint_is_never_rolled_back(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
moved_ours = tmp_path / "moved-ours"
|
|
sync, state = _published_checkpoint_sync_failure(
|
|
destination, lambda: _another_writer_swaps(destination, moved_ours),
|
|
)
|
|
monkeypatch.setattr(persistence, "_sync_directory", sync)
|
|
with pytest.raises(RuntimeError) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination, replace_existing=False) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert state["failed"]
|
|
assert str(raised.value) == (
|
|
f"Checkpoint was published to {destination}, but syncing {tmp_path} failed, so the "
|
|
"rename may not survive a crash. Nothing was rolled back or removed; inspect "
|
|
f"{destination}."
|
|
)
|
|
assert str(raised.value.__cause__) == "injected parent directory sync failure"
|
|
assert sorted(path.name for path in destination.iterdir()) == ["winner.txt"]
|
|
assert (moved_ours / "model.bin").read_bytes() == b"ours"
|
|
|
|
|
|
def test_published_no_replace_checkpoint_never_cleans_a_recreated_staging_path(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
# The default replace path keeps its upstream rollback, which reuses the
|
|
# staging pathname; that pre-existing race is tracked separately.
|
|
destination = tmp_path / "checkpoint"
|
|
recreated = []
|
|
|
|
def another_writer_recreates_the_staging_path():
|
|
staging = recreated_paths[0]
|
|
staging.mkdir()
|
|
(staging / "theirs.txt").write_text("other writer", encoding="utf-8")
|
|
recreated.append(staging)
|
|
|
|
recreated_paths = []
|
|
sync, state = _published_checkpoint_sync_failure(
|
|
destination, another_writer_recreates_the_staging_path,
|
|
)
|
|
monkeypatch.setattr(persistence, "_sync_directory", sync)
|
|
with pytest.raises(RuntimeError, match="Nothing was rolled back or removed"):
|
|
with persistence.atomic_checkpoint_directory(
|
|
destination, replace_existing=False,
|
|
) as staging:
|
|
recreated_paths.append(staging)
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert state["failed"] and recreated
|
|
assert (recreated[0] / "theirs.txt").read_text(encoding="utf-8") == "other writer"
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
|
|
|
|
def test_no_replace_rename_maps_directory_conflicts_to_file_exists():
|
|
import errno
|
|
|
|
for code in (errno.EEXIST, errno.ENOTEMPTY):
|
|
with pytest.raises(FileExistsError):
|
|
persistence._raise_rename_error(code, Path("a"), Path("b"))
|
|
with pytest.raises(persistence.NoReplaceRenameUnsupported, match="local filesystem"):
|
|
persistence._raise_rename_error(errno.EINVAL, Path("a"), Path("b"))
|
|
with pytest.raises(OSError) as raised:
|
|
persistence._raise_rename_error(errno.EACCES, Path("a"), Path("b"))
|
|
assert not isinstance(raised.value, (FileExistsError, persistence.NoReplaceRenameUnsupported))
|
|
|
|
|
|
class _InterruptAfterRename(BaseException):
|
|
"""Stands in for an asynchronous interruption after the rename returned."""
|
|
|
|
|
|
def test_interrupted_no_replace_promotion_never_rolls_back_or_cleans_staging(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
real_rename = persistence._rename_no_replace
|
|
seen = {}
|
|
|
|
def rename_then_interrupt(source, target):
|
|
real_rename(source, target)
|
|
# Another process reuses the now-free staging pathname.
|
|
source.mkdir()
|
|
(source / "theirs.txt").write_text("other writer", encoding="utf-8")
|
|
seen["staging"] = source
|
|
raise _InterruptAfterRename()
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename_then_interrupt)
|
|
# A cancellation stays a cancellation: it is re-raised with the outcome as
|
|
# a note instead of being converted into a RuntimeError (#649).
|
|
with pytest.raises(_InterruptAfterRename) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination, replace_existing=False) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
staging = seen["staging"]
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (staging / "theirs.txt").read_text(encoding="utf-8") == "other writer"
|
|
assert raised.value.__notes__ == [
|
|
f"Publishing the checkpoint to {destination} was interrupted, so it is unknown "
|
|
f"whether it was published. Nothing was rolled back or removed; inspect "
|
|
f"{destination} and {staging}.",
|
|
]
|
|
|
|
|
|
def test_failed_no_replace_promotion_never_touches_a_foreign_destination(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
seen = {}
|
|
|
|
def staging_vanishes_and_a_foreign_destination_appears(source, target):
|
|
persistence._remove_checkpoint_path(source)
|
|
target.mkdir()
|
|
(target / "foreign.txt").write_text("other writer", encoding="utf-8")
|
|
seen["staging"] = source
|
|
raise OSError("injected rename failure")
|
|
|
|
monkeypatch.setattr(
|
|
persistence, "_rename_no_replace", staging_vanishes_and_a_foreign_destination_appears,
|
|
)
|
|
with pytest.raises(RuntimeError, match="unknown whether it was published"):
|
|
with persistence.atomic_checkpoint_directory(destination, replace_existing=False) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert sorted(path.name for path in destination.iterdir()) == ["foreign.txt"]
|
|
assert not seen["staging"].exists()
|
|
|
|
|
|
def test_definite_no_replace_conflict_still_removes_unpublished_staging(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
|
|
def conflict(_source, target):
|
|
target.mkdir()
|
|
raise FileExistsError(17, "File exists", str(target))
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", conflict)
|
|
with pytest.raises(FileExistsError, match="already exists"):
|
|
with persistence.atomic_checkpoint_directory(destination, replace_existing=False) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert list(destination.iterdir()) == []
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == [".checkpoint.lock", "checkpoint"]
|
|
|
|
|
|
# Default (replace) commit rollback must only move the destination it promoted (#645).
|
|
|
|
|
|
def _another_writer_replaces(destination, moved_ours, kind):
|
|
"""Move the promoted checkpoint away and put a foreign entry at ``destination``."""
|
|
os.rename(destination, moved_ours)
|
|
if kind == "directory":
|
|
destination.mkdir()
|
|
(destination / "winner.txt").write_text("other writer", encoding="utf-8")
|
|
elif kind == "file":
|
|
destination.write_text("other writer", encoding="utf-8")
|
|
else:
|
|
target = destination.with_name("foreign-target")
|
|
target.mkdir()
|
|
(target / "winner.txt").write_text("other writer", encoding="utf-8")
|
|
destination.symlink_to(target, target_is_directory=True)
|
|
|
|
|
|
def _assert_foreign_entry_untouched(destination, kind):
|
|
if kind == "directory":
|
|
assert sorted(path.name for path in destination.iterdir()) == ["winner.txt"]
|
|
elif kind == "file":
|
|
assert destination.read_text(encoding="utf-8") == "other writer"
|
|
else:
|
|
assert destination.is_symlink()
|
|
assert (destination / "winner.txt").read_text(encoding="utf-8") == "other writer"
|
|
|
|
|
|
def _holds_staging_handle() -> bool:
|
|
"""Whether staging is pinned by a held descriptor here (the check ``_open_directory_handle`` uses).
|
|
|
|
False on Windows and other platforms without descriptor-based removal,
|
|
where post-promotion rollback refuses because nothing is pinned (#667).
|
|
"""
|
|
return bool(persistence.shutil.rmtree.avoids_symlink_attacks)
|
|
|
|
|
|
@contextmanager
|
|
def _post_promotion_rollback(expected=None, match=None):
|
|
"""Expect ``expected`` where staging is pinned; elsewhere the unpinned refusal (#667)."""
|
|
if _holds_staging_handle():
|
|
with pytest.raises(expected or persistence.CheckpointRollbackRefused, match=match) as raised:
|
|
yield raised
|
|
else:
|
|
with pytest.raises(persistence.CheckpointRollbackRefused, match="not provably") as raised:
|
|
yield raised
|
|
|
|
|
|
def _unverified_staging_note(staging, *, refused):
|
|
"""The suffix a failed rollback adds when no staging handle was held and staging remains."""
|
|
if _holds_staging_handle() or not os.path.lexists(staging):
|
|
return ""
|
|
if refused:
|
|
return f" The staging path {staging} was also left in place, unverified."
|
|
return f"; the staging path {staging} was also left in place, unverified"
|
|
|
|
|
|
def _assert_refused_without_a_pin(raised, destination, staging, ours=b"ours", prior=b"prior"):
|
|
"""On a platform without a held staging handle, nothing was moved or removed."""
|
|
backups = list(destination.parent.glob(f".{destination.name}.backup-*"))
|
|
assert len(backups) <= 1
|
|
backup = backups[0] if backups else None
|
|
kept = (
|
|
f"the previous checkpoint was kept at {backup}."
|
|
if backup is not None else "no previous checkpoint existed."
|
|
)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: it is not provably the checkpoint "
|
|
"this write promoted, so another writer may have replaced it. Nothing was moved or "
|
|
f"removed; {kept}" + _unverified_staging_note(staging, refused=True)
|
|
)
|
|
assert (destination / "model.bin").read_bytes() == ours
|
|
if backup is not None:
|
|
assert (backup / "model.bin" if backup.is_dir() else backup).read_bytes() == prior
|
|
|
|
|
|
def _only_backup(parent: Path) -> Path:
|
|
backups = list(parent.glob(".checkpoint.backup-*"))
|
|
assert len(backups) == 1
|
|
return backups[0]
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["directory", "file", "symlink"])
|
|
def test_default_rollback_never_moves_a_destination_replaced_after_promotion(
|
|
tmp_path, monkeypatch, kind,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
moved_ours = tmp_path / "moved-ours"
|
|
sync, state = _published_checkpoint_sync_failure(
|
|
destination, lambda: _another_writer_replaces(destination, moved_ours, kind),
|
|
)
|
|
monkeypatch.setattr(persistence, "_sync_directory", sync)
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert state["failed"]
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: it is not provably the checkpoint "
|
|
"this write promoted, so another writer may have replaced it. Nothing was moved or "
|
|
f"removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
assert str(raised.value.__cause__) == "injected parent directory sync failure"
|
|
_assert_foreign_entry_untouched(destination, kind)
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
assert (moved_ours / "model.bin").read_bytes() == b"ours"
|
|
|
|
|
|
def test_default_rollback_of_a_new_destination_never_moves_a_replacement(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
moved_ours = tmp_path / "moved-ours"
|
|
sync, state = _published_checkpoint_sync_failure(
|
|
destination, lambda: _another_writer_replaces(destination, moved_ours, "directory"),
|
|
)
|
|
monkeypatch.setattr(persistence, "_sync_directory", sync)
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert state["failed"]
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: it is not provably the checkpoint "
|
|
"this write promoted, so another writer may have replaced it. Nothing was moved or "
|
|
"removed; no previous checkpoint existed."
|
|
)
|
|
_assert_foreign_entry_untouched(destination, "directory")
|
|
assert list(tmp_path.glob(".checkpoint.backup-*")) == []
|
|
|
|
|
|
def test_default_rollback_still_restores_its_own_promoted_checkpoint(tmp_path, monkeypatch, portable_no_replace_rename):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
sync, state = _published_checkpoint_sync_failure(destination, lambda: None)
|
|
monkeypatch.setattr(persistence, "_sync_directory", sync)
|
|
|
|
with _post_promotion_rollback(OSError, match="injected parent directory sync failure") as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert state["failed"]
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
assert (destination / "model.bin").read_bytes() == b"prior"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == [".checkpoint.lock", "checkpoint"]
|
|
|
|
|
|
def _replace_inside_check_to_move_window(monkeypatch, destination, moved_ours, fail_move_back=False):
|
|
real_rename = persistence._rename_no_replace
|
|
seen = {}
|
|
|
|
def rename(source, target):
|
|
if Path(source) == destination and ".staging-" in Path(target).name:
|
|
# Another writer wins the window between the identity check and the move.
|
|
_another_writer_replaces(destination, moved_ours, "directory")
|
|
seen["staging"] = Path(target)
|
|
elif fail_move_back and ".staging-" in Path(source).name and Path(target) == destination:
|
|
raise OSError("injected move-back failure")
|
|
return real_rename(source, target)
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename)
|
|
return seen
|
|
|
|
|
|
def test_default_rollback_moves_back_a_destination_replaced_during_the_move(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
moved_ours = tmp_path / "moved-ours"
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
seen = _replace_inside_check_to_move_window(monkeypatch, destination, moved_ours)
|
|
|
|
with _post_promotion_rollback() as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
assert seen == {} # refused before any move, so the window never opened
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
assert seen["staging"] == staging
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback found {destination} replaced while moving it aside: the entry "
|
|
"it moved is not the checkpoint this write promoted, so it was moved back to "
|
|
f"{destination}. Nothing was removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
_assert_foreign_entry_untouched(destination, "directory")
|
|
assert not staging.exists()
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
assert (moved_ours / "model.bin").read_bytes() == b"ours"
|
|
|
|
|
|
def test_default_rollback_keeps_a_replaced_entry_at_staging_when_moving_back_fails(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
moved_ours = tmp_path / "moved-ours"
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
_replace_inside_check_to_move_window(monkeypatch, destination, moved_ours, fail_move_back=True)
|
|
|
|
with _post_promotion_rollback() as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback found {destination} replaced while moving it aside: the entry "
|
|
"it moved is not the checkpoint this write promoted, and moving it back failed "
|
|
f"(injected move-back failure), so it was left at {staging}. Nothing was removed; "
|
|
f"the previous checkpoint was kept at {backup}."
|
|
)
|
|
assert sorted(path.name for path in staging.iterdir()) == ["winner.txt"]
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_rollback_refuses_to_move_a_destination_whose_identity_is_unknown(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
backup = tmp_path / ".checkpoint.backup-owned"
|
|
destination.mkdir()
|
|
(destination / "theirs.txt").write_text("unknown owner", encoding="utf-8")
|
|
backup.mkdir()
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused, match="not provably"):
|
|
persistence._rollback_checkpoint_commit(
|
|
destination, staging, backup, had_destination=True, promoted_identity=None,
|
|
)
|
|
|
|
assert (destination / "theirs.txt").read_text(encoding="utf-8") == "unknown owner"
|
|
assert backup.is_dir() and not staging.exists()
|
|
|
|
|
|
class _CancelledCommit(BaseException):
|
|
"""Stands in for KeyboardInterrupt/SystemExit during a default commit."""
|
|
|
|
|
|
def test_cancelled_default_commit_keeps_cancellation_when_rollback_is_refused(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
moved_ours = tmp_path / "moved-ours"
|
|
real_sync = persistence._sync_directory
|
|
state = {"cancelled": False}
|
|
|
|
def cancel_after_promotion(path):
|
|
model = destination / "model.bin"
|
|
if (path == tmp_path and not state["cancelled"] and model.is_file()
|
|
and model.read_bytes() == b"ours"):
|
|
state["cancelled"] = True
|
|
_another_writer_replaces(destination, moved_ours, "directory")
|
|
raise _CancelledCommit()
|
|
real_sync(path)
|
|
|
|
monkeypatch.setattr(persistence, "_sync_directory", cancel_after_promotion)
|
|
with pytest.raises(_CancelledCommit) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert raised.value.__notes__ == [
|
|
f"Checkpoint rollback left {destination} in place: it is not provably the checkpoint "
|
|
"this write promoted, so another writer may have replaced it. Nothing was moved or "
|
|
f"removed; the previous checkpoint was kept at {backup}."
|
|
]
|
|
_assert_foreign_entry_untouched(destination, "directory")
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_cancellation_during_default_rollback_is_not_wrapped(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
real_replace = os.replace
|
|
|
|
def fail_promotion(source, target):
|
|
if ".staging-" in Path(source).name:
|
|
raise OSError("injected promotion failure")
|
|
return real_replace(source, target)
|
|
|
|
def cancel_restore(source, target):
|
|
raise _CancelledCommit()
|
|
|
|
monkeypatch.setattr(persistence.os, "replace", fail_promotion)
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", cancel_restore)
|
|
with pytest.raises(_CancelledCommit) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert raised.value.__notes__ == [
|
|
f"Checkpoint promotion and rollback both failed; recover the previous checkpoint from {backup}"
|
|
+ _unverified_staging_note(staging, refused=False)
|
|
]
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def _fail_after_promotion(monkeypatch, destination, after_promotion=lambda: None):
|
|
sync, state = _published_checkpoint_sync_failure(destination, after_promotion)
|
|
monkeypatch.setattr(persistence, "_sync_directory", sync)
|
|
return state
|
|
|
|
|
|
def _prior_checkpoint(destination):
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"prior")
|
|
|
|
|
|
def _move_aside_then(monkeypatch, destination, action):
|
|
"""Run ``action(staging)`` right after rollback moves ``destination`` to staging."""
|
|
real_rename = persistence._rename_no_replace
|
|
seen = {}
|
|
|
|
def rename(source, target):
|
|
result = real_rename(source, target)
|
|
if Path(source) == destination and ".staging-" in Path(target).name:
|
|
seen["staging"] = Path(target)
|
|
action(Path(target))
|
|
return result
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", rename)
|
|
return seen
|
|
|
|
|
|
@pytest.mark.parametrize("error", [
|
|
PermissionError(13, "injected permission denied"),
|
|
OSError(5, "injected I/O error"),
|
|
])
|
|
def test_default_rollback_keeps_staging_when_the_moved_entry_cannot_be_verified(
|
|
tmp_path, monkeypatch, portable_no_replace_rename, error,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
real_check = persistence._names_promoted_checkpoint
|
|
|
|
def unverifiable_after_the_move(path, identity):
|
|
if ".staging-" in path.name:
|
|
raise error
|
|
return real_check(path, identity)
|
|
|
|
monkeypatch.setattr(persistence, "_names_promoted_checkpoint", unverifiable_after_the_move)
|
|
with _post_promotion_rollback() as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback moved {destination} aside to {staging} but could not verify "
|
|
f"that it is the checkpoint this write promoted ({error}); it was left there. "
|
|
f"Nothing was removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
# The refusal keeps its own cause (#675); the commit error is its context.
|
|
assert raised.value.__cause__ is error
|
|
assert str(error.__context__) == "injected parent directory sync failure"
|
|
assert (staging / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
assert not destination.exists()
|
|
|
|
|
|
def test_interrupt_between_move_aside_and_verification_keeps_staging(tmp_path, monkeypatch, portable_no_replace_rename):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
|
|
def interrupt(_staging):
|
|
raise _CancelledCommit()
|
|
|
|
seen = _move_aside_then(monkeypatch, destination, interrupt)
|
|
with _post_promotion_rollback(_CancelledCommit) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
backup = _only_backup(tmp_path)
|
|
assert seen["staging"] == staging
|
|
assert raised.value.__notes__ == [
|
|
f"Checkpoint promotion and rollback both failed; recover the previous checkpoint "
|
|
f"from {backup}; {destination} may have been moved aside to {staging}, which was "
|
|
"left unverified"
|
|
]
|
|
assert (staging / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def _create_foreign(destination, kind):
|
|
if kind == "file":
|
|
destination.write_text("other writer", encoding="utf-8")
|
|
elif kind == "symlink":
|
|
target = destination.with_name("foreign-target")
|
|
target.mkdir()
|
|
(target / "winner.txt").write_text("other writer", encoding="utf-8")
|
|
destination.symlink_to(target, target_is_directory=True)
|
|
else:
|
|
destination.mkdir()
|
|
|
|
|
|
@pytest.mark.parametrize("backup_kind", ["directory", "file"])
|
|
@pytest.mark.parametrize("kind", ["file", "symlink", "empty_directory"])
|
|
def test_default_rollback_never_overwrites_an_entry_created_before_restore(
|
|
tmp_path, monkeypatch, portable_no_replace_rename, kind, backup_kind,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
if backup_kind == "directory":
|
|
_prior_checkpoint(destination)
|
|
else:
|
|
destination.write_bytes(b"prior")
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
_move_aside_then(monkeypatch, destination, lambda _staging: _create_foreign(destination, kind))
|
|
|
|
with _post_promotion_rollback() as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback could not restore {backup} to {destination}: another writer "
|
|
f"created {destination} in the meantime. Nothing was overwritten; the previous "
|
|
f"checkpoint was kept at {backup}."
|
|
)
|
|
if kind == "file":
|
|
assert destination.read_text(encoding="utf-8") == "other writer"
|
|
elif kind == "symlink":
|
|
assert destination.is_symlink()
|
|
assert (destination / "winner.txt").read_text(encoding="utf-8") == "other writer"
|
|
else:
|
|
assert destination.is_dir() and not destination.is_symlink()
|
|
assert list(destination.iterdir()) == []
|
|
prior = backup / "model.bin" if backup_kind == "directory" else backup
|
|
assert prior.read_bytes() == b"prior"
|
|
# Our own promoted directory was positively identified, so it is cleaned up.
|
|
assert not staging.exists()
|
|
|
|
|
|
def test_default_rollback_moves_nothing_without_no_replace_rename(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
|
|
def unsupported(_source, _target):
|
|
raise persistence.NoReplaceRenameUnsupported("injected: unsupported")
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", unsupported)
|
|
with _post_promotion_rollback() as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left the promoted checkpoint in place at {destination}: this "
|
|
"filesystem has no atomic no-replace rename, so it could not be moved or replaced "
|
|
"without risking another writer's entry. Nothing was moved or removed; the previous "
|
|
f"checkpoint was kept at {backup}."
|
|
)
|
|
# No worse than before for the in-place checkpoint: it is complete and kept.
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
assert not staging.exists()
|
|
|
|
|
|
def test_default_rollback_keeps_the_backup_when_restore_lacks_no_replace_rename(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# Promotion itself failed, so the only step is the restore, which refuses.
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
real_replace = os.replace
|
|
|
|
def fail_promotion(source, target):
|
|
if ".staging-" in Path(source).name:
|
|
raise OSError("injected promotion failure")
|
|
return real_replace(source, target)
|
|
|
|
def unsupported(_source, _target):
|
|
raise persistence.NoReplaceRenameUnsupported("injected: unsupported")
|
|
|
|
monkeypatch.setattr(persistence.os, "replace", fail_promotion)
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", unsupported)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback could not restore {backup} to {destination} without risking "
|
|
"overwriting another writer's entry: this filesystem has no atomic no-replace "
|
|
f"rename. Nothing was overwritten; the previous checkpoint was kept at {backup}."
|
|
+ _unverified_staging_note(staging, refused=True)
|
|
)
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_default_rollback_fails_closed_without_a_pinned_staging_identity(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
monkeypatch.setattr(persistence, "_open_directory_handle", lambda _path: None)
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused, match="not provably") as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert str(backup) in str(raised.value)
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
assert not staging.exists()
|
|
|
|
|
|
def test_windows_without_a_pinned_handle_refuses_rollback(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
# #667: Windows cannot hold a directory descriptor, so the promoted
|
|
# directory's file ID is never pinned. An unpinned lstat identity could be
|
|
# reused by a foreign entry, so rollback refuses there exactly as on POSIX.
|
|
# The simulation is a platform without descriptor-held staging, with the
|
|
# former Windows switch set the way the Windows lane set it.
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
monkeypatch.setattr(persistence, "_UNPINNED_IDENTITY_ALLOWED", True, raising=False)
|
|
monkeypatch.setattr(persistence, "_open_directory_handle", lambda _path: None)
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused, match="not provably") as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert str(backup) in str(raised.value)
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
assert persistence._promoted_identity(destination, None) is None
|
|
|
|
|
|
def test_windows_without_a_pinned_handle_still_restores_after_a_failed_promotion(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
# Restoring the backup to a vacated destination needs no identity, so the
|
|
# common Windows failure (the promotion rename itself fails) still rolls back.
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
monkeypatch.setattr(persistence, "_open_directory_handle", lambda _path: None)
|
|
real_replace = persistence.os.replace
|
|
|
|
def fail_promotion(source, target):
|
|
if ".staging-" in Path(source).name:
|
|
raise PermissionError(13, "injected sharing violation")
|
|
return real_replace(source, target)
|
|
|
|
monkeypatch.setattr(persistence.os, "replace", fail_promotion)
|
|
with pytest.raises(PermissionError, match="injected sharing violation"):
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert (destination / "model.bin").read_bytes() == b"prior"
|
|
assert list(tmp_path.glob(".checkpoint.backup-*")) == []
|
|
|
|
|
|
def _exception_chain(error):
|
|
"""Every exception reachable through ``__cause__`` (or, failing that, ``__context__``)."""
|
|
chain = []
|
|
while error is not None and error not in chain:
|
|
chain.append(error)
|
|
error = error.__cause__ or error.__context__
|
|
return chain
|
|
|
|
|
|
@pytest.mark.parametrize("pinned", [True, False])
|
|
def test_refused_rollback_keeps_the_refusal_cause_and_the_commit_error_in_one_chain(
|
|
tmp_path, monkeypatch, portable_no_replace_rename, pinned,
|
|
):
|
|
# #675 E1: whether or not the refusal text is extended (it is when no
|
|
# staging handle was held and staging remains), the raised refusal keeps
|
|
# the refusal's own cause (here the restore rename's FileExistsError) and
|
|
# the commit error that started the rollback reachable in one chain.
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
if not pinned:
|
|
monkeypatch.setattr(persistence, "_open_directory_handle", lambda _path: None)
|
|
real_replace = persistence.os.replace
|
|
real_rename_no_replace = persistence._rename_no_replace
|
|
commit_errors = []
|
|
|
|
def fail_promotion(source, target):
|
|
if ".staging-" in Path(source).name:
|
|
commit_errors.append(PermissionError(13, "injected promotion failure"))
|
|
raise commit_errors[-1]
|
|
return real_replace(source, target)
|
|
|
|
def a_writer_takes_the_destination_before_restore(source, target):
|
|
if ".backup-" in Path(source).name:
|
|
Path(target).write_text("other writer", encoding="utf-8")
|
|
return real_rename_no_replace(source, target)
|
|
|
|
monkeypatch.setattr(persistence.os, "replace", fail_promotion)
|
|
monkeypatch.setattr(
|
|
persistence, "_rename_no_replace", a_writer_takes_the_destination_before_restore,
|
|
)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
refusal_text = (
|
|
f"Checkpoint rollback could not restore {backup} to {destination}: another writer "
|
|
f"created {destination} in the meantime. Nothing was overwritten; the previous "
|
|
f"checkpoint was kept at {backup}."
|
|
)
|
|
extended = not (pinned and _holds_staging_handle())
|
|
if extended:
|
|
assert str(raised.value) == (
|
|
refusal_text
|
|
+ f" The staging path {staging} was also left in place, unverified."
|
|
)
|
|
assert staging.is_dir()
|
|
else:
|
|
assert str(raised.value) == refusal_text
|
|
assert not staging.exists()
|
|
chain = _exception_chain(raised.value)
|
|
refusals = [error for error in chain if str(error) == refusal_text]
|
|
assert len(refusals) == 1
|
|
refusal = refusals[0]
|
|
assert isinstance(refusal, persistence.CheckpointRollbackRefused)
|
|
if extended:
|
|
assert raised.value.__cause__ is refusal
|
|
# The restore refusal's own cause is never overwritten by the commit error.
|
|
assert isinstance(refusal.__cause__, FileExistsError)
|
|
assert chain[-1] is commit_errors[0]
|
|
assert chain.index(refusal) < chain.index(refusal.__cause__) < len(chain) - 1
|
|
assert destination.read_text(encoding="utf-8") == "other writer"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def _zero_inode_status(path):
|
|
real = os.lstat(path)
|
|
return os.stat_result((real.st_mode, 0, real.st_dev, real.st_nlink, real.st_uid,
|
|
real.st_gid, real.st_size, 0, 0, 0))
|
|
|
|
|
|
def test_zero_inode_identity_is_never_proof_of_ownership(tmp_path, monkeypatch):
|
|
# Volumes without file IDs report st_ino == 0 for every entry.
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
staging.mkdir()
|
|
if _holds_staging_handle():
|
|
# Directories can only be opened as descriptors where the code holds one (not on Windows).
|
|
handle = os.open(staging, os.O_RDONLY)
|
|
try:
|
|
monkeypatch.setattr(persistence.os, "fstat", lambda _fd: _zero_inode_status(staging))
|
|
assert persistence._promoted_identity(staging, handle) is None
|
|
finally:
|
|
monkeypatch.undo()
|
|
os.close(handle)
|
|
assert persistence._promoted_identity(staging, None) is None
|
|
monkeypatch.setattr(
|
|
persistence.os, "stat", lambda path, follow_symlinks=True: _zero_inode_status(path),
|
|
)
|
|
zero_identity = (os.lstat(staging).st_dev, 0)
|
|
assert not persistence._names_promoted_checkpoint(staging, zero_identity)
|
|
|
|
|
|
def test_rollback_on_a_volume_without_file_ids_refuses(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
backup = tmp_path / ".checkpoint.backup-owned"
|
|
destination.mkdir()
|
|
(destination / "theirs.txt").write_text("foreign replacement", encoding="utf-8")
|
|
backup.mkdir()
|
|
zero_identity = (os.lstat(destination).st_dev, 0)
|
|
monkeypatch.setattr(
|
|
persistence.os, "stat", lambda path, follow_symlinks=True: _zero_inode_status(path),
|
|
)
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused, match="not provably"):
|
|
persistence._rollback_checkpoint_commit(
|
|
destination, staging, backup, had_destination=True, promoted_identity=zero_identity,
|
|
)
|
|
|
|
monkeypatch.undo()
|
|
assert (destination / "theirs.txt").read_text(encoding="utf-8") == "foreign replacement"
|
|
assert backup.is_dir() and not staging.exists()
|
|
|
|
|
|
def test_rollback_reports_a_failed_move_aside_without_claiming_it_moved(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
real_rename = persistence._rename_no_replace
|
|
|
|
def fail_move_aside(source, target):
|
|
if Path(source) == destination:
|
|
raise OSError(5, "injected I/O error")
|
|
return real_rename(source, target)
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", fail_move_aside)
|
|
with _post_promotion_rollback() as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback could not move {destination} aside to {staging} ([Errno 5] "
|
|
"injected I/O error); it is unknown whether it moved, and both paths were left as "
|
|
f"they are. Nothing was removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_pre_move_identity_check_errors_are_refusals(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
backup = tmp_path / ".checkpoint.backup-owned"
|
|
destination.mkdir()
|
|
backup.mkdir()
|
|
|
|
def unreadable(path, identity):
|
|
raise PermissionError(13, "injected permission denied")
|
|
|
|
monkeypatch.setattr(persistence, "_names_promoted_checkpoint", unreadable)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
persistence._rollback_checkpoint_commit(
|
|
destination, staging, backup, had_destination=True, promoted_identity=(1, 2),
|
|
)
|
|
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: its identity could not be checked "
|
|
"([Errno 13] injected permission denied). Nothing was moved or removed; the previous "
|
|
f"checkpoint was kept at {backup}."
|
|
)
|
|
assert destination.is_dir() and backup.is_dir()
|
|
|
|
|
|
def test_refusal_without_a_held_handle_names_staging_only_when_something_is_there(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# The promoted directory was moved away by the other writer, so nothing is at
|
|
# the staging path and the message does not claim one was kept. The suffix for
|
|
# a kept staging entry is covered by the no-handle generic-failure test.
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
moved_ours = tmp_path / "moved-ours"
|
|
_fail_after_promotion(
|
|
monkeypatch, destination,
|
|
lambda: _another_writer_replaces(destination, moved_ours, "directory"),
|
|
)
|
|
monkeypatch.setattr(persistence, "_open_directory_handle", lambda _path: None)
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: it is not provably the checkpoint "
|
|
"this write promoted, so another writer may have replaced it. Nothing was moved or "
|
|
f"removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
assert not os.path.lexists(staging)
|
|
_assert_foreign_entry_untouched(destination, "directory")
|
|
|
|
|
|
def test_recovery_message_after_a_restore_whose_final_sync_fails(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
real_sync = persistence._sync_directory
|
|
state = {"failures": 0}
|
|
|
|
def fail_promotion_sync_and_rollback_sync(path):
|
|
model = destination / "model.bin"
|
|
promoted = model.is_file() and model.read_bytes() == b"ours"
|
|
if path == tmp_path and state["failures"] == 0 and promoted:
|
|
state["failures"] = 1
|
|
raise OSError("injected promotion sync failure")
|
|
if path == tmp_path and state["failures"] == 1:
|
|
state["failures"] = 2
|
|
raise OSError("injected rollback sync failure")
|
|
real_sync(path)
|
|
|
|
monkeypatch.setattr(persistence, "_sync_directory", fail_promotion_sync_and_rollback_sync)
|
|
with _post_promotion_rollback(RuntimeError) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
if not _holds_staging_handle():
|
|
_assert_refused_without_a_pin(raised, destination, staging)
|
|
return
|
|
assert str(raised.value) == (
|
|
f"Checkpoint promotion failed and rollback restored the previous checkpoint at "
|
|
f"{destination}, but did not complete; the restore may not be durable"
|
|
)
|
|
assert (destination / "model.bin").read_bytes() == b"prior"
|
|
assert list(tmp_path.glob(".checkpoint.backup-*")) == []
|
|
|
|
|
|
def test_held_staging_cleanup_leaves_a_path_it_cannot_confirm_on_a_volume_without_file_ids(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
staging.mkdir()
|
|
(staging / "keep.txt").write_text("unconfirmed", encoding="utf-8")
|
|
handle = persistence._open_directory_handle(staging)
|
|
if handle is None: # pragma: no cover - no descriptor-based removal here
|
|
pytest.skip("descriptor-held cleanup is unavailable on this platform")
|
|
try:
|
|
monkeypatch.setattr(persistence.os, "fstat", lambda _fd: _zero_inode_status(staging))
|
|
monkeypatch.setattr(
|
|
persistence.os, "stat",
|
|
lambda path, follow_symlinks=True, dir_fd=None: _zero_inode_status(path),
|
|
)
|
|
assert not persistence._names_held_directory(staging, handle)
|
|
persistence._cleanup_failed_staging(staging, handle)
|
|
monkeypatch.undo()
|
|
finally:
|
|
os.close(handle)
|
|
|
|
assert (staging / "keep.txt").read_text(encoding="utf-8") == "unconfirmed"
|
|
|
|
|
|
def test_rollback_refuses_when_staging_was_recreated_over_a_new_destination(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# State (no backup expected, D, S): the failed commit's promoted checkpoint
|
|
# cannot be moved aside, so rollback must say so rather than return.
|
|
destination = tmp_path / "checkpoint"
|
|
seen = {}
|
|
|
|
def another_writer_recreates_staging():
|
|
seen["staging"].mkdir()
|
|
(seen["staging"] / "theirs.txt").write_text("other writer", encoding="utf-8")
|
|
|
|
_fail_after_promotion(monkeypatch, destination, another_writer_recreates_staging)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
seen["staging"] = staging
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: {staging} also exists, so the "
|
|
"promoted checkpoint could not be moved aside without risking another writer's "
|
|
"entry. Nothing was moved or removed; no previous checkpoint existed."
|
|
+ _unverified_staging_note(staging, refused=True)
|
|
)
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (staging / "theirs.txt").read_text(encoding="utf-8") == "other writer"
|
|
|
|
|
|
def test_rollback_refuses_when_the_backup_disappeared_after_promotion(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
elsewhere = tmp_path / "taken-backup"
|
|
taken = {}
|
|
|
|
def another_writer_takes_the_backup():
|
|
taken["backup"] = _only_backup(tmp_path)
|
|
os.rename(taken["backup"], elsewhere)
|
|
|
|
_fail_after_promotion(monkeypatch, destination, another_writer_takes_the_backup)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert str(raised.value) == (
|
|
"Checkpoint rollback could not restore the previous checkpoint: its backup "
|
|
f"{taken['backup']} is missing. {destination} and {staging} were left as they are "
|
|
"(the destination may hold this write's promoted checkpoint); nothing was moved "
|
|
"or removed."
|
|
)
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (elsewhere / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_rollback_wording_when_promotion_was_never_attempted(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
backup = tmp_path / ".checkpoint.backup-missing"
|
|
destination.mkdir()
|
|
staging.mkdir()
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
persistence._rollback_checkpoint_commit(
|
|
destination, staging, None, had_destination=False, promotion_attempted=False,
|
|
)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: {staging} also exists, so the "
|
|
"entry there, which this write never promoted, could not be moved aside without "
|
|
"risking another writer's entry. Nothing was moved or removed; no previous "
|
|
"checkpoint existed."
|
|
)
|
|
persistence._remove_checkpoint_path(destination)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
persistence._rollback_checkpoint_commit(
|
|
destination, staging, backup, had_destination=True, promotion_attempted=False,
|
|
)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback could not restore the previous checkpoint: its backup {backup} "
|
|
f"is missing. {destination} and {staging} were left as they are; nothing was moved "
|
|
"or removed."
|
|
)
|
|
|
|
|
|
def test_failed_rollback_of_a_new_destination_names_no_phantom_checkpoint(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
|
|
def generic_failure(*_args, **_kwargs):
|
|
raise OSError("injected generic rollback failure")
|
|
|
|
monkeypatch.setattr(persistence, "_move_promoted_checkpoint_aside", generic_failure)
|
|
with pytest.raises(RuntimeError) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert str(raised.value) == (
|
|
"Checkpoint promotion and rollback both failed; no previous checkpoint existed; "
|
|
f"inspect {destination}"
|
|
)
|
|
|
|
|
|
def test_held_directory_never_matches_a_non_directory(tmp_path):
|
|
staging = tmp_path / ".checkpoint.staging-owned"
|
|
staging.mkdir()
|
|
handle = persistence._open_directory_handle(staging)
|
|
if handle is None: # pragma: no cover - no descriptor-based removal here
|
|
pytest.skip("descriptor-held cleanup is unavailable on this platform")
|
|
try:
|
|
os.rmdir(staging)
|
|
staging.write_text("other writer", encoding="utf-8")
|
|
assert not persistence._names_held_directory(staging, handle)
|
|
finally:
|
|
os.close(handle)
|
|
|
|
|
|
def _without_a_held_staging_handle(monkeypatch):
|
|
monkeypatch.setattr(persistence, "_open_directory_handle", lambda _path: None)
|
|
|
|
|
|
def test_no_handle_rollback_never_deletes_a_foreign_entry_at_staging(tmp_path, monkeypatch):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
seen = {}
|
|
|
|
def another_writer_takes_the_staging_name():
|
|
seen["staging"].write_text("other writer", encoding="utf-8")
|
|
|
|
_without_a_held_staging_handle(monkeypatch)
|
|
_fail_after_promotion(monkeypatch, destination, another_writer_takes_the_staging_name)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused, match="also exists"):
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
seen["staging"] = staging
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert staging.read_text(encoding="utf-8") == "other writer"
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (_only_backup(tmp_path) / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_no_handle_generic_rollback_failure_keeps_and_names_staging(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_without_a_held_staging_handle(monkeypatch)
|
|
real_replace = persistence.os.replace
|
|
|
|
def fail_promotion(source, target):
|
|
if ".staging-" in Path(source).name:
|
|
raise OSError("injected promotion failure")
|
|
return real_replace(source, target)
|
|
|
|
monkeypatch.setattr(persistence.os, "replace", fail_promotion)
|
|
real_rename = persistence._rename_no_replace
|
|
|
|
def fail_restore(source, target):
|
|
if ".backup-" in Path(source).name:
|
|
raise OSError("injected restore failure")
|
|
return real_rename(source, target)
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", fail_restore)
|
|
with pytest.raises(RuntimeError) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert not isinstance(raised.value, persistence.CheckpointRollbackRefused)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint promotion and rollback both failed; recover the previous checkpoint "
|
|
f"from {backup}; the staging path {staging} was also left in place, unverified"
|
|
)
|
|
# Pathname cleanup never runs after a rollback failure without a held handle.
|
|
assert (staging / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
def test_identity_never_matches_a_non_directory(tmp_path):
|
|
entry = tmp_path / "file"
|
|
entry.write_text("not a checkpoint", encoding="utf-8")
|
|
status = os.lstat(entry)
|
|
assert not persistence._names_promoted_checkpoint(entry, (status.st_dev, status.st_ino))
|
|
|
|
|
|
# ── #649: cancellation, interrupted discovery publishing, fd-held staging cleanup ──
|
|
|
|
|
|
@pytest.mark.parametrize("interrupt", [KeyboardInterrupt, SystemExit])
|
|
def test_cancelled_parent_sync_after_no_replace_publish_stays_a_cancellation(
|
|
tmp_path, monkeypatch, portable_no_replace_rename, interrupt,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
real_sync = persistence._sync_directory
|
|
|
|
def cancel_after_promotion(path):
|
|
if path == destination.parent and (destination / "model.bin").is_file():
|
|
raise interrupt()
|
|
real_sync(path)
|
|
|
|
monkeypatch.setattr(persistence, "_sync_directory", cancel_after_promotion)
|
|
with pytest.raises(interrupt) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination, replace_existing=False) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert raised.value.__notes__ == [
|
|
f"Checkpoint was published to {destination}, but syncing {tmp_path} failed, so the "
|
|
f"rename may not survive a crash. Nothing was rolled back or removed; inspect "
|
|
f"{destination}.",
|
|
]
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
|
|
|
|
def test_exclusive_text_files_name_a_file_interrupted_mid_publish_as_maybe_published(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
real_publish = persistence._publish_file_no_replace
|
|
|
|
def publish_then_interrupt(temporary, destination):
|
|
real_publish(temporary, destination)
|
|
if destination == second:
|
|
raise KeyboardInterrupt()
|
|
|
|
monkeypatch.setattr(persistence, "_publish_file_no_replace", publish_then_interrupt)
|
|
with pytest.raises(KeyboardInterrupt) as raised:
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
assert raised.value.__notes__ == [
|
|
f"already published and left in place (the set may be incomplete): {first}; "
|
|
f"{second} may have been published and was left in place",
|
|
]
|
|
assert second.read_text(encoding="utf-8") == "report\n"
|
|
|
|
|
|
def test_exclusive_text_files_name_a_lone_interrupted_file_as_maybe_published(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
only = tmp_path / "config.json"
|
|
|
|
def interrupted(_temporary, _destination):
|
|
raise KeyboardInterrupt()
|
|
|
|
monkeypatch.setattr(persistence, "_publish_file_no_replace", interrupted)
|
|
with pytest.raises(KeyboardInterrupt) as raised:
|
|
persistence.write_new_text_files({only: "config\n"})
|
|
assert raised.value.__notes__ == [f"{only} may have been published and was left in place"]
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not persistence.shutil.rmtree.avoids_symlink_attacks,
|
|
reason="no descriptor-based directory removal on this platform",
|
|
)
|
|
def test_definite_no_replace_refusal_never_cleans_a_recreated_staging_path(
|
|
tmp_path, monkeypatch, caplog,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
seen = {}
|
|
|
|
def staging_recreated_then_refused(source, target):
|
|
# Another process deletes our staging directory, recreates the same
|
|
# name with its own data, and the rename is then definitely refused.
|
|
persistence.shutil.rmtree(source)
|
|
source.mkdir()
|
|
(source / "theirs.txt").write_text("other writer", encoding="utf-8")
|
|
seen["staging"] = source
|
|
target.mkdir()
|
|
raise FileExistsError(17, "File exists", str(target))
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", staging_recreated_then_refused)
|
|
with pytest.raises(FileExistsError, match="already exists"):
|
|
with persistence.atomic_checkpoint_directory(destination, replace_existing=False) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert (seen["staging"] / "theirs.txt").read_text(encoding="utf-8") == "other writer"
|
|
assert any("no longer names" in message for message in caplog.messages)
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not persistence.shutil.rmtree.avoids_symlink_attacks,
|
|
reason="no descriptor-based directory removal on this platform",
|
|
)
|
|
def test_failed_staging_cleanup_removes_nested_contents_through_the_held_directory(tmp_path):
|
|
destination = tmp_path / "checkpoint"
|
|
outside = tmp_path / "outside"
|
|
outside.mkdir()
|
|
(outside / "sentinel").write_text("preserve", encoding="utf-8")
|
|
with pytest.raises(OSError, match="injected write failure"):
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "nested" / "deeper").mkdir(parents=True)
|
|
(staging / "nested" / "deeper" / "shard.bin").write_bytes(b"partial")
|
|
(staging / "link").symlink_to(outside, target_is_directory=True)
|
|
raise OSError("injected write failure")
|
|
|
|
assert not staging.exists()
|
|
assert (outside / "sentinel").read_text(encoding="utf-8") == "preserve"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["outside"]
|
|
|
|
|
|
# ── #665 / #667: persistence follow-ups ──
|
|
|
|
|
|
def _staging_cannot_be_held_open(monkeypatch):
|
|
"""Make holding the staging directory open fail, as under EMFILE."""
|
|
import errno
|
|
|
|
real_open = persistence.os.open
|
|
|
|
def emfile_on_staging(path, flags, *args, **kwargs):
|
|
if (
|
|
isinstance(path, (str, os.PathLike))
|
|
and ".staging-" in Path(path).name
|
|
and flags & getattr(os, "O_DIRECTORY", 0)
|
|
and flags & getattr(os, "O_NOFOLLOW", 0) # the pin, not a directory fsync
|
|
and "dir_fd" not in kwargs
|
|
):
|
|
raise OSError(errno.EMFILE, "Too many open files")
|
|
return real_open(path, flags, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(persistence.os, "open", emfile_on_staging)
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not persistence.shutil.rmtree.avoids_symlink_attacks,
|
|
reason="no descriptor-based directory removal on this platform",
|
|
)
|
|
def test_failed_staging_that_could_not_be_held_open_is_kept_with_a_warning(
|
|
tmp_path, monkeypatch, caplog,
|
|
):
|
|
# #665: without a held descriptor, pathname removal could delete a
|
|
# directory another process recreated at the staging path, so it is kept.
|
|
destination = tmp_path / "checkpoint"
|
|
_staging_cannot_be_held_open(monkeypatch)
|
|
with caplog.at_level("WARNING", logger=persistence.__name__):
|
|
with pytest.raises(OSError, match="injected write failure"):
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"partial")
|
|
raise OSError("injected write failure")
|
|
|
|
assert (staging / "model.bin").read_bytes() == b"partial"
|
|
assert not destination.exists()
|
|
assert any(
|
|
"could not be held open" in message and "Too many open files" in message
|
|
for message in caplog.messages
|
|
)
|
|
assert any(
|
|
f"staging directory {staging} was left in place" in message
|
|
for message in caplog.messages
|
|
)
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not persistence.shutil.rmtree.avoids_symlink_attacks,
|
|
reason="no descriptor-based directory removal on this platform",
|
|
)
|
|
def test_a_successful_commit_is_unaffected_when_staging_could_not_be_held_open(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_staging_cannot_be_held_open(monkeypatch)
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == [".checkpoint.lock", "checkpoint"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"failure",
|
|
[
|
|
OSError(5, "Input/output error"), # e.g. NFS EIO after the link took effect
|
|
PermissionError(13, "Permission denied"),
|
|
TimeoutError(110, "Connection timed out"),
|
|
],
|
|
ids=["eio", "eacces", "etimedout"],
|
|
)
|
|
def test_exclusive_text_files_name_a_file_whose_publish_failed_uncertainly(
|
|
tmp_path, monkeypatch, failure,
|
|
):
|
|
# #665: only FileExistsError and NoReplaceRenameUnsupported prove that
|
|
# nothing appeared; any other OSError may follow a link or rename that took
|
|
# effect (a remote filesystem can report EIO after success).
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
real_publish = persistence._publish_file_no_replace
|
|
|
|
def fail_on_the_second(temporary, destination):
|
|
if destination == second:
|
|
raise failure
|
|
return real_publish(temporary, destination)
|
|
|
|
monkeypatch.setattr(persistence, "_publish_file_no_replace", fail_on_the_second)
|
|
with pytest.raises(type(failure)) as raised:
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
assert raised.value is failure
|
|
assert raised.value.__notes__ == [
|
|
f"already published and left in place (the set may be incomplete): {first}; "
|
|
f"{second} may have been published and was left in place",
|
|
]
|
|
|
|
|
|
def test_exclusive_text_files_do_not_name_a_definitely_refused_file(tmp_path, monkeypatch):
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
real_publish = persistence._publish_file_no_replace
|
|
|
|
def unsupported_on_the_second(temporary, destination):
|
|
if destination == second:
|
|
raise persistence.NoReplaceRenameUnsupported("no atomic no-replace rename here")
|
|
return real_publish(temporary, destination)
|
|
|
|
monkeypatch.setattr(persistence, "_publish_file_no_replace", unsupported_on_the_second)
|
|
with pytest.raises(persistence.NoReplaceRenameUnsupported) as raised:
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
|
|
assert raised.value.__notes__ == [
|
|
f"already published and left in place (the set may be incomplete): {first}",
|
|
]
|
|
assert not second.exists()
|
|
|
|
|
|
class _NoteRefusingError(OSError):
|
|
"""An error whose ``add_note`` fails the way it can under memory pressure."""
|
|
|
|
def add_note(self, note):
|
|
raise MemoryError("injected allocation failure while adding a note")
|
|
|
|
|
|
def test_exclusive_text_files_never_replace_the_primary_error_when_a_note_fails(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# #665: the publishing note is best effort; a MemoryError while attaching
|
|
# it must not chain over, and so replace, the error that stopped publishing.
|
|
first = tmp_path / "config.json"
|
|
second = tmp_path / "report.json"
|
|
primary = _NoteRefusingError(5, "injected publish failure")
|
|
real_publish = persistence._publish_file_no_replace
|
|
|
|
def fail_on_the_second(temporary, destination):
|
|
if destination == second:
|
|
raise primary
|
|
return real_publish(temporary, destination)
|
|
|
|
monkeypatch.setattr(persistence, "_publish_file_no_replace", fail_on_the_second)
|
|
with pytest.raises(_NoteRefusingError) as raised:
|
|
persistence.write_new_text_files({first: "config\n", second: "report\n"})
|
|
assert raised.value is primary
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not persistence.shutil.rmtree.avoids_symlink_attacks,
|
|
reason="no descriptor-based directory removal on this platform",
|
|
)
|
|
def test_rollback_after_promotion_refuses_when_staging_could_not_be_held_open(
|
|
tmp_path, monkeypatch, portable_no_replace_rename,
|
|
):
|
|
# #665/#667: a pin that failed (EMFILE) leaves no identity, so the promoted
|
|
# checkpoint is never moved aside; it stays, and the backup is kept.
|
|
destination = tmp_path / "checkpoint"
|
|
_prior_checkpoint(destination)
|
|
_staging_cannot_be_held_open(monkeypatch)
|
|
_fail_after_promotion(monkeypatch, destination)
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
with persistence.atomic_checkpoint_directory(destination) as staging:
|
|
(staging / "model.bin").write_bytes(b"ours")
|
|
|
|
backup = _only_backup(tmp_path)
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: it is not provably the checkpoint "
|
|
"this write promoted, so another writer may have replaced it. Nothing was moved or "
|
|
f"removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
assert str(raised.value.__cause__) == "injected parent directory sync failure"
|
|
assert not os.path.lexists(staging) # promoted: it is the destination now
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (backup / "model.bin").read_bytes() == b"prior"
|
|
|
|
|
|
# ── #679: coverage of #674/#682 refusal, identity and cleanup branches ──
|
|
|
|
|
|
_DIGEST = "0" * 64
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("digests", "message"),
|
|
[
|
|
(["preprocessor_config.json"], "digests must be an object"),
|
|
({"../model.safetensors": _DIGEST}, "unsafe filename: '../model.safetensors'"),
|
|
({"preprocessor_config.json": "not-a-sha256"}, "invalid digest: preprocessor_config.json"),
|
|
],
|
|
)
|
|
def test_checkpoint_processor_digests_reject_malformed_manifests(digests, message):
|
|
with pytest.raises(ValueError, match=message):
|
|
persistence.checkpoint_processor_digests({"processor_config_sha256": digests})
|
|
|
|
|
|
def test_checkpoint_processor_digests_accept_a_well_formed_or_absent_manifest():
|
|
manifest = {"preprocessor_config.json": _DIGEST}
|
|
assert persistence.checkpoint_processor_digests({"processor_config_sha256": manifest}) == manifest
|
|
assert persistence.checkpoint_processor_digests({}) == {}
|
|
|
|
|
|
def _link_fails_then_destination_appears(monkeypatch, appear):
|
|
"""``os.link`` reports EIO without linking; ``appear`` runs after the first identity check.
|
|
|
|
The first ``_names_our_link`` runs against an absent destination, so the
|
|
real ``lstat`` fails and the identity cannot be told (``None``). ``appear``
|
|
then makes an entry show up before the fallback rename, which finds it.
|
|
"""
|
|
import errno
|
|
|
|
def link_fails(_source, _destination):
|
|
raise OSError(errno.EIO, "injected I/O error")
|
|
|
|
real_names_our_link = persistence._names_our_link
|
|
calls = []
|
|
|
|
def names_our_link(temporary, destination):
|
|
result = real_names_our_link(temporary, destination)
|
|
calls.append(result)
|
|
if len(calls) == 1:
|
|
appear(temporary, destination)
|
|
return result
|
|
|
|
monkeypatch.setattr(persistence.os, "link", link_fails)
|
|
# The emulation raises FileExistsError for any present entry on every
|
|
# platform (a real Windows rename onto a hard link of the same file succeeds).
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", emulated_rename_no_replace)
|
|
monkeypatch.setattr(persistence, "_names_our_link", names_our_link)
|
|
return calls
|
|
|
|
|
|
def test_exclusive_text_files_count_a_late_visible_link_as_published_after_the_rename_conflict(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# #682: the uncertain link was not visible at the first identity check (a
|
|
# stale cache), so the rename runs and finds the destination present. The
|
|
# second identity check proves it is this write's own link: published, not
|
|
# reported as uncertain or as another writer's output.
|
|
real_link = os.link # captured before the helper replaces it
|
|
calls = _link_fails_then_destination_appears(monkeypatch, real_link)
|
|
first = tmp_path / "config.json"
|
|
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
|
|
assert calls == [None, True]
|
|
assert first.read_text(encoding="utf-8") == "config\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json"]
|
|
|
|
|
|
def test_exclusive_text_files_report_a_writer_racing_an_uncertain_link_as_an_existing_output(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
# #682: another writer created the destination after the uncertain link's
|
|
# identity check. The rename conflict plus a different identity is a
|
|
# definite conflict, and the other writer's file is left untouched.
|
|
def other_writer(_temporary, destination):
|
|
destination.write_text("other writer\n", encoding="utf-8")
|
|
|
|
calls = _link_fails_then_destination_appears(monkeypatch, other_writer)
|
|
first = tmp_path / "config.json"
|
|
|
|
with pytest.raises(FileExistsError) as raised:
|
|
persistence.write_new_text_files({first: "config\n"})
|
|
|
|
assert calls == [None, False]
|
|
assert str(raised.value) == f"Output already exists: {first}"
|
|
assert getattr(raised.value, "__notes__", []) == []
|
|
assert first.read_text(encoding="utf-8") == "other writer\n"
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["config.json"]
|
|
|
|
|
|
def test_rollback_refuses_to_move_aside_a_destination_that_vanished(tmp_path):
|
|
# #674: a destination gone by the identity check is not provably the
|
|
# promoted checkpoint, so nothing is moved and staging is not created.
|
|
destination = tmp_path / "checkpoint"
|
|
staging = tmp_path / ".checkpoint.staging-test"
|
|
backup = tmp_path / ".checkpoint.backup-test"
|
|
state = persistence._RollbackState()
|
|
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
persistence._move_promoted_checkpoint_aside(destination, staging, backup, (1, 2), state)
|
|
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: it is not provably the checkpoint "
|
|
"this write promoted, so another writer may have replaced it. Nothing was moved or "
|
|
f"removed; the previous checkpoint was kept at {backup}."
|
|
)
|
|
assert not state.staging_may_be_foreign
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def test_rollback_refuses_when_staging_reappears_during_the_move_aside(tmp_path, monkeypatch):
|
|
# #674: the identity check passed, but another writer recreated the staging
|
|
# path before the no-replace move. The move is refused, nothing is moved
|
|
# or removed, and staging is not marked as holding the promoted checkpoint.
|
|
destination = tmp_path / "checkpoint"
|
|
destination.mkdir()
|
|
(destination / "model.bin").write_bytes(b"ours")
|
|
staging = tmp_path / ".checkpoint.staging-test"
|
|
identity = persistence._usable_identity(os.stat(destination, follow_symlinks=False))
|
|
assert identity is not None
|
|
state = persistence._RollbackState()
|
|
|
|
def staging_recreated(source, target):
|
|
target.mkdir()
|
|
(target / "theirs.txt").write_text("other writer", encoding="utf-8")
|
|
emulated_rename_no_replace(source, target)
|
|
|
|
monkeypatch.setattr(persistence, "_rename_no_replace", staging_recreated)
|
|
with pytest.raises(persistence.CheckpointRollbackRefused) as raised:
|
|
persistence._move_promoted_checkpoint_aside(destination, staging, None, identity, state)
|
|
|
|
assert str(raised.value) == (
|
|
f"Checkpoint rollback left {destination} in place: {staging} reappeared, so it could "
|
|
"not be moved aside. Nothing was moved or removed; no previous checkpoint existed."
|
|
)
|
|
assert isinstance(raised.value.__cause__, FileExistsError)
|
|
assert not state.staging_may_be_foreign
|
|
assert (destination / "model.bin").read_bytes() == b"ours"
|
|
assert (staging / "theirs.txt").read_text(encoding="utf-8") == "other writer"
|
|
|
|
|
|
def _held_staging(tmp_path):
|
|
staging = tmp_path / ".checkpoint.staging-test"
|
|
(staging / "nested").mkdir(parents=True)
|
|
(staging / "nested" / "shard.bin").write_bytes(b"partial")
|
|
(staging / "model.bin").write_bytes(b"partial")
|
|
return staging, persistence._open_directory_handle(staging)
|
|
|
|
|
|
def _after_listing_held_directory(monkeypatch, handle, action):
|
|
"""Run ``action`` right after the held directory's entries are listed."""
|
|
real_listdir = persistence.os.listdir
|
|
|
|
def listdir(path=None):
|
|
names = real_listdir(path) if path is not None else real_listdir()
|
|
if path == handle:
|
|
action()
|
|
return names
|
|
|
|
monkeypatch.setattr(persistence.os, "listdir", listdir)
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not persistence.shutil.rmtree.avoids_symlink_attacks,
|
|
reason="no descriptor-based directory removal on this platform",
|
|
)
|
|
def test_held_staging_cleanup_tolerates_entries_removed_concurrently(tmp_path, monkeypatch):
|
|
staging, handle = _held_staging(tmp_path)
|
|
try:
|
|
_after_listing_held_directory(
|
|
monkeypatch, handle, lambda: (staging / "model.bin").unlink(),
|
|
)
|
|
persistence._remove_held_directory(staging, handle)
|
|
finally:
|
|
os.close(handle)
|
|
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not persistence.shutil.rmtree.avoids_symlink_attacks,
|
|
reason="no descriptor-based directory removal on this platform",
|
|
)
|
|
def test_held_staging_cleanup_keeps_a_directory_swapped_in_while_removing(tmp_path, monkeypatch):
|
|
# The held staging directory is renamed away and another process puts its
|
|
# own directory at the staging path mid-cleanup. Removal works through the
|
|
# descriptor, never the path, and the final rmdir is skipped because the
|
|
# path no longer names the held directory.
|
|
staging, handle = _held_staging(tmp_path)
|
|
moved = tmp_path / "moved"
|
|
|
|
def swap():
|
|
os.rename(staging, moved)
|
|
staging.mkdir()
|
|
(staging / "theirs.txt").write_text("other writer", encoding="utf-8")
|
|
|
|
try:
|
|
_after_listing_held_directory(monkeypatch, handle, swap)
|
|
persistence._remove_held_directory(staging, handle)
|
|
finally:
|
|
os.close(handle)
|
|
|
|
assert (staging / "theirs.txt").read_text(encoding="utf-8") == "other writer"
|
|
assert list(moved.iterdir()) == []
|