oliver-zehentleitner / maity

clean-up: limits and abuse: one waiter-per-key rule (the _holding copies) and one long-poll loop

Sign in to report

tsk-javm686g22kx 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 A2-5 of the audit.

This may also concern the security team: by reading the code (not reproduced), two waits of one key on one replica can both end as superseded, so a waiter may be dropped without an answer. Kept apart from the routine clean-up for that reason. No security card covers it.

1. "One waiter per key" exists twice, with different rules (design, possible bug)

  • routes/device_codes.py:57, 107-137 and routes/notifications.py:42, 208-244 each keep a _holding: dict[str, object] where the last arrival wins.
  • longpoll.Registry.enter (longpoll.py:57-71) decides by the database start (started) instead. Its docstring says why arrival order is wrong: "a wait can reach this after a newer one of its key ... leaving neither".
  • The _holding waits pass no on_start to wakeups.opened (device_codes.py:122), so their waiter's started stays 0.
  • Proposal: give wakeups.Held the waiter's superseded flag and pass on_start, so _holding can go and both routes use the registry's rule.
  • First step: a test that starts two waits of one key on one replica and asserts that exactly one ends as superseded. If it passes today, say so on the card and treat the rest as clean-up.

2. The long-poll loop is copied (duplication)

  • routes/events.py:264-318 and routes/runner.py:280-331 hold the same loop: wakeups.opened, registry.enter, the superseded and disconnect checks, event.clear, read, stopping, the 2 s fallback (FALLBACK_SECONDS), then leave. Only read, the ended check and the quiet pass differ.
  • Proposal: a longpoll.hold(...) helper that owns the loop, with read and quiet passed in, used by both routes (and by the two routes of item 1 once they use the registry).

Effort: M (both items). Risk: medium. Long-poll timing; run test_wakeups, test_event_quiet_passes and test_shutdown on PostgreSQL.

Order

  • Item 1 before item 2.
  • tsk-x1rsd4r6htcv (in review) changes what an events waiter does on a wake in routes/events.py. Do item 2 after it.

Covered elsewhere

  • None. Searched: _holding, longpoll, wakeups.

Effort sum: 1M.