Repository navigation
fix(state): make malformed-DB backup copies atomic with the live-connection registry #71982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stalker023reg
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
stalker023reg:fix/raw-copy-offline-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| """The raw-copy quarantine/backup paths hold the connection-lifecycle lock. | ||
|
|
||
| ``offline_file_access`` (hermes_cli.sqlite_safe_read) exists because checking | ||
| ``has_live_connection()`` and *then* doing raw file I/O is a check/use race: a | ||
| connection opened in the window between the two has its POSIX advisory locks | ||
| cancelled by the raw ``close()`` -- the exact corruption route the registry | ||
| guards against. The snapshot path in ``session_recovery`` was converted to the | ||
| context manager when it landed; these tests pin the other two raw-copy sites | ||
| to the same contract: | ||
|
|
||
| * ``hermes_state._backup_db_file`` (malformed-DB backup before schema surgery) | ||
| * ``hermes_cli.kanban_db._backup_corrupt_db`` (corrupt-board quarantine) | ||
|
|
||
| Each site gets the same pair: a live connection means refusal, and a | ||
| connection attempted MID-COPY blocks on the lifecycle lock until the copy is | ||
| done rather than slipping into the gap. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import shutil | ||
| import sqlite3 | ||
| import threading | ||
|
|
||
| import pytest | ||
|
|
||
| import hermes_state | ||
| from hermes_cli import kanban_db | ||
| from hermes_cli import sqlite_safe_read | ||
| from hermes_cli.sqlite_safe_read import connect_tracked | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _clean_registry(): | ||
| """Each test starts and ends with an empty live-connection registry. | ||
|
|
||
| The registry is process-global; a connection leaked by one test would make | ||
| ``offline_file_access`` in the next raise ``LiveConnectionError`` and | ||
| silently skip the copy under test. | ||
| """ | ||
| with sqlite_safe_read._live_lock: | ||
| sqlite_safe_read._live_connections.clear() | ||
| yield | ||
| with sqlite_safe_read._live_lock: | ||
| sqlite_safe_read._live_connections.clear() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def db_file(tmp_path): | ||
| path = tmp_path / "state.db" | ||
| conn = sqlite3.connect(str(path)) | ||
| conn.execute("CREATE TABLE t (x)") | ||
| conn.commit() | ||
| conn.close() | ||
| (tmp_path / "state.db-wal").write_bytes(b"wal") | ||
| return path | ||
|
|
||
|
|
||
| def _connect_attempt_during(monkeypatch, db_path): | ||
| """Patch ``shutil.copy2`` so a tracked connect races the first copy. | ||
|
|
||
| The verdict is taken INSIDE the patched copy, while the raw I/O is still | ||
| in flight -- once the site under test returns, its guard has released the | ||
| lifecycle lock and the queued connect is free to land, so a check after | ||
| the return would race the very thing it measures. | ||
|
|
||
| Returns (thread, holder, verdict): ``verdict["landed_during_copy"]`` says | ||
| whether the connect got through mid-copy; ``holder`` collects the | ||
| connection so the test can close it. | ||
| """ | ||
| real_copy2 = shutil.copy2 | ||
| started = threading.Event() | ||
| holder: list[sqlite3.Connection] = [] | ||
| verdict: dict = {"landed_during_copy": None} | ||
|
|
||
| def _racing_connect(): | ||
| started.set() | ||
| conn = connect_tracked(str(db_path), check_same_thread=False) | ||
| holder.append(conn) | ||
|
|
||
| thread = threading.Thread(target=_racing_connect, daemon=True) | ||
| fired = threading.Event() | ||
|
|
||
| def _copy2_with_race(src, dst, *a, **kw): | ||
| if not fired.is_set(): | ||
| fired.set() | ||
| thread.start() | ||
| started.wait(timeout=5) | ||
| # Give the connector every chance to slip in if nothing blocks it. | ||
| thread.join(timeout=0.5) | ||
| verdict["landed_during_copy"] = bool(holder) | ||
| return real_copy2(src, dst, *a, **kw) | ||
|
|
||
| # Both call sites bind the stdlib module object (kanban_db at module | ||
| # level, hermes_state via a function-local ``import shutil``), so one | ||
| # patch on the module covers both. | ||
| monkeypatch.setattr(shutil, "copy2", _copy2_with_race) | ||
| return thread, holder, verdict | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # hermes_state._backup_db_file | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_backup_db_file_refuses_with_live_connection(db_file): | ||
| conn = connect_tracked(str(db_file)) | ||
| try: | ||
| assert hermes_state._backup_db_file(db_file) is None | ||
| assert not list(db_file.parent.glob("*.malformed-backup-*")) | ||
| finally: | ||
| conn.close() | ||
|
|
||
|
|
||
| def test_backup_db_file_copy_is_atomic_with_the_registry(db_file, monkeypatch): | ||
| """A connect attempted mid-copy must wait, not land in the gap.""" | ||
| thread, holder, verdict = _connect_attempt_during(monkeypatch, db_file) | ||
|
|
||
| result = hermes_state._backup_db_file(db_file) | ||
|
|
||
| assert result is not None and result.exists() | ||
| assert verdict["landed_during_copy"] is False, ( | ||
| "a connection was opened while the raw copy was in flight -- its " | ||
| "POSIX locks would be cancelled by the copy's close()" | ||
| ) | ||
| thread.join(timeout=5) | ||
| assert holder, "the queued connect must succeed once the copy is done" | ||
| holder[0].close() | ||
|
|
||
|
|
||
| def test_backup_db_file_still_copies_without_the_registry(db_file, monkeypatch): | ||
| """Constrained embeds without hermes_cli keep the best-effort copy.""" | ||
| import builtins | ||
|
|
||
| real_import = builtins.__import__ | ||
|
|
||
| def _no_safe_read(name, *args, **kwargs): | ||
| if name == "hermes_cli.sqlite_safe_read": | ||
| raise ImportError("embed path") | ||
| return real_import(name, *args, **kwargs) | ||
|
|
||
| monkeypatch.setattr(builtins, "__import__", _no_safe_read) | ||
| result = hermes_state._backup_db_file(db_file) | ||
| assert result is not None and result.exists() | ||
| assert result.with_name(result.name + "-wal").exists() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # hermes_cli.kanban_db._backup_corrupt_db | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_backup_corrupt_db_refuses_with_live_connection(db_file): | ||
| conn = connect_tracked(str(db_file)) | ||
| try: | ||
| assert kanban_db._backup_corrupt_db(db_file) is None | ||
| assert not list(db_file.parent.glob("*.corrupt.*.bak")) | ||
| finally: | ||
| conn.close() | ||
|
|
||
|
|
||
| def test_backup_corrupt_db_copy_is_atomic_with_the_registry(db_file, monkeypatch): | ||
| thread, holder, verdict = _connect_attempt_during(monkeypatch, db_file) | ||
|
|
||
| result = kanban_db._backup_corrupt_db(db_file) | ||
|
|
||
| assert result is not None and result.exists() | ||
| assert verdict["landed_during_copy"] is False, ( | ||
| "a connection was opened while the quarantine fingerprint/copy was " | ||
| "in flight -- its POSIX locks would be cancelled by our close()" | ||
| ) | ||
| thread.join(timeout=5) | ||
| assert holder, "the queued connect must succeed once the quarantine is done" | ||
| holder[0].close() | ||
|
|
||
|
|
||
| def test_backup_corrupt_db_still_copies_sidecars(db_file): | ||
| result = kanban_db._backup_corrupt_db(db_file) | ||
| assert result is not None and result.exists() | ||
| assert result.with_name(result.name + "-wal").exists() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startedis set beforeconnect_tracked, and this fixed 0.5-second join can expire simply because the connector was not scheduled. The resultingFalseis then treated as proof of locking, so an unguarded implementation can pass. Please use a park/release event protocol liketests/hermes_cli/test_session_recovery.py:305-389to prove the connector attempted to open while the copy is held.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point. The 0.5-second join is inherently a timing dependency - a slow CI host could let the connector miss the window entirely and still return
False, giving a false pass on an unguarded implementation.I'll rework the fixture to use a park/release event pair: block inside
_copy2_with_raceuntil the connector has had a chance to actually call intoconnect_tracked, then measure the verdict from within the lock's scope rather than from a wall-clock join. That way the probe either lands and the verdict isTrue, or it can't land (blocked by the guard) and staysFalse- no scheduling dependency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Replaced
_connect_attempt_duringwith a park/release event protocol matching the pattern intests/hermes_cli/test_session_recovery.py:inside_copy/release_copypark the copy thread inside the patchedcopy2until explicitly released.connect_attemptedis set immediately beforeconnect_trackedis called, so a "still blocked" assertion can't pass just because the connector thread hadn't been scheduled yet.connection_openedis set inside the connector once the connection actually lands.The assertion sequence in both atomic tests is now:
inside_copy(copy is parked, holds the lock).connect_attempted(connector is at the lock boundary).connection_openedhas NOT fired within 1 s (lock ordering, not scheduling).connection_openedfires within 30 s (lock released, connect succeeds).Committed as
test(state): replace timing-based join with park/release event protocol.