Skip to content

Commit 4461551

Browse files
committed
fix: take back the published name when the post-link cleanup or sync fails
A failed unlink of the temporary name or fsync of the directory after the final link now removes our own link again before the error propagates, so a failed commit never leaves a published file the caller does not know about.
1 parent 1cf1f96 commit 4461551

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/minideblib/mcp/safeio.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from __future__ import annotations
1717

18+
import contextlib
1819
import errno
1920
import hashlib
2021
import io
@@ -566,9 +567,19 @@ def commit(self, final_name: str | None = None) -> str:
566567
# best effort: whatever is under the final name now is not ours, so it is
567568
# left alone (it may belong to another writer); abort() removes our temp link
568569
raise WriteRootError(errno.EEXIST, f"{shown}: replaced while publishing")
570+
try:
571+
os.unlink(self.temp_name, dir_fd=dir_fd)
572+
os.fsync(dir_fd)
573+
except OSError:
574+
# not durably published: take our own link back so a failed commit
575+
# never leaves a published file the caller does not know about
576+
with contextlib.suppress(OSError):
577+
if _identity(os.stat(name, dir_fd=dir_fd, follow_symlinks=False)) == (
578+
self._ident
579+
):
580+
os.unlink(name, dir_fd=dir_fd)
581+
raise
569582
self.committed = True
570-
os.unlink(self.temp_name, dir_fd=dir_fd)
571-
os.fsync(dir_fd)
572583
except BaseException:
573584
if not self.committed:
574585
self.abort()

‎tests/test_mcp_safeio.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,39 @@ def fake_stat(path: str, *a: Any, **kw: Any) -> os.stat_result:
760760
assert (tmp_path / "a.deb").read_bytes() == b"theirs"
761761

762762

763+
@pytest.mark.parametrize("failing", ["unlink", "fsync"])
764+
def test_post_link_failure_leaves_nothing_published(
765+
root: WriteRoot, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, failing: str
766+
) -> None:
767+
nf = root.create_file("a.deb")
768+
nf.file.write(b"x")
769+
temp = nf.temp_name
770+
real_unlink, real_fsync = os.unlink, os.fsync
771+
fsyncs: list[int] = []
772+
failed: list[bool] = []
773+
774+
def fake_unlink(path: str, *a: Any, **kw: Any) -> None:
775+
if failing == "unlink" and path == temp and not failed:
776+
failed.append(True) # fail only the first unlink of the temporary name
777+
raise OSError(errno.EIO, "boom")
778+
real_unlink(path, *a, **kw)
779+
780+
def fake_fsync(fd: int) -> None:
781+
fsyncs.append(fd)
782+
# the first fsync is the file's, the second the directory's
783+
if failing == "fsync" and len(fsyncs) == 2:
784+
raise OSError(errno.EIO, "boom")
785+
real_fsync(fd)
786+
787+
monkeypatch.setattr(safeio.os, "unlink", fake_unlink)
788+
monkeypatch.setattr(safeio.os, "fsync", fake_fsync)
789+
with pytest.raises(OSError, match="boom"):
790+
nf.commit()
791+
monkeypatch.undo()
792+
assert not (tmp_path / "a.deb").exists()
793+
assert list(tmp_path.iterdir()) == []
794+
795+
763796
def test_commit_invalid_name_aborts(root: WriteRoot, tmp_path: Path) -> None:
764797
nf = root.create_file("a.deb")
765798
nf.file.write(b"x")

0 commit comments

Comments
 (0)