Skip to content

Commit 3d5f877

Browse files
committed
create: Keep cleaning up after Ctrl-C while deleting wallet files
Ctrl-C during the folder delete or the macOS RAM disk detach escaped before the other step ran and before the user was told what was left. Both steps now catch it like the RPC steps do, so cleanup finishes and anything still on disk is named in the error that stops creation. Claude-Session: https://claude.ai/code/session_01T233rKgZqE5wzDm3EVTHL1
1 parent 0d5df21 commit 3d5f877

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎src/codex32/_bitcoin_core.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,17 @@ def _close(self) -> None:
557557
pass
558558
except KeyboardInterrupt:
559559
interrupted = True
560-
if self._folder:
561-
shutil.rmtree(self._folder, ignore_errors=True)
562-
detach = ("/usr/bin/hdiutil", "detach", "-force", self._disk)
563-
if self._disk and not subprocess.run(detach, capture_output=True, check=False).returncode:
564-
self._disk = ""
560+
try:
561+
if self._folder:
562+
shutil.rmtree(self._folder, ignore_errors=True)
563+
except KeyboardInterrupt:
564+
interrupted = True
565+
try:
566+
detach = ("/usr/bin/hdiutil", "detach", "-force", self._disk)
567+
if self._disk and not subprocess.run(detach, capture_output=True, check=False).returncode:
568+
self._disk = ""
569+
except KeyboardInterrupt:
570+
interrupted = True
565571
if not os.path.exists(self._folder):
566572
self._folder = ""
567573
left = self._folder or self._disk

‎tests/test_bitcoin_core.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,40 @@ def test_core_entropy_stops_creation_when_a_draws_wallet_stays(
982982
assert told == ([left] if "addhdkey" in fail else []) and (left in str(stopped.value)) == (not told)
983983

984984

985+
@pytest.mark.parametrize("step", ("rmtree", "detach"))
986+
def test_core_entropy_names_what_ctrl_c_left_while_deleting_files(
987+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, step: str
988+
) -> None:
989+
rpc, disk, detached = _EntropyRPC(), "/dev/disk9", list[str]()
990+
991+
def ram_folder(self: CoreEntropy) -> str:
992+
self._disk = disk
993+
return tempfile.mkdtemp(dir=tmp_path)
994+
995+
def run(command: tuple[str, ...], **_options: object) -> subprocess.CompletedProcess[bytes]:
996+
if step == "detach":
997+
raise KeyboardInterrupt
998+
detached.append(command[-1])
999+
return subprocess.CompletedProcess(command, 0)
1000+
1001+
def interrupted(*_arguments: object, **_options: object) -> None:
1002+
raise KeyboardInterrupt
1003+
1004+
monkeypatch.setattr(BitcoinCore, "_rpc", rpc)
1005+
monkeypatch.setattr(CoreEntropy, "_ram_folder", ram_folder)
1006+
monkeypatch.setattr("codex32._bitcoin_core.subprocess.run", run)
1007+
if step == "rmtree":
1008+
monkeypatch.setattr(shutil, "rmtree", interrupted)
1009+
entropy = CoreEntropy(BitcoinCore("bitcoin-cli", "main", 320000), lambda _message: None)
1010+
1011+
with pytest.raises(BitcoinCoreError, match="creation stopped") as stopped:
1012+
entropy(1)
1013+
# Ctrl-C while deleting files still runs the next step, then says what to remove by hand.
1014+
left = os.path.dirname(rpc.calls[0][0][2].removeprefix("wallet_name=")) if step == "rmtree" else disk
1015+
assert f"was not removed. Delete or eject {left}." in str(stopped.value)
1016+
assert detached == ([disk] if step == "rmtree" else []) and not rpc.loaded
1017+
1018+
9851019
@pytest.mark.parametrize("lost", ("createwallet", "unloadwallet"))
9861020
def test_core_entropy_removes_a_wallet_whose_call_never_answered_then_stops(
9871021
monkeypatch: pytest.MonkeyPatch, lost: str

0 commit comments

Comments
 (0)