Skip to content

The agent that grows with you

pm: keep locked stale previous entries instead of failing install

janviernine62761ec9913a
diff --git a/pm/install.py b/pm/install.py
index 8ab88c44a33..72280fa8963 100644
--- a/pm/install.py
+++ b/pm/install.py
@@ -250,6 +250,15 @@ def _remove_downloads(store: Store, artifacts: list[dict]) -> None:
_remove_entry(store, f"fetch-{artifact['sha256']}")
+def _discard_previous_entry(store: Store, previous_entry) -> None:
+ # A stale previous entry is only a restore point. A locked file
+ # (mapped DLL, AV hold) must not fail the install that replaced it.
+ try:
+ _remove_entry(store, previous_entry.name)
+ except OSError as e:
+ LOG.warning("keeping %s: %s (will retry on next install; delete manually once unlocked)", previous_entry.name, e)
+
+
def _entry_verified(package: Package, fact: dict, store: Store, target: str) -> bool:
"""Explicit installs re-check realized bytes; startup keeps its cheap facts check."""
entry = store.entry(fact["entry"])
@@ -304,7 +313,7 @@ def _publish_entry(package, store, staged, entry, previous_entry, target):
_restore_previous_entry(store, entry, previous_entry)
raise
if previous_entry.exists():
- _remove_entry(store, previous_entry.name)
+ _discard_previous_entry(store, previous_entry)
def _settle_previous_entry(package, store, entry, previous_entry, previous, target) -> None:
@@ -315,7 +324,7 @@ def _settle_previous_entry(package, store, entry, previous_entry, previous, targ
# an interrupted stage always restores its prior usable bytes.
if (previous and previous.get("entry") == entry.name
and _entry_verified(package, previous, store, target)):
- _remove_entry(store, previous_entry.name)
+ _discard_previous_entry(store, previous_entry)
else:
_restore_previous_entry(store, entry, previous_entry)
diff --git a/tests/pm/test_previous_entry_discard.py b/tests/pm/test_previous_entry_discard.py
new file mode 100644
index 00000000000..4ab936c3d11
--- /dev/null
+++ b/tests/pm/test_previous_entry_discard.py
@@ -0,0 +1,22 @@
+"""Locked stale previous entries must not fail the install that replaced them."""
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import patch
+
+from pm.install import _discard_previous_entry
+
+
+def test_discard_previous_entry_swallows_locked_file():
+ store = SimpleNamespace()
+ previous = SimpleNamespace(name=".previous-python-test")
+ with patch("pm.install._remove_entry", side_effect=PermissionError(5, "Access is denied")):
+ _discard_previous_entry(store, previous)
+
+
+def test_discard_previous_entry_removes_when_unlocked():
+ store = SimpleNamespace()
+ previous = SimpleNamespace(name=".previous-python-test")
+ with patch("pm.install._remove_entry") as remove:
+ _discard_previous_entry(store, previous)
+ remove.assert_called_once_with(store, ".previous-python-test")