Repository navigation
Description
What is broken.
The cron keep-alive in server/app/services/diary_keepalive.py works in three steps:
- It claims a batch of «Сетевой город» sessions, reads each row's sealed
upstream_tokenand commits. - It pings every session with no transaction open.
- It writes the results back.
The module docstring and the _apply docstring both say the write-back is a guarded UPDATE that "matches nothing if the row was signed out or its credential rotated meanwhile", so "a vanished or changed row is skipped, not a lost update". The code only guards the second half. The UPDATE in _apply matches on id and expired_at IS NULL. It never compares the stored credential with the one that was claimed; _Result does not even carry the claimed value.
Every successful ping also sets upstream_token, because _ping always returns connection.credential. For a NetSchool connection that is json.dumps of the session dict, which is never empty. So every ok result writes back the credential as it was at claim time, plus whatever the ping itself picked up. If a diary read stored a newer credential in the meantime, it is overwritten.
Reads do rotate credentials. NetSchoolClient._authed_json takes a new at response header and changed NSSESSIONID/ESRNSec cookies into the session dict. DiaryClient._remember_token then seals the new credential into the row and commits. Its own docstring explains why: "dropping it would replay the old one next call". The provider contract in providers/diary/base.py makes the same promise: "a token the upstream rotates mid-call is kept".
The failure scenario.
- A family is signed in to «Сетевой город». Their
diary_sessionsrow holds credential A, withat = XandNSSESSIONID = S1. - The cron tick reaches
keep_alive._claimreads A for that row, stampskeepalive_attempted_atand commits. From here on no lock is held. - While the pings are in flight, the parent opens the diary in the bot or the app. The upstream's answer carries a new
at: Yheader, or aSet-Cookiewith a newNSSESSIONID = S2.DiaryClient._remember_tokenseals credential B (withY/S2) intoupstream_tokenand commits. In a batch of 200 pinged 8 at a time against an 18-second deadline, with a 5-second read timeout, this window is roughly 20 seconds for every row in the batch, because_applyruns only after the slowest ping. - The ping for that row succeeds.
_applyrunsUPDATE diary_sessions SET upstream_token = seal(A'), kept_alive_at = …, upstream_ok_at = … WHERE id = … AND expired_at IS NULL. Here A' is A plus only what the ping itself absorbed. The row is still live, so theUPDATEmatches and B is replaced. - The next read unseals A' and sends the old
at = X/NSSESSIONID = S1. If the upstream retired them when it issued the new values,_authed_jsongets a 401, a redirect or a login page and raisesSessionExpired. The row is expired and the parent is told to sign in again, although they did nothing wrong. The keep-alive exists to prevent exactly this. In the milder case the upstream still accepts the old values, but the rotation that the read path, the provider contract and the docstring all promise to keep is silently lost. server/tests/test_diary_keepalive.pyhas no test that changesupstream_tokenbetween the claim and the apply, so nothing catches this.
Where.
server/app/services/diary_keepalive.py:18-22: the module docstring promises a guard for "its credential rotated meanwhile".server/app/services/diary_keepalive.py:163-165: the_applydocstring repeats the promise ("matches on what was read … rotated between the claim and now simply matches nothing").server/app/services/diary_keepalive.py:170-178: theokbranch setsupstream_tokenunderWHERE id = … AND expired_at IS NULLonly.server/app/services/diary_keepalive.py:156:_pingalways returnsconnection.credential, so every successful ping carries a credential to write.server/app/services/diary_keepalive.py:66,:123: the claimed credential is captured in_Claim.credentialbut never compared at write-back._Result(:70-73) does not carry it.server/app/services/diary_keepalive.py:124: the claim commits before the pings, so no row lock covers the in-flight window.server/app/services/diary.py:473-489,:502-504:DiaryClient._call/_remember_tokenstore a rotated credential after every read.server/app/providers/netschool/client.py:146-156,:233-236: the NetSchool client absorbs rotated cookies and a newatheader into the credential.server/app/providers/diary/base.py:48-50: the provider contract promises that a rotated token is kept.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
Let a phone register a diary session it opened itself, and fix what t…