From 5d277d00652dc912d3801b386589c8e0696f6910 Mon Sep 17 00:00:00 2001 From: argszero Date: Sun, 15 Nov 2026 18:37:16 +0800 Subject: [PATCH] emrg: the input-history save retries a rename a second handle refuses (#2900) --- emrg/session.py | 73 ++++++++++++++++++++++++++++++++++++++++++- tests/test_session.py | 64 +++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) diff --git a/emrg/session.py b/emrg/session.py index 52dbf5a5a..427ff27b5 100644 --- a/emrg/session.py +++ b/emrg/session.py @@ -19,6 +19,7 @@ import ntpath import os import secrets +import time from datetime import datetime from pathlib import Path from typing import Iterable @@ -43,6 +44,68 @@ #: cheap read on every append. _INPUT_HISTORY_MAX = 200 +# ── input history · a rename a second handle can refuse (issue #2900) ───────── +#: How long the rename of a freshly written input history keeps being retried before the +#: save counts as failed. ``os.replace`` opens nothing and is atomic, but on Windows it +#: also raises ``PermissionError`` (``WinError 5``) whenever another handle is open on the +#: destination **at that instant** — a second handle in this process, an indexer, a +#: scanner, a reader that has the file open for a moment. Nothing in the caller prevents +#: that, and treating it as fatal loses the write: the new `.tmp` is left on disk, the old +#: file stays, so the cap is not applied and the next read returns the previous content +#: with nothing reporting that the save did not happen. Measured on Windows Server 2022 +#: against an unpatched tree: 11 failures in 14 samples of one test, the same command on +#: the same tree answering differently between runs. +#: +#: The window is short on purpose. ``daemon.py`` calls the save from the message handler, +#: on the event loop every other session shares, so this number is also the longest a +#: stuck rename may stall them. +_INPUT_HISTORY_REPLACE_BUDGET_SECONDS = 0.5 + +#: The first pause between attempts, doubled after each failure, so a holder that lets go +#: within milliseconds is not waited out at the full step. +_INPUT_HISTORY_REPLACE_FIRST_DELAY_SECONDS = 0.02 + + +def _replace_input_history(tmp: Path, path: Path, budget: float | None = None) -> int: + """Rename the freshly written ``tmp`` over ``path``, retrying a transient holder. + + Returns **the number of retried attempts**, and zero means the first one landed. The + caller reports a non-zero answer rather than discarding it: a save that needed a retry + is a save that would have been lost without one, and a silent retry would turn this + defect into a hidden one. + + The bound is explicit — ``budget`` seconds, not "until it works" — and computed from + ``time.monotonic`` so a clock change cannot stretch it. When it is exhausted the + ``PermissionError`` is raised with the destination path and the operation named, which + is the part of the message this code controls; the platform's own wording rides along + as ``__cause__`` and is never asserted on, because it differs by locale. + + This lives here rather than beside ``emrg/server/atomic.py``'s writers because of the + import direction: this module imports nothing from ``emrg.server``, and the server layer + is what imports *it*. The same failure mode reaches ``sessions_index._write`` and the + YAML writers that share ``atomic.py``; retrying those is a separate change, and this one + deliberately does not touch a writer whose promise is "never raises". + """ + if budget is None: + budget = _INPUT_HISTORY_REPLACE_BUDGET_SECONDS + deadline = time.monotonic() + budget + delay = _INPUT_HISTORY_REPLACE_FIRST_DELAY_SECONDS + attempts = 0 + while True: + try: + os.replace(tmp, path) + except PermissionError as exc: + attempts += 1 + if budget <= 0 or time.monotonic() >= deadline: + raise PermissionError( + f"could not replace {path} with {tmp.name}: another handle on the " + f"destination did not let go within {budget:g}s ({attempts} attempt(s))" + ) from exc + time.sleep(delay) + delay *= 2 + continue + return attempts + def generate_session_id(cwd: Path) -> str: """Generate a human-friendly session ID: s_YYMMDD_HHMM_xxxxxxxx. @@ -1031,7 +1094,15 @@ def append_input_history(self, text: str) -> None: with open(tmp, "w", encoding="utf-8") as f: for record in records: f.write(json.dumps(record, ensure_ascii=False) + "\n") - os.replace(tmp, path) + retries = _replace_input_history(tmp, path) + if retries: + logger.warning( + "input history %s landed after %d retried rename(s): the destination was " + "held open, and this save would have been lost without the retry " + "(issue #2900)", + path, + retries, + ) def read_input_history(self) -> list[dict]: """The recorded prompts, oldest first — the order recall walks backwards through.""" diff --git a/tests/test_session.py b/tests/test_session.py index 0a4f78be4..4cf59eeb4 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -2,6 +2,7 @@ import ast import json +import logging import os import re import warnings @@ -1507,6 +1508,69 @@ def test_a_fifo_input_history_does_not_wedge_the_write(self, tmp_path): with pytest.raises(NotARegularFile): within(DEADLINE_SECONDS, lambda: session.append_input_history("hi")) + def test_a_transient_holder_on_the_destination_does_not_lose_the_save( + self, tmp_path, monkeypatch, caplog + ): + """Windows refuses the rename while anything holds the destination open (#2900). + + `os.replace` is atomic and opens nothing, which is why this save uses it — but on + Windows it also raises `WinError 5` when a second handle is open on the destination + at that instant, and preventing that is not in the caller's hands. Retried within a + bound, the write lands; the retry is **reported**, because a silent one would hide + the day the bound stops being enough. + + Nothing here runs a real Windows failure: the first rename is made to fail in + process, so the arm measures this code rather than the host it happens to run on. + """ + session = self._session(tmp_path) + real_replace = os.replace + attempts = [] + + def held_once_then_released(src, dst, *args, **kwargs): + attempts.append((str(src), str(dst))) + if len(attempts) == 1: + raise PermissionError(13, "The process cannot access the file", str(dst)) + return real_replace(src, dst, *args, **kwargs) + + monkeypatch.setattr(os, "replace", held_once_then_released) + with caplog.at_level(logging.WARNING, logger="emrg.session"): + session.append_input_history("the prompt that would have been lost") + + assert len(attempts) == 2, "the rename was tried again rather than abandoned" + assert [r["text"] for r in session.read_input_history()] == [ + "the prompt that would have been lost" + ], "the save landed" + assert "retried rename" in caplog.text, "a retry is reported, not silent" + + def test_a_holder_that_never_lets_go_fails_on_the_rename_not_on_the_content( + self, tmp_path, monkeypatch + ): + """The bound is real: with the window at zero the same call fails, on the rename. + + Asserting on the error is what keeps this a reverse arm — a test that only read the + file back would pass for a save that failed for some other reason. What a read then + returns is still the previous content, which is the lost write the issue reports: the + cap was not applied and nothing said so. + """ + from emrg import session as session_module + + session = self._session(tmp_path) + session.append_input_history("what was there before") + + def never(src, dst, *args, **kwargs): + raise PermissionError(13, "The process cannot access the file", str(dst)) + + monkeypatch.setattr(session_module, "_INPUT_HISTORY_REPLACE_BUDGET_SECONDS", 0.0) + monkeypatch.setattr(os, "replace", never) + + with pytest.raises(PermissionError) as caught: + session.append_input_history("never lands") + + message = str(caught.value) + assert str(session._input_history_path()) in message, "the error names the destination" + assert "could not replace" in message, "and the operation that failed" + assert [r["text"] for r in session.read_input_history()] == ["what was there before"] + def _session_dir(self, tmp_path, name="s1"): return tmp_path / ".emrg" / "sessions" / name