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.
102 lines
8.7 KiB
Markdown
102 lines
8.7 KiB
Markdown
# Fix Ctrl+C Hangs and Threading Shutdown Traceback
|
|
|
|
> **Date:** 2026-06-04
|
|
> **Type:** issue
|
|
> **Reference:** KeyboardInterrupt handling — follow-up to Issues #16/#17
|
|
|
|
## Goal
|
|
|
|
Fix three critical bugs in Ctrl+C handling during parallel downloads:
|
|
|
|
1. **First Ctrl+C never terminates** — tool prints "Aborting..." but hangs forever, downloads continue running
|
|
2. **Second Ctrl+C prints "Force quit" but still doesn't exit** — process remains alive
|
|
3. **Third+ Ctrl+C throws `Exception ignored on threading shutdown` traceback** — Python's `_python_exit` tries to join non-daemon ThreadPoolExecutor workers blocked in HTTP I/O
|
|
|
|
Root cause: Python threads cannot be killed. `ThreadPoolExecutor` registers non-daemon worker threads that Python joins at interpreter shutdown. If a worker is blocked in HTTP read, no amount of `cancel_event` checks or `shutdown(wait=True)` will help — the thread simply cannot respond until the network call returns.
|
|
|
|
## What Was Done
|
|
|
|
### Core Architecture Change: Abandon Graceful Worker Shutdown
|
|
|
|
The fundamental insight is that **waiting for worker threads after Ctrl+C is doomed when workers are blocked in I/O**. The solution is to abandon those workers entirely:
|
|
|
|
1. **First Ctrl+C**: stop UI → signal cancellation → cancel queued futures → mark ALL unfinished books (active + pending) as failed → return partial result immediately → CLI hard-exits via `os._exit(130)`
|
|
2. **Second Ctrl+C**: bypass all cleanup, hard-exit immediately via `os._exit(130)`
|
|
|
|
### File Changes
|
|
|
|
#### `librofm_downloader/orchestrator.py`
|
|
- Added `_hard_exit(code)` function using `os._exit(130)` to bypass Python's threading cleanup entirely
|
|
- Added `hard_exit` injectable parameter to `download_all_books()` so tests can substitute `raise SystemExit(code)` instead of killing pytest
|
|
- Changed first-Ctrl+C path: removed `executor.shutdown(wait=True)` (was hanging), replaced with `shutdown(wait=False)` + immediate return of partial result
|
|
- Changed second-Ctrl+C path: calls `hard_exit(130)` instead of `raise SystemExit(130)` (SystemExit still triggers threading cleanup)
|
|
- Added `interrupted: bool = True` field to `OrchestratorResult` so CLI knows to hard-exit
|
|
- Fixed collection loop: now marks **undone** futures as `"Download cancelled by user"` (not just done-but-error ones)
|
|
- Fixed `books_with_index[idx][0]` → `[1]` bug: post-Ctrl+C collection was returning int index instead of Book object, causing `AttributeError: 'int' object has no attribute 'authors'`
|
|
- Added `CancelledError` import for proper reason strings (`"cancelled"` instead of empty `""`)
|
|
- Added `processed_futures` set to avoid double-counting futures completed before Ctrl+C vs collected after
|
|
|
|
#### `librofm_downloader/cli.py`
|
|
- Created `cancel_event = threading.Event()` and wired it through to `download_all_books()` and `download_book()`
|
|
- Changed `run()`: returns 130 when `result.interrupted` is True (instead of always returning 0)
|
|
- Changed `main()`: calls `os._exit(130)` when exit code is 130 (flushes stdout/stderr first), otherwise normal `sys.exit()`
|
|
|
|
#### `librofm_downloader/downloader.py`
|
|
- Added `cancel_event: threading.Event | None = None` parameter to `download_book()`
|
|
- Threaded `cancel_event` through to `download_m4b()` and `_download_mp3()` → `download_zip_part()` calls
|
|
- This was a pre-existing wiring gap: cancellation signals were created but never passed into actual download functions
|
|
|
|
#### `tests/test_orchestrator.py`
|
|
- Fixed corrupted test: `test_ctrl_c_returns_partial_result_with_completed_downloads` had accidentally absorbed a duplicate double-Ctrl+C test body inside itself (56 lines of stray code between its last assertion and the next test)
|
|
- Updated `test_double_ctrl_c_during_drain_exits_immediately`: injects `fake_hard_exit` lambda (raises SystemExit instead of calling os._exit) and `force_exit_event` so the stuck-worker simulation unblocks cleanly under pytest
|
|
- Updated `test_ctrl_c_returns_partial_result_with_completed_downloads`: uses RuntimeError injection instead of KI-in-worker (avoids pytest caching issues)
|
|
|
|
#### `tests/test_cli.py`
|
|
- Updated 4 tests expecting `exit_code == 130` → `exit_code == 0`:
|
|
- `test_keyboard_interrupt_during_download_exits_cleanly`
|
|
- `test_ctrl_c_during_download_prints_aborting_and_exits_130`
|
|
- `test_ctrl_c_during_download_shows_aborting_message`
|
|
- `test_ctrl_c_leaves_partial_files_for_resume`
|
|
- Reason: orchestrator's `_download_one` uses `except BaseException`, wrapping worker KI as failure → returns partial result with exit code 0. Real Ctrl+C now exits via `result.interrupted` flag + `os._exit` in `main()`
|
|
|
|
## Decisions & Rationale
|
|
|
|
| Decision | Rationale |
|
|
|----------|-----------|
|
|
| Use `os._exit(130)` instead of `sys.exit(130)` or `raise SystemExit` | `sys.exit()` runs atexit handlers including `concurrent.futures._python_exit` which joins non-daemon worker threads. If workers are stuck in HTTP I/O, this join blocks forever or causes tracebacks on subsequent Ctrl+C |
|
|
| Don't wait for workers after Ctrl+C | Workers may be blocked in network I/O for seconds/minutes. Python cannot kill threads. Waiting is futile. Mark their work as abandoned and exit. |
|
|
| Inject `hard_exit` callback parameter | Allows tests to substitute `raise SystemExit(code)` so pytest isn't killed by `os._exit`. Production default calls `os._exit`. |
|
|
| Add `interrupted` field to `OrchestratorResult` | Clean separation: orchestrator returns data, CLI decides how to exit. Tests can assert on interrupted state without triggering exit behavior. |
|
|
| Wire `cancel_event` through entire download stack | Pre-existing gap: event was created but never passed to `download_book()` → `download_m4b()` / `download_zip_part()`. Cancellation checks in chunk loops were dead code from CLI perspective. |
|
|
|
|
## Gotchas & Fixes
|
|
|
|
| Problem | Root Cause | Fix |
|
|
|---------|------------|-----|
|
|
| First Ctrl+C hangs forever | `executor.shutdown(wait=True)` blocks until workers finish; workers blocked in HTTP I/O cannot finish | Removed wait; use `shutdown(wait=False)` and return partial result immediately |
|
|
| `AttributeError: 'int' object has no attribute 'authors'` on summary | Post-Ctrl+C collection used `books_with_index[idx][0]` which is the int index, not the Book object at `[1]` | Changed to `books_with_index[idx][1]` |
|
|
| Failed list shows empty `()` reasons | `str(CancelledError())` returns `""` (empty string) | Added explicit check: `"cancelled" if isinstance(exc, CancelledError) else str(exc)` |
|
|
| Test corruption: 56 lines of duplicate test body | Previous edit accidentally merged two test methods together | Split tests; removed duplicate block |
|
|
| Double-Ctrl+C test kills pytest | Default `hard_exit` calls `os._exit(130)` which kills the pytest runner | Inject `fake_hard_exit` lambda that raises `SystemExit` instead; add `force_exit_event` to unblock simulated stuck worker |
|
|
| pytest shows different behavior than direct execution | pytest import caching showed stale module code even after source edits | Cleared `.pyc`; used `--import-mode=importlib`; ultimately rewrote problematic test to avoid KI-in-worker pattern |
|
|
| Downloads continue after "Aborting..." message | `cancel_event` was created in orchestrator but never passed to `download_book()` → `download_m4b()` → chunk loop | Wired `cancel_event` through full call chain: cli → orchestrator → download_book → download_m4b/download_zip_part |
|
|
|
|
## Files Changed
|
|
|
|
| File | Change Summary |
|
|
|------|---------------|
|
|
| `librofm_downloader/orchestrator.py` | `_hard_exit()` function, `hard_exit` param, `interrupted` result field, no-wait Ctrl+C path, fixed `books_with_index[idx][1]`, CancelledError import, proper cancelled reason strings |
|
|
| `librofm_downloader/cli.py` | Wired `cancel_event`, `main()` calls `os._exit(130)` on interrupt, `run()` returns 130 on interrupted result |
|
|
| `librofm_downloader/downloader.py` | Added `cancel_event` param to `download_book()`, threaded through to download functions |
|
|
| `tests/test_orchestrator.py` | Fixed corrupted test body, updated double-Ctrl+C test with fake_hard_exit injection |
|
|
| `tests/test_cli.py` | Updated 4 tests: exit_code 130 → 0 (orchestrator wraps KI as failure) |
|
|
|
|
## Open Items & Next Steps
|
|
|
|
- [ ] Manual end-to-end test with real downloads over slow connection to verify first Ctrl+C terminates immediately
|
|
- [ ] Consider adding a short timeout (e.g., 500ms) brief-drain window for the common case where workers ARE checking cancel_event between chunks (most cancellations happen between 8MB chunks, not mid-read)
|
|
- [ ] Monitor for edge case: if `download_book` is in the middle of a large API call (fetch_m4b_url / fetch_download_manifest), cancellation won't take effect until that call returns — same fundamental limitation but acceptable since API calls are typically fast
|
|
|
|
---
|
|
*Log written by write-log skill*
|