Skip to content

Commit 539e08f

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 bea2914 commit 539e08f

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
@@ -554,11 +554,17 @@ def _close(self) -> None:
554554
pass
555555
except KeyboardInterrupt:
556556
interrupted = True
557-
if self._folder:
558-
shutil.rmtree(self._folder, ignore_errors=True)
559-
detach = ("/usr/bin/hdiutil", "detach", "-force", self._disk)
560-
if self._disk and not subprocess.run(detach, capture_output=True, check=False).returncode:
561-
self._disk = ""
557+
try:
558+
if self._folder:
559+
shutil.rmtree(self._folder, ignore_errors=True)
560+
except KeyboardInterrupt:
561+
interrupted = True
562+
try:
563+
detach = ("/usr/bin/hdiutil", "detach", "-force", self._disk)
564+
if self._disk and not subprocess.run(detach, capture_output=True, check=False).returncode:
565+
self._disk = ""
566+
except KeyboardInterrupt:
567+
interrupted = True
562568
if not os.path.exists(self._folder):
563569
self._folder = ""
564570
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
@@ -975,6 +975,40 @@ def test_core_entropy_stops_creation_when_a_draws_wallet_stays(
975975
assert told == ([left] if "addhdkey" in fail else []) and (left in str(stopped.value)) == (not told)
976976

977977

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

0 commit comments

Comments
 (0)