oliver-zehentleitner / maity
clean-up: hub shared helpers: one SweepWorker for the 18 copied background workers, one insert-then-lock helper
tsk-dzpzpn5xrm5j Open
From the clean-up audit tsk-hgnbapyw7hnh, the cross-area findings (areas 10-17) that the per-area cards left to one shared card. Read at main 80647565; every line number is at that SHA, so check it against current main first. The low-risk shared helpers (If-None-Match, chunks, base64url cursors, ignoring_duplicates) are on a sibling card filed at the same time; this card holds the two items that touch threads and row locks.
Order:
mail/outbox.py: do item 1's change tooutbox.Workerafter tsk-dv8f620hjebf (in progress, outbox poison rows) and tsk-3ar6h2s0kff5 (adds an hourly prune tooutbox.Worker).write_guard.ensure_row(item 2): tsk-447hj5cxcjvc wants account_lock rows created at account creation instead of lazily. If that lands first,ensure_rowmay go away instead of moving to the helper.notifications/mailer.py(both items): tsk-e2m6krwdk5rg and tsk-y5hkg2ztvwq9 edit the same file; rebase rather than conflict.
1. The background minute-sweep Worker class is copied in 18 modules (duplication)
- Files (each
class Workerwithstart/stop/_runaroundworker_runs.claim): - attachments.py:603 (claim :624)
- auth/known_sources.py:110 (:131)
- availability.py:938 (:961)
- commitment.py:260 (:283)
- images.py:413 (:446)
- invitations.py:641 (:664)
- jourfixe/worker.py:34 (:55)
- mail/outbox.py:351 (:377)
- not_listening.py:280 (:304)
- not_listening_digest.py:596 (:626)
- notifications/mailer.py:596 (:624)
- org_invitations.py:384 (:407)
- public_pages.py:541 (:593)
- public_snapshot.py:171 (:212)
- registration_accounts.py:485 (:510; variant: takes
hub, runs two steps in one transaction) - reports.py:643 (:664)
- takeovers.py:859 (:882)
- probe.py:1013 (:1067; a different shape: a job queue plus named passes,
stoppinginstead of_stop). Probably stays as it is, or uses only the claim-and-log part. - Not copies: request_stats.Reporter (request_stats.py:236) and wakeups.Listener (wakeups.py:265) are thread loops without a claim.
- What is wrong: the same about 20 lines (thread name,
Event,join(timeout=5),while not stop.wait(interval), the claim,log.exception) are written 17 times. The copies have already drifted: - 9 of the 17 skip a pass during a maintenance window (
maintenance.running_now: availability, commitment, invitations, jourfixe, not_listening, not_listening_digest, org_invitations, registration_accounts, takeovers). The other 8 do not. - 7 of the 17 pass
self._stopinto the claim (attachments, images, mail/outbox, not_listening_digest, notifications/mailer, public_pages, public_snapshot), so a long pass can stop between steps and renews its lease. The other 10 cannot. - Proposal: one
worker_runs.SweepWorker(engine, name, interval, step, *, thread_name, skip_in_window, pass_stop)(or a base class withstep(self, claim)), which every module above except probe uses. Each module keeps only its step function and its interval constant. - Open questions:
- Should every sweep skip a maintenance window, or only those whose comment says "nobody can answer during a window"? Decide per worker. The helper makes it one explicit flag instead of a missing line.
- Should every pass get the stop event (and so lease renewal through
Claim.is_set)? This is cheap and also helps tsk-f5jge5aw7ah1's replica rules. - Effort: M. Risk: low to medium. No behaviour change if the flags keep today's choices. Test names that patch
Worker._runor a module'sINTERVALneed updating. app.py starts and stops every worker, so the start/stop order must stay the same.
2. "Insert the row if absent, then lock it" is written 8 times (duplication)
- Files (each: a
begin_nested()savepoint with an INSERT,except IntegrityError: pass, thenSELECT ... FOR UPDATE): - mail/address.py:136-149 (
take_lock, insert :144, catch :145; used bylockat :129 and by routes/registration.py:264 forregistration_lock) - handles.py:129-140 (
lock, :133-134) - write_guard.py:108-121 (
ensure_row; this one is followed by a shared lock at :133) - mail/outbox.py:142-160 (
_admit: lock first, insert only when missing, then lock again) - notifications/mailer.py:362-378 (
_state, :369-370) - notifications/preferences.py:137-160 (
row_of, :158) - landing.py:744-766 (
lock, :762) and legal.py:118-132 (lock, :128): two more copies, not in the audit's list. - What is wrong: each copy decides again whether to read first, whether to catch inside or outside the savepoint, and how to lock (exclusive or shared). The pattern is subtle on MariaDB (gap locks) and SQLite (no FOR UPDATE), so it should be written and tested in one place.
- Proposal:
db.ensure_locked(conn, table, key: dict, defaults: dict, *, read=False) -> Row. It tries the plain locked SELECT first, inserts in a savepoint only when the row is missing, and locks again. The 8 callers use it. - Effort: M. Risk: medium. These are hot lock paths: address locks at registration, handle locks, the account lock that every human write takes, and mail budgets. Keep each caller's lock mode and order, and run the race tests on all three engines (hub-mariadb).
Left out as duplicates: none. Searched: Worker, worker_runs, ensure_row, IntegrityError, insert then lock. The per-area cards tsk-j967n09yz6ps, tsk-hz5nkwyn9sj9 and tsk-tnetc5fgr7pm left the Worker class to this card on purpose.
Effort sum: M+M = 6 points.
Notes
Maity Coder Claude 003 · Nov 13, 2026, 23:12 UTC
From the clean-up audit (tsk-hgnbapyw7hnh; areas at 80647565, uncovered-files pass at 9dfd4b65).
The copies have drifted (A8-10): some copies pass the stop event to
claimand some don't, and only some wait while a maintenance window runs. One shared loop should fix both for every worker.Sites the audit named:
takeovers.py:859andcommitment.py:260(compared line by line: identical)reports.Worker,attachments.Worker,images.Workerprobe.py:1013availability.py:938,not_listening.py:280,not_listening_digest.py:596jourfixe/worker.py:35-58, with a broadexceptat:58app.py's lifespan starts and stops 21 workers by hand. Card tsk-581bcbm72g95 holds that wiring; this card holds the loop itself.
Maity Coder Claude 002 · Nov 23, 2026, 00:04 UTC
Item 2, write_guard.ensure_row: settled by tsk-447hj5cxcjvc (branch agent-c/37qm-account-lock-at-creation). The lead decided ensure_row goes away: account_lock rows are made with the account plus a backfill migration, and write_guard.lock_row raises AccountLockMissing for a human without a row. So ensure_row does not move to the shared insert-then-lock helper; the other seven copies of item 2 remain for this card.