oliver-zehentleitner / maity

clean-up: limits and abuse: idempotency dead exemptions, per-write table sweep, stale compatibility rule

Sign in to report

tsk-arnqbr6et9wp Open

From the clean-up audit tsk-hgnbapyw7hnh, Area 2 (Limits and abuse), read at 80647565. Line numbers are at 80647565; check each one against current main before changing it. Finding numbers are the audit's (A2-n).

Three small items in idempotency.py and its tests and docs. All S, one PR.

1. The idempotency exemption list is dead code (A2-6, debt)

  • idempotency.py:34-35, 59-63: EXEMPT_PREFIXES = ("/api/v1/join/", "/api/v1/auth/") and EXEMPT_PATHS = ("/api/v1/me/resume",) only matter inside key_for. key_for runs only from run, replay_first and routes/jour_fixe.py:661. None of the exempt routes calls these: POST /join/{token} (joins.py:913), POST /me/resume (me_session.py:228), and no route under /auth/ (routes/auth.py, email_sign_in.py and password_reset.py do not import idempotency).
  • In practice a route is exempt by not calling idempotency.run. That is also how the runner's five exemptions of §1.5 work, and they are not in the list.
  • hub/tests/test_idempotency.py:189-191 test_resume_needs_no_key passes with or without the list, so it tests nothing about idempotency.py.
  • Proposal: delete both constants and the exempt lines; say in the docstring that a route is exempt by not going through run. Move the resume test to the me_session tests, or drop it.
  • Effort: S. Risk: very low.

2. Every keyed write sweeps the whole idempotency table inside its transaction (A2-7, debt, hot path)

  • idempotency.py:187-189 _store runs DELETE FROM idempotency_key WHERE created_at <= now - 24h in the action's transaction, on every successful agent POST or PATCH with a key (the hottest write path). Concurrent writers that find the same expired rows wait on each other's row locks until commit, on PostgreSQL and MariaDB. Nothing else deletes from this table (only loadtest.py:237 names it).
  • shared_counts._upsert (shared_counts.py:109-131) solves the same problem by sweeping every 1000th add (SWEEP_EVERY).
  • Proposal: sweep every Nth store (as shared_counts does), or in a periodic worker, outside the request's transaction. The read already filters by created_at > now - RETENTION (idempotency.py:155), so expired rows left a little longer are harmless.
  • Effort: S. Risk: low.

3. A compatibility rule for keys stored before the call hash (A2-8, debt / docs drift)

  • docs/api.md:328-330: "A key stored before this rule answers idempotency_mismatch on every repeat until it expires (24 hours)." hub/tests/test_idempotency.py:153-167 (test_a_key_stored_before_the_call_hash_is_a_mismatch) keeps it alive by writing a body-only hash into the table.
  • The alpha has no legacy users and keys live 24 hours, so no such key exists. The code has no special path for them; the test only proves again that a different hash is a mismatch, which test_a_repeat_with_another_body_is_a_mismatch (line 145) already covers.
  • Proposal: drop the sentence and the test.
  • Effort: S. Risk: none.

Order

  • After tsk-cjafxd0mchhh (in progress), which changes _replay and the loaders in idempotency.py. Rebase on it rather than conflict.
  • If item 2 picks a periodic worker, use the shared sweep worker of tsk-dzpzpn5xrm5j instead of another copied Worker class.

Covered elsewhere

  • None. Searched: idempotency.py, EXEMPT_PREFIXES, idempotency_key.

Effort sum: 3S.