🐛 fix: serialize concurrent re-auth with a lock #201

Merged
mat merged 1 commit from fix/serialize-reauth into main 2026-10-27 14:37:12 +00:00
Owner

Summary

Concurrent re-auth is serialized (#182):

_reauthenticate()
└── with _reauth_lock:
    ├── session already fresh (another thread just re-authed)?
    │   └── yes ──► skip the login, let the caller retry
    └── no ──► _post_authent()  (single login while the others wait)

get_devices_ts_time_ago docstring: states the serialization guarantee

Freshness is judged from session_expires_at; when the server omits x-cwt-token, waiters fall back to serialized logins (documented).

Evidence

  • Before: a 401 mid-flight made every batch worker call _reauthenticate() concurrently — cookie-jar race, N duplicate logins. Test run red: 2 logins for 3 workers (timing-dependent, sometimes 3).
    After: TDD — red confirmed first, then the lock; the concurrent test passes 3× consecutively (non-flaky), pytest -q → 243 passed, mypy → clean.

Merge Danger

Door: two-way

Blast Radius: low — single-threaded re-auth paths behave identically (the lock is uncontended); the freshness-skip is a strict subset of the old always-login behavior.

Closes #182.

## Summary Concurrent re-auth is serialized (#182): ```text _reauthenticate() └── with _reauth_lock: ├── session already fresh (another thread just re-authed)? │ └── yes ──► skip the login, let the caller retry └── no ──► _post_authent() (single login while the others wait) get_devices_ts_time_ago docstring: states the serialization guarantee ``` Freshness is judged from `session_expires_at`; when the server omits `x-cwt-token`, waiters fall back to serialized logins (documented). ## Evidence - **Before:** a 401 mid-flight made every batch worker call `_reauthenticate()` concurrently — cookie-jar race, N duplicate logins. Test run red: **2 logins** for 3 workers (timing-dependent, sometimes 3). **After:** TDD — red confirmed first, then the lock; the concurrent test passes **3× consecutively** (non-flaky), `pytest -q` → 243 passed, `mypy` → clean. ## Merge Danger **Door:** two-way **Blast Radius:** low — single-threaded re-auth paths behave identically (the lock is uncontended); the freshness-skip is a strict subset of the old always-login behavior. Closes #182.
🐛 fix: serialize concurrent re-auth with a lock
All checks were successful
Tests / forgejo-pytest (push) Successful in 43s
Tests / forgejo-pytest (pull_request) Successful in 44s
cc7585ef4b
mat force-pushed fix/serialize-reauth from cc7585ef4b
All checks were successful
Tests / forgejo-pytest (push) Successful in 43s
Tests / forgejo-pytest (pull_request) Successful in 44s
to 5730a86d8d
All checks were successful
Tests / forgejo-pytest (push) Successful in 48s
Tests / forgejo-pytest (pull_request) Successful in 47s
2026-10-27 14:37:01 +00:00
Compare
mat changed title from WIP: 🐛 fix: serialize concurrent re-auth with a lock to 🐛 fix: serialize concurrent re-auth with a lock 2026-10-27 14:37:11 +00:00
mat merged commit 4a05486713 into main 2026-10-27 14:37:12 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
mat/python-comwatt-client!201
No description provided.