Skip to content

The agent that grows with you

test(backup): the SIGTERM cleanup test signals a copy it holds open

Teknium8b8f99060545
diff --git a/hermes_cli/backup_sqlite.py b/hermes_cli/backup_sqlite.py
index cf4dcb75fab..587abde6eac 100644
--- a/hermes_cli/backup_sqlite.py
+++ b/hermes_cli/backup_sqlite.py
@@ -9,7 +9,6 @@ import os
import signal
import sqlite3
import sys
-import tempfile
import time
from contextlib import suppress
from datetime import datetime, timezone, UTC
@@ -190,9 +189,9 @@ def preflight_state_db(home: Path) -> dict:
_reclaim_abandoned_staging(home, prefix)
stamp = datetime.now(UTC).strftime("%Y-%m-%dT%H-%M-%S-%fZ")
destination = home / f"{prefix}{stamp}-{os.getpid()}.bak"
- fd, name = tempfile.mkstemp(prefix=prefix, suffix=".partial", dir=home)
- os.close(fd)
- staged = Path(name)
+ # Named before it exists, so a SIGTERM at any point after creation reaches the
+ # cleanup below; _safe_copy_db creates it owner-only and refuses a symlink.
+ staged = home / f"{prefix}{stamp}-{os.getpid()}.partial"
try:
if not _safe_copy_db(source, staged):
raise RuntimeError("SQLite safe copy failed; previous emergency snapshots were retained")
diff --git a/tests/hermes_cli/test_backup_preflight_staging.py b/tests/hermes_cli/test_backup_preflight_staging.py
index c94808c26d5..8deb1897a3a 100644
--- a/tests/hermes_cli/test_backup_preflight_staging.py
+++ b/tests/hermes_cli/test_backup_preflight_staging.py
@@ -14,14 +14,16 @@ from pathlib import Path
import pytest
+from tests.waiting import wait_until
+
SCRIPT = Path(__file__).resolve().parents[2] / "hermes_cli" / "backup_sqlite.py"
-def _home_with_db(tmp_path: Path, rows: int) -> Path:
+def _home_with_db(tmp_path: Path, rows: int, journal_mode: str = "WAL") -> Path:
home = tmp_path / "home"
home.mkdir()
with sqlite3.connect(home / "state.db") as db:
- db.execute("PRAGMA journal_mode=WAL")
+ db.execute(f"PRAGMA journal_mode={journal_mode}")
db.execute("CREATE TABLE messages (body TEXT)")
db.executemany("INSERT INTO messages VALUES (?)", [("x" * 4000,)] * rows)
return home
@@ -33,15 +35,30 @@ def _staging(home: Path) -> list[str]:
@pytest.mark.platforms("posix")
def test_sigterm_mid_copy_removes_the_staging_copy(tmp_path):
- home = _home_with_db(tmp_path, 60000)
- proc = subprocess.Popen([sys.executable, "-I", "-S", str(SCRIPT), str(home)],
- stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- deadline = time.monotonic() + 30
- while not _staging(home) and proc.poll() is None and time.monotonic() < deadline:
- time.sleep(0.005)
- proc.send_signal(signal.SIGTERM)
- proc.communicate(timeout=60)
+ home = _home_with_db(tmp_path, 10, journal_mode="DELETE")
+ # The barrier: an exclusive lock on a rollback-journal source keeps the copy
+ # busy-waiting until the child is signalled, however fast the disk is.
+ holder = sqlite3.connect(home / "state.db", isolation_level=None)
+ holder.execute("BEGIN EXCLUSIVE")
+ try:
+ proc = subprocess.Popen([sys.executable, "-I", "-S", str(SCRIPT), str(home)],
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ try:
+ wait_until(lambda: _staging(home) or proc.poll() is not None, timeout=30, what="the staging copy")
+ assert proc.poll() is None and _staging(home), "the pre-flight must still be copying"
+ proc.send_signal(signal.SIGTERM)
+ stdout, stderr = proc.communicate(timeout=60)
+ finally:
+ if proc.poll() is None:
+ proc.kill()
+ proc.wait()
+ finally:
+ holder.execute("ROLLBACK")
+ holder.close()
+ # 128 + SIGTERM is the handler's exit; a finished or failed copy exits 0 or 1.
+ assert proc.returncode == 128 + signal.SIGTERM, stderr
+ assert stdout == b""
assert _staging(home) == []