Skip to content

The agent that grows with you

fix(pm): keep identical held tool entries live

KoNit-Kaaffae6b169b
diff --git a/pm/install.py b/pm/install.py
index d18af96b341..c418aa491e5 100644
--- a/pm/install.py
+++ b/pm/install.py
@@ -322,10 +322,24 @@ def _restore_previous_entry(store: Store, entry, previous) -> None:
@contextmanager
def _publish_entry(package, store, staged, entry, previous_entry, target):
"""Keep rollback live through the caller's native facts commit, if any."""
+ kept_live = False
if entry.exists() or entry.is_symlink():
- retry_held(lambda: entry.rename(previous_entry))
+ try:
+ retry_held(lambda: entry.rename(previous_entry))
+ except OSError as rename_error:
+ try:
+ matches_staged = (not package.verify(entry, target)
+ and tree_digest(entry) == tree_digest(staged))
+ except OSError:
+ raise rename_error
+ if not matches_staged:
+ raise
+ LOG.warning("%s entry is in use but already matches the staged bytes; keeping it live",
+ package.name)
+ kept_live = True
try:
- store.publish(staged, entry.name)
+ if not kept_live:
+ store.publish(staged, entry.name)
reason = package.verify(entry, target)
if reason:
raise InstallError(package.name, f"published entry failed verification: {reason}")
diff --git a/tests/pm/test_publish_entry.py b/tests/pm/test_publish_entry.py
new file mode 100644
index 00000000000..1a9f005599e
--- /dev/null
+++ b/tests/pm/test_publish_entry.py
@@ -0,0 +1,62 @@
+"""Publication behavior when a live Windows process holds an existing entry."""
+
+from pathlib import Path
+
+import pytest
+
+from pm import install
+from pm.store import Store
+
+
+class _VerifiedPackage:
+ name = "tool"
+
+ def verify(self, entry, target):
+ return None if (entry / "bin/tool").is_file() else "tool is missing"
+
+
+def _write_entry(path: Path, contents: bytes = b"tool") -> None:
+ (path / "bin").mkdir(parents=True)
+ (path / "bin/tool").write_bytes(contents)
+
+
+def _hold_entry_rename(monkeypatch, entry, previous) -> None:
+ real_rename = Path.rename
+
+ def held_entry_rename(self, destination):
+ if self == entry and destination == previous:
+ raise PermissionError("live entry is held")
+ return real_rename(self, destination)
+
+ monkeypatch.setattr(install, "retry_held", lambda operation: operation())
+ monkeypatch.setattr(Path, "rename", held_entry_rename)
+
+
+def test_publish_keeps_a_byte_identical_held_entry(tmp_path, monkeypatch):
+ """Regression for #139088: an unchanged live entry need not be republished."""
+ store = Store(tmp_path / "store")
+ entry, previous, staged = (store.entry("tool-1"), store.entry(".previous-tool-1"),
+ tmp_path / "staged")
+ _write_entry(entry)
+ _write_entry(staged)
+ _hold_entry_rename(monkeypatch, entry, previous)
+
+ with install._publish_entry(_VerifiedPackage(), store, staged, entry, previous, "target"):
+ pass
+
+ assert (entry / "bin/tool").read_bytes() == b"tool"
+ assert not previous.exists()
+
+
+def test_publish_raises_when_a_held_entry_differs_from_staged_bytes(tmp_path, monkeypatch):
+ """A held entry remains an error unless it is exactly the staged replacement."""
+ store = Store(tmp_path / "store")
+ entry, previous, staged = (store.entry("tool-1"), store.entry(".previous-tool-1"),
+ tmp_path / "staged")
+ _write_entry(entry)
+ _write_entry(staged, b"replacement")
+ _hold_entry_rename(monkeypatch, entry, previous)
+
+ with pytest.raises(PermissionError, match="live entry is held"):
+ with install._publish_entry(_VerifiedPackage(), store, staged, entry, previous, "target"):
+ pass