Make DownloadHistory.write() safe for concurrent callers by adding a threading.Lock. Multiple worker threads will call history.write() simultaneously when books finish downloading in parallel.
Changes to history.py:
Add import threading and a _lock = threading.Lock() instance attribute on DownloadHistory.__init__()
Acquire self._lock in write() around both self._data[entry.isbn] = asdict(entry) and self._flush()
Leave is_downloaded() and find() lock-free — CPython dict reads for single keys are atomic, and these are best-effort checks where stale reads are acceptable
Lock is reentrant-safe (Python threading.Lock is a mutex, not a reentrant lock, but write() never calls itself recursively so this is fine)
Acceptance criteria
DownloadHistory has a _lock attribute initialized in __init__
write() acquires the lock before mutating _data and calling _flush()
3–4 new tests: concurrent writes via threading.Thread do not corrupt JSON; no lost entries under contention; lock is acquired/released; sequential behavior unchanged
Existing history tests still pass (no regression)
Blocked by
None - can start immediately
## Parent
#11
## What to build
Make `DownloadHistory.write()` safe for concurrent callers by adding a `threading.Lock`. Multiple worker threads will call `history.write()` simultaneously when books finish downloading in parallel.
**Changes to `history.py`:**
- Add `import threading` and a `_lock = threading.Lock()` instance attribute on `DownloadHistory.__init__()`
- Acquire `self._lock` in `write()` around both `self._data[entry.isbn] = asdict(entry)` and `self._flush()`
- Leave `is_downloaded()` and `find()` lock-free — CPython dict reads for single keys are atomic, and these are best-effort checks where stale reads are acceptable
- Lock is reentrant-safe (Python `threading.Lock` is a mutex, not a reentrant lock, but `write()` never calls itself recursively so this is fine)
## Acceptance criteria
- [ ] `DownloadHistory` has a `_lock` attribute initialized in `__init__`
- [ ] `write()` acquires the lock before mutating `_data` and calling `_flush()`
- [ ] 3–4 new tests: concurrent writes via `threading.Thread` do not corrupt JSON; no lost entries under contention; lock is acquired/released; sequential behavior unchanged
- [ ] Existing history tests still pass (no regression)
## Blocked by
None - can start immediately
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Parent
#11
What to build
Make
DownloadHistory.write()safe for concurrent callers by adding athreading.Lock. Multiple worker threads will callhistory.write()simultaneously when books finish downloading in parallel.Changes to
history.py:import threadingand a_lock = threading.Lock()instance attribute onDownloadHistory.__init__()self._lockinwrite()around bothself._data[entry.isbn] = asdict(entry)andself._flush()is_downloaded()andfind()lock-free — CPython dict reads for single keys are atomic, and these are best-effort checks where stale reads are acceptablethreading.Lockis a mutex, not a reentrant lock, butwrite()never calls itself recursively so this is fine)Acceptance criteria
DownloadHistoryhas a_lockattribute initialized in__init__write()acquires the lock before mutating_dataand calling_flush()threading.Threaddo not corrupt JSON; no lost entries under contention; lock is acquired/released; sequential behavior unchangedBlocked by
None - can start immediately
Closed via commit
a18232c— thread-safe DownloadHistory.write() with threading.Lock. 5 new tests, 184 total passing.