✨ feat: proactive re-auth before WS handshakes #161

Merged
mat merged 1 commit from feat/ws-proactive-reauth into main 2026-10-27 10:27:12 +00:00
Owner

Summary

 _connect_and_start (WS handshake entry point — start + every reconnect)
+  self._reauthenticate_if_expired()   # before reading the cookie

The WebSocket handshake was the only client path not going through _request, so the proactive half of auto_reauth (#160) never applied to it. One line in _connect_and_start fixes both entry points: a stream no longer starts with a nearly-dead cookie, and after a drop the reconnect handshake presents a fresh cookie instead of eating a 401 → re-login → retry. The reactive 401-at-handshake path is kept as the fallback (early revocation, unknown expiry, clock skew).

Live-context from today's probes: POST /v1/logout does NOT invalidate the session server-side (the old cookie keeps working), so expiry is presumably only enforced where the cookie is presented — REST requests and WS handshakes. A 2 h live probe is currently running to confirm whether an established stream survives the real expiry instant; either outcome keeps this slice correct (it covers every path where the cookie is actually presented).

Evidence

  • Before: stream start / reconnect with an expired session → handshake 401 → reactive re-auth → retry (or terminal auth error).
  • After: 5 new tests (proactive fires at start when expired and hands the fresh cookie to the handshake; fires on reconnect when the session expired mid-stream; silent when expiry is >30 s away, without stored credentials, or with auto_reauth=False); existing reactive-handshake tests pass unchanged; pytest = 202 passed; mypy clean.

Merge Danger

Door: two-way — one line plus tests; revert restores reactive-only handshakes.

Blast Radius: low — stream_measurements only; with auto_reauth (default) an expired-at-start stream now costs one login POST instead of a 401 round trip.

Closes #158 (handshake half; the established-stream-through-expiry observation lands in the field notes once the running 2 h probe completes)

## Summary ```diff _connect_and_start (WS handshake entry point — start + every reconnect) + self._reauthenticate_if_expired() # before reading the cookie ``` The WebSocket handshake was the only client path not going through `_request`, so the proactive half of `auto_reauth` (#160) never applied to it. One line in `_connect_and_start` fixes both entry points: a stream no longer starts with a nearly-dead cookie, and after a drop the reconnect handshake presents a fresh cookie instead of eating a 401 → re-login → retry. The reactive 401-at-handshake path is kept as the fallback (early revocation, unknown expiry, clock skew). Live-context from today's probes: `POST /v1/logout` does NOT invalidate the session server-side (the old cookie keeps working), so expiry is presumably only enforced where the cookie is presented — REST requests and WS handshakes. A 2 h live probe is currently running to confirm whether an established stream survives the real expiry instant; either outcome keeps this slice correct (it covers every path where the cookie is actually presented). ## Evidence - **Before:** stream start / reconnect with an expired session → handshake 401 → reactive re-auth → retry (or terminal auth error). - **After:** 5 new tests (proactive fires at start when expired and hands the fresh cookie to the handshake; fires on reconnect when the session expired mid-stream; silent when expiry is >30 s away, without stored credentials, or with `auto_reauth=False`); existing reactive-handshake tests pass unchanged; `pytest` = **202 passed**; `mypy` clean. ## Merge Danger **Door:** two-way — one line plus tests; revert restores reactive-only handshakes. **Blast Radius:** low — `stream_measurements` only; with `auto_reauth` (default) an expired-at-start stream now costs one login POST instead of a 401 round trip. Closes #158 (handshake half; the established-stream-through-expiry observation lands in the field notes once the running 2 h probe completes)
✨ feat: proactive re-auth before WS handshakes (#158)
All checks were successful
Tests / forgejo-pytest (push) Successful in 44s
Tests / forgejo-pytest (pull_request) Successful in 43s
9ca164d389
mat changed title from WIP: ✨ feat: proactive re-auth before WS handshakes to ✨ feat: proactive re-auth before WS handshakes 2026-10-27 10:27:06 +00:00
mat merged commit fd3861cbfb into main 2026-10-27 10:27:12 +00:00
mat deleted branch feat/ws-proactive-reauth 2026-10-27 10:27: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!161
No description provided.