Skip to content

fix(run): count a failed re-extraction in both front-ends - #204

Open
harsh-thakkar7 wants to merge 1 commit into
LeyckerS:mainfrom
harsh-thakkar7:test/reextract-accounting
Open

harsh-thakkar7 wants to merge 1 commit into
LeyckerS:mainfrom
harsh-thakkar7:test/reextract-accounting

Conversation

@harsh-thakkar7

Copy link
Copy Markdown
Contributor

Closes #178

The repro first, as the issue asks

The issue presumed a live repro (a fake host that stalls once, then errors on re-extraction) was the first half of the work. tests/test_reextract_accounting.py is exactly that: a loopback aiohttp host that trickles at ~5 KB/s against a declared 65536-byte body, extraction stubbed at the moon_extract boundary to succeed once and fail on the re-extraction. The stall constants are monkeypatched down (grace 90s → 1s, 50 MB file gate → 0, 4 MB read chunk → 1 KB) so production's detector fires inside a test; everything else — including the real download_file and its stall kill — is the shipping code path.

Proven red on main: the engine test times out asserting _running is still set after 15 s ("the run never finished: the failed re-extraction was never counted and the run waited for it forever"), and the CLI test observes four extraction attempts behind 1s/2s/4s of backoff before the link is counted at all.

What the repro found that the issue's call-site reading did not

The issue says both front-ends wait forever. The engine does. The CLI does not, and the difference matters for the fix:

  • Engine: the fail branch's not is_re excludes the record, no other mark_done() is reachable, n_done never reaches the link count → hang. As described.
  • CLI: rec.stall_kills is never incremented on a stall kill (only the engine does that, at moon_engine.py:255), so is_re is dead code there. A re-extraction is re-queued as if it were a first attempt and burns the full retry budget — three extra extraction attempts behind 7 s of backoff — before it is counted. No hang, but exactly the "burns the retry budget twice" outcome the guard exists to prevent, plus the report's Stall kills sum permanently reads 0 for CLI runs.

So the fix is two changes per front-end, not one.

Change (3 hunks, no behaviour change beyond the accounting)

moon_engine.py — the fail branch (one line plus comment): drop not is_re. A failed re-extraction is counted as one fail, lands in failed_urls, and gets its single terminal mark_done_fn(). The retry branch above still excludes is_re, so it never burns the budget twice.

moon_cli.py — the stall branch: increment rec.stall_kills += 1, mirroring the engine's kill site. This is what makes is_re true at all in the CLI, which fixes the report field and the retry-budget burn.

moon_cli.py — the fail branch: drop not is_re and route the counter by is_re. This second part is not cosmetic: mark_extraction is a no-op for an already-counted record (guard at moon_engine.py:169), and a re-extraction was already counted as extracted on its first pass — routing it through mark_extraction leaves _dl_done short of _dl_total, all_done unset, and the CLI hanging via a different mechanism. The terminal count for a re-extraction is mark_download_end(False); a first-attempt extraction failure still uses mark_extraction. I found this by watching the CLI test stay red after the naive one-line fix — it is why the repro came first.

Design argument (per the issue: count the drop as a fail)

Over giving re-extractions their own retry budget: a re-extraction that fails has already spent its stall kills and its retries on the first pass; re-budgeting it lets one record occupy a worker lane roughly twice as long as any other, and the only thing actually broken here is the accounting — the record reaches a terminal state and nobody tells the run. Nothing about download behaviour needs to change for the hang to go away, so I changed nothing about download behaviour.

Verification

  • Both tests red on main (hang after 15 s / four attempts behind backoff), green with the fix — the same run of pytest tests/ -q flips 2 → 0 failures
  • pytest tests/ -q → 172 passed (170 before), no browser, no network beyond loopback; the new file adds ~3 s
  • ruff check . → clean
  • No other test moved, including test_no_chrome.py's extraction-failure and retry assertions, which share these branches

Honest notes

  • A counter asymmetry I chose not to hide: for a failed re-extraction the engine leaves dl_done short (it counts no download end — consistent with its own first-attempt extraction failures, where dl_done is also 0), while the CLI's mark_download_end(False) increments it. The CLI has no choice: mark_extraction cannot count the record twice. The engine could _inc("_dl_done") for symmetry; I left it because the engine's own convention for an extraction-time terminal state is dl_done == 0, and changing it would move numbers in the GUI for a case this issue is not about.
  • One pre-existing wart found in passing, not touched: moon_engine.py:396 increments _url_done for every dequeued entry, so a stall-killed link increments it twice (once per pass) against _url_total. That inflates extract_done in the GUI for every stall kill. It is a separate bug from the accounting hang and fixing it would change snapshot numbers, so it is out of scope here — flagging it so it is not lost.
  • Not verified: a real run against a genuinely stalling host (the 90 s grace makes that a manual exercise). The constants patched for the test are the production detector's own, and the kill/re-queue/fail path itself is unmodified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

a stall-killed link whose re-extraction fails is never counted — both front-ends wait forever

1 participant