fix(orchestrator): Ctrl+C hangs and threading shutdown traceback

Python ThreadPoolExecutor workers cannot be killed when blocked in HTTP
I/O. Previous approach of waiting for graceful cancellation via
shutdown(wait=True) caused infinite hangs, and re-raising KeyboardInterrupt
still triggered Python's _python_exit thread-join cleanup on exit.

Replace with hard-exit strategy: first Ctrl+C marks all unfinished books
(active + pending) as cancelled, prints summary, returns partial result
with interrupted=True. CLI then calls os._exit(130) to bypass thread
cleanup entirely. Second Ctrl+C hard-exits immediately.

Also fixes: books_with_index[idx][0] returning int index instead of
Book object (AttributeError on summary), empty CancelledError reason
strings, missing cancel_event wiring through download_book->download_m4b
call chain, and corrupted test body from earlier edit collision.

256 tests pass.
This commit is contained in:
AlexKucera committed 2026-06-04 13:10:10 +02:00
1 parent b7cae793df
commit 3dcae24d34
12 files changed
+835 -116

No files matched your search

+17 -2
View File
@@ -1,6 +1,8 @@
"""CLI entry point — wire config → auth → fetch library → download → history."""
import os
import sys
import threading
from pathlib import Path
from rich.console import Console
@@ -139,6 +141,7 @@ def run(
# 5. Download loop with TTY-aware reporting
reporter = DownloadReporter()
cancel_event = threading.Event()
console.print(f"\n[bold]{len(new_books)} book(s) to download:[/bold]\n")
# --- Download loop (parallel via orchestrator — Issue #16) ---
@@ -154,6 +157,7 @@ def run(
format_strategy=config.format,
config=config,
progress=reporter.update,
cancel_event=cancel_event,
)
return _download_fn
@@ -162,6 +166,7 @@ def run(
workers=resolved_workers,
download_fn=_make_download_fn(),
reporter=reporter,
cancel_event=cancel_event,
)
downloaded = result.downloaded_count
@@ -178,6 +183,9 @@ def run(
skipped_books=skipped_books,
)
if result.interrupted:
return 130
if verbose:
console.print("[dim]── done ────────────────────────────────────────[/dim]")
@@ -214,14 +222,21 @@ def main() -> None:
)
args = parser.parse_args()
sys.exit(run(
exit_code = run(
config_path=args.config,
secrets_path=args.secrets,
history_path=args.history,
verbose=args.verbose,
limit=args.limit,
workers=args.workers,
))
)
if exit_code == 130:
try:
sys.stdout.flush()
sys.stderr.flush()
finally:
os._exit(130)
sys.exit(exit_code)
if __name__ == "__main__":