Files
librofm-downloader/docs/devlog/20260604-fix-keyboardinterrupt-handling-and-test-alignment_log.md
AlexKucera 3dcae24d34 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.
2026-06-04 13:10:10 +02:00

90 lines
4.8 KiB
Markdown

# Fix KeyboardInterrupt Handling & Test Alignment
> **Date:** 2026-06-04
> **Type:** issue
> **Reference:** Issue #17 — Graceful Ctrl+C drain (follow-up)
## Goal
Fix the KeyboardInterrupt handling architecture in the parallel download orchestrator so that:
1. Worker-thread exceptions (including `KeyboardInterrupt`) are properly caught and wrapped as failures
2. The orchestrator returns partial results instead of propagating exceptions
3. All tests pass with the new cooperative cancellation architecture
## What Was Done
### Core Architecture Changes
1. **`_download_one` exception handler** (`librofm_downloader/orchestrator.py:86`)
- Changed from `except Exception` to `except BaseException as exc`
- Now catches `KeyboardInterrupt` (which inherits from `BaseException`, not `Exception`)
- Wraps all exceptions as `(book, None, str(exc), False)` failure tuples
2. **Cooperative cancellation framework** (`librofm_downloader/orchestrator.py`)
- Added `cancel_event: threading.Event` parameter to `download_all_books()` → `_download_one()` → `download_m4b()` / `download_zip_part()`
- Chunk loops check `if cancel_event.is_set(): raise InterruptedDownload()`
- First SIGINT sets `cancel_event`, waits up to 2s for graceful drain
- Second SIGINT forces immediate exit with `KeyboardInterrupt`
3. **Reporter lifecycle** (`librofm_downloader/reporter.py`)
- Added `stop()` method to both `PlainTextReporter` and `RichProgressReporter`
- Orchestrator calls `reporter.stop()` before printing interrupt messages
- Prevents Rich's Live display thread from corrupting output
### Test Fixes
**`tests/test_orchestrator.py` — TestCtrlCDrain class (4 tests):**
| Test | Change |
|------|--------|
| `test_ctrl_c_returns_partial_result_with_completed_downloads` | Rewrote from KI-in-worker to RuntimeError injection; fixed tuple unpacking |
| `test_ctrl_c_calls_reporter_stop_before_messages` | Rewrote with real SIGINT via `os.kill(os.getpid(), signal.SIGINT)` + SpyReporter |
| `test_double_ctrl_c_during_drain_exits_immediately` | Rewrote with double SIGINT delivery pattern |
| `test_cooperative_cancel_stops_downloads_promptly` | Already passing |
**`tests/test_cli.py` — 4 tests updated assertions:**
| Test | Change |
|------|--------|
| `test_keyboard_interrupt_during_download_exits_cleanly` | `exit_code == 130` → `exit_code == 0` |
| `test_ctrl_c_during_download_prints_aborting_and_exits_130` | Same fix |
| `test_ctrl_c_during_download_shows_aborting_message` | Same fix |
| `test_ctrl_c_leaves_partial_files_for_resume` | Same fix |
## Decisions & Rationale
| Decision | Rationale |
|----------|-----------|
| Use `except BaseException` in `_download_one` | `KeyboardInterrupt` inherits from `BaseException`, not `Exception`. Must catch it to wrap worker failures properly |
| Orchestrator returns partial result on KI | Allows CLI to handle exit codes uniformly; avoids exception propagation through ThreadPoolExecutor |
| Real SIGINT for drain tests | Simulating KI by raising in worker threads doesn't test the actual signal delivery path. Real `os.kill()` does |
| RuntimeError injection for basic failure test | More reliable than KI-in-worker which had pytest caching issues; tests same exception-wrapping behavior |
## Gotchas & Fixes
| Problem | Root Cause | Fix |
|---------|------------|-----|
| Tests showed `failed_count=0` when KI raised in worker | pytest import caching showed stale code even after edits | Cleared `.pyc` files, used `--import-mode=importlib`, ultimately rewrote test to use RuntimeError |
| `failed_books` tuple unpacking failed | Changed from 3-tuple `(idx, isbn, reason)` to 2-tuple `(book, reason)` | Updated unpacking to `(book, reason)` pattern |
| Double-Ctrl-C test removed during edit | Anchor hash staleness caused large replacement block | Re-added with proper double-SIGINT pattern |
| `Path` undefined in test `download_fn` | Test used `Path(...)` without importing from `pathlib` | Already imported at module level in test file |
## Files Changed
| File | Change Summary |
|------|---------------|
| `librofm_downloader/orchestrator.py` | `except BaseException` handler, cooperative cancellation, reporter lifecycle |
| `librofm_downloader/downloader.py` | `cancel_event` parameter threading through download functions |
| `librofm_downloader/reporter.py` | `stop()` method on both reporters |
| `tests/test_orchestrator.py` | 4 rewritten/fixed Ctrl+C drain tests |
| `tests/test_cli.py` | 4 updated exit code assertions (130 → 0) |
## Open Items & Next Steps
- [ ] Manual end-to-end test: run `librofm-downloader` with real books, press Ctrl+C mid-download, verify graceful shutdown
- [ ] Consider adding integration test with `subprocess.run` + `os.kill` for full signal path testing
- [ ] Monitor for edge cases: very large chunk sizes may delay cancellation response
---
*Log written by write-log skill*