Repository navigation
fix(tracking): page the pending-task read so large runs aren't cut short - #716
Merged
Merged
Conversation
PostgREST silently caps an un-paginated select at 1000 rows. The drain loop read a brand's pending tasks in one request, so the largest brand — which submits ~2130 tasks — saw a pending count frozen at exactly 1000 on every poll. That produced two false readings at once. On the first poll it looked like 1130 tasks had already finished, which set "delivery started" and began the stall clock against a run that had received nothing. On every poll after it the number never moved, which read as "nothing is progressing". Fifteen minutes of that, plus the tasks crossing the 30-minute ghost age, exited the run four seconds after the threshold — four minutes before its first result arrived. Three consecutive nights the brand's ledger row was deleted and its dashboard and pulse stayed frozen on an older window. It only ever bit the one brand above 1000 tasks (next largest: 840), which is why it looked brand-specific rather than like a query bug. Two smaller holes closed alongside it, both able to cause the same premature exit on their own: - "Delivery started" now requires a result to actually exist, not merely a pending row to disappear. The callback handler also deletes rows for FAILED tasks, for a COMPLETED task with no response body, and when the brand lookup fails; ~800 of today's tasks produced no result at all, so a shrinking pending set was never proof that anything had arrived. - The ghost age is now configurable (CLORO_GHOST_TASK_AGE_MIN, default 30 — unchanged behaviour). Its premise is that a task older than the threshold can no longer be in flight, and current delivery latency undercuts that: one brand's first result arrived at 34 minutes, another's at 29.7. Replaying today's numbers through both versions: the old drain exits at 30.3 minutes with 0 of 1310 results and the run is discarded; the new one exits drained at 57.0 minutes with all 1310, having detected first delivery at 34.3 minutes against a true 34.0.
6 of 11 tasks
gkhngyk
added a commit
that referenced
this pull request
Aug 19, 2026
…row cap (#740) The skip added in #739 read one row per *citation*, not per answer: 200 answers matched 1,591 rows in production, and PostgREST silently caps an un-paginated select at 1,000. Everything past that first page looked unstored, was reprocessed, and had its writes discarded as duplicates — so a repair run reported thousands of citations written while the table did not move at all. The same trap as #714 and #716, and this file now says so. The lookup is paginated. A short page is the end-of-results signal, which is why it reads in exactly PostgREST's own page size. The reporting that hid it is fixed too. persistCitationRows counted the rows it attempted rather than the rows that landed, so duplicates counted as writes. The upsert now returns what it inserted, and the function separates three outcomes that were previously one number: n rows inserted 0 nothing new — every row was already there, which is what --force produces on every answer it revisits null nothing stored, retry Conflating 0 with failure would have made --force report every answer as broken; conflating failure with 0 is what let the original 228 lost answers look like a healthy run. Refs #732
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The drain loop read a brand's pending tasks in one un-paginated request, and PostgREST silently caps those at 1000 rows. The largest brand submits ~2130 tasks, so every poll reported exactly 1000 pending — wrong in two directions at once:
processed = 2130 − 1000 = 1130, so the loop decided delivery had started and began the stall clock against a run that had received nothing.Fifteen minutes of that plus the tasks crossing the 30-minute ghost age exited the run 06:45:43 — four seconds past the threshold — with its first result arriving 06:49:52. Three consecutive nights the ledger row was deleted and the dashboard and pulse stayed frozen on an older window while ~1300–1800 results landed on time.
Only the one brand over 1000 tasks was ever affected (next largest: 840), which is why it read as brand-specific rather than as a query bug. Verified against this project's REST endpoint: an un-paginated select returns exactly 1000 rows.
Two smaller holes are closed alongside it, each able to cause the same premature exit on its own:
CLORO_GHOST_TASK_AGE_MIN, default 30 — unchanged behaviour). Its premise is that a task older than the threshold can no longer be in flight, and delivery latency undercuts that today: one brand's first result arrived at 34 minutes, another's at 29.7, seconds under the threshold.Related issue
Closes #715
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
The failure is timing-dependent, so it is covered two ways.
Unit tests (
yarn testfromserver/, 199 passing, 5 new) pin the paging behaviour: a 2130-row set — what the brand actually submits — comes back whole; an 840-row set costs one request; a page that comes back exactly full triggers one more; a failed page returns an error rather than a partial set, since a half-read pending set is exactly the "progress that never happened" this change exists to prevent.Replaying the real numbers from the 13th through both versions of the loop — 2130 tasks, first result at 34.0 min, delivery ending at 57 min, 1310 results, 11 ghosts:
ghosts@ 30.3 mindrained@ 57.0 minIn production after the next nightly cycle: the brand should get a
tracking_runsrow again, the log should carrycloro delivery startedwith awaitedMsnear the real delivery latency rather than near zero, and the drain should finish(drained)rather than(ghosts).Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)