Skip to content

bug: drain loop reads only the first 1000 pending tasks, discarding large runs #715

Closed

Description

@gkhngyk

Description

For three consecutive nights the largest brand's tracking run was discarded even though its results arrived normally. The ledger row was deleted each time, so the dashboard's 24h window and the Daily Pulse both stayed anchored to an older day while ~1300–1800 fresh results sat in the database.

The cause is a query, not a timing threshold: the drain loop reads a brand's pending tasks without pagination, and PostgREST silently caps un-paginated selects at 1000 rows.

const { data: rows } = await supabaseAdmin
  .from('cloro_pending_tasks')
  .select('task_id, submitted_at')
  .eq('brand_id', brandId);        // no range → capped at 1000

The comment above it reads "a handful of rows at most", which was true when it was written.

Why it produces exactly this failure

The brand submits ~2130 scraper tasks, so every poll reports 1000 pending regardless of reality. That is wrong in two directions at once:

  1. First poll: processed = 2130 − 1000 = 1130, so the loop concludes delivery has started and begins the stall clock — against a run that has received nothing.
  2. Every later poll: the count never changes, because real pending stays above 1000, so the loop concludes nothing is progressing.

Fifteen minutes of "no progress" satisfies the ghost-poll count; the tasks then cross the 30-minute ghost age and the run exits.

Measured on the 13th: tasks submitted 06:15:39, worker exited 06:45:43 — four seconds after the 30-minute mark — and the first real result landed 06:49:52, four minutes later. 1310 results were stored by a run that no longer existed.

Why only one brand

Brand Scraper tasks
the affected brand 2130
next largest 840
third 832
everything else < 410

Only one brand is above the 1000-row cap, which is why this looked brand-specific rather than like a query bug. It is latent for the others: the next two are one growth spurt away from the same wall.

Confirmed empirically against this project's REST endpoint — an un-paginated select returns exactly 1000 rows.

Two smaller holes, each able to cause the same exit alone

"Delivery started" is inferred from the wrong signal. The check is pending < expectedSubmitted, but the callback handler also deletes pending rows for FAILED tasks, for a COMPLETED task with no response body, and when the brand lookup fails. None of those produce a result — and ~800 of that day's tasks produced no result at all. A shrinking pending set is not proof that anything arrived.

The ghost age is hardcoded at 30 minutes. Its premise is that a task older than the threshold can no longer be in flight, and current delivery latency undercuts it: one brand's first result arrived at 34 minutes, another's at 29.7 — seconds under the threshold. It cannot be tuned without a deploy.

Proposed fix

  • Page the pending-task read (1000-row pages, the same approach used for the citations scan).
  • Require an actual result row before treating delivery as started.
  • Make the ghost age configurable, default unchanged at 30 minutes.

Acceptance criteria

  • A run submitting more than 1000 tasks sees its true pending count on every poll.
  • A run receives no "delivery started" signal until a result exists for it.
  • A brand whose first result arrives later than the ghost age still completes and stamps its run.
  • Brands under 1000 tasks behave exactly as they do today.
  • A failed page read retries the poll rather than returning a partial set.

Activity

  1. added
    bug
    tracking
    on Aug 13, 2026
  2. self-assigned this
    on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugtracking

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions