Skip to content

fix(batch): stop timed-out places, handle SIGTERM, keep collection fast - #34

Merged
marcinobel merged 1 commit into
developfrom
feature/issue-33-batch-timeout
Nov 20, 2026
Merged

marcinobel merged 1 commit into
developfrom
feature/issue-33-batch-timeout

Conversation

@marcinobel

Copy link
Copy Markdown
Member

Summary

Fixes #33: revcli batch can now finish long lists unattended.

  • A place that runs past --location-timeout is really stopped, its browser is closed, and only then does the next place start.
  • kill <pid> (SIGTERM) closes the browser and ends revcli with exit 143 (SIGHUP 129 when attached to a terminal, Ctrl+C 130).
  • Large places no longer slow down: each collect cycle reads only the review cards not collected yet.
  • Output and state files are written atomically.

This PR also contains revcli scrape --time-limit <minutes> (stop collecting after N minutes and save the partial result), which was developed in the same working tree.

Root causes (verified)

  1. batch.ts used Promise.race(scrape, timer): the losing scrape kept running and its persistent Chromium kept ~/.revcli/chrome-profile locked, so the next place failed. The timer was never cleared either.
  2. Playwright installs its own SIGTERM/SIGHUP handlers while a browser runs (playwright-core/lib/coreBundle.js, sigtermHandler): they close browsers but never exit, and their presence turns off Node's default exit on SIGTERM. That is why kill left revcli running.
  3. Every collect cycle re-read and re-parsed every review card in the DOM and sent the whole collected-id list to the expand scan, so cycle cost grew with the list (hypothesis in the issue, confirmed by measurement below).

Approach and assumptions

  • Cancellation uses the built-in AbortSignal: runWithTimeout() (src/core/timeout.ts) aborts the signal; withRetry stops retrying; withBrowserSession() closes the browser on abort, so every pending Playwright call rejects. The batch waits for the stopped scrape to settle (15 s grace). If it does not settle, the batch stops (BROWSER_NOT_CLOSED, exit 1) instead of starting a second browser on the same profile.
  • closeBrowser() shares one close per browser: a second context.close() returns at once while the first is still running, which would let the next place start too early.
  • revcli launches with handleSIGINT/handleSIGTERM/handleSIGHUP: false and installs its own handlers from src/index.ts (SIGHUP only when attached to a terminal, so nohup keeps working).
  • Collected cards get data-revcli-collected="<review id>"; a card is skipped only while the mark equals its current review id, so an element Google reuses for another review is read again. Nothing that was collected before is skipped (the 2,000-review test asserts all 2,000 are collected).
  • Assumption: in headed mode, interactive sign-in time counts against --location-timeout (documented in CHANGELOG/AGENTS.md).

Acceptance criteria

# Criterion Evidence
1 Timed-out scrape stops, browser closed before the next place tests/batch-run.test.ts (event order "Slow started → Slow browser closed → Fast started"); tests/browser/abort.test.ts (real persistent browser on a temp profile: abort mid-scroll rejects < 5 s, a second browser then opens the same profile); live run below
2 Place 1 times out, place 2 scraped and saved tests/batch-run.test.ts (files written for places 2 and 3, none for place 1, state lists them); live run below
3 SIGTERM closes every browser, non-zero exit within 10 s tests/browser/shutdown.test.ts (real child process + Chromium on a temp profile: exit 143 < 10 s, pgrep finds no Chromium); live run below
4 2,000-review fixture: last 10 cycles ≤ 2× first 10 tests/browser/scroll.test.ts: before first 10 = 177ms, last 10 = 970ms (5.5×); after first 10 = 86ms, last 10 = 138ms. Also a timing-free check: every cycle reads ≤ 100 cards
5 New tests fail before the fix, pass after Red runs on the unfixed code (seams first, behaviour unchanged): batch order expected [ 'Slow started', 'Fast started' ] to deeply equal [ 'Slow started', …(2) ]; SIGTERM expected 'timeout' to be 143; AC4 expected 970 to be less than or equal to 354; retry/shutdown tests failed against stubs. All pass after the fix

Checks run

$ npm run typecheck
> tsc --noEmit
(exit 0)

$ npm test
 Test Files  33 passed (33)
      Tests  470 passed (470)

Baseline before the change: 25 files, 420 tests, all passing. No existing test was changed, skipped or weakened (tests/retry.test.ts and tests/browser/scroll.test.ts only gained tests).

Live run (macOS, headless, signed-in profile)

Batch with a 1,673-review place first and a 27-review place second, --headless --delay 1000 --location-timeout 60000 --location-delay 1000:

 ERROR  [1/2] <place 1>: Location timeout after 60s
ℹ Found: <place 2> (header reports 27 reviews)
ℹ No new reviews after scrolling – no more reviews available (27 total)
✔ [2/2] <place 2>: 27 reviews
ℹ Batch complete: 1 succeeded, 1 failed in 92.9s
--- files: <place-2>.json (27 reviews); .revcli-state.json lists place 2 only
--- revcli browsers still running: none

kill <revcli pid> during scrolling:

--- before kill, revcli browser processes: 2
revcli gone after 0.1 s
exit code: 143
--- after kill, revcli browser processes: 0

Note: the issue's step 3 uses -o <dir>; batch takes -d, --output-dir.

Not done / follow-ups

  • With the default --delay 3000, the end-of-list detection (6 stale scrolls with back-off) takes ~60 s per place, so a small place cannot finish within a 60 s location timeout. Pre-existing, related to Improve stale scroll detection with backoff and loading spinner awareness #2; not changed here. The live run above used --delay 1000.
  • Tech debt: a signal that lands between writing a place's output file and writing the state file means --resume scrapes that place again; a collected card that Google re-renders as a new element may get a stuck "More" button clicked again (≤ 2 clicks per cycle); launchBrowser's close-on-partial-launch path has no test.
  • Pre-existing npm audit findings (2 critical / 5 high, mostly in the vitest/vite dev chain; zod high in production deps, fix needs a major upgrade). No dependency changed in this PR.

Reused code

None from outside the repo. The atomic write follows the existing state-file pattern in batch.ts; the test child reuses browserLaunchOptions().

Issue #33: `revcli batch` could not finish long lists unattended.

- A location timeout now really stops the scrape: runWithTimeout() aborts
  a signal that withRetry and scrapeLocation honour, closes the browser,
  and waits until it is closed before the next place starts. A late result
  is never saved. If a stopped browser does not close within 15 s, the
  batch stops with exit 1 (resume with --resume).
- revcli owns SIGHUP/SIGINT/SIGTERM: Playwright's own handlers (whose
  SIGTERM handler closed browsers without exiting) are turned off; a signal
  aborts running work, closes every browser and exits 129/130/143.
- Collected review cards are marked with their review id and skipped, so a
  collect cycle reads only new cards (last/first 10 cycles at 2,000 reviews:
  970/177 ms before, ~140/85 ms after). A reused card is read again.
- Output and state files are written atomically (temp file + rename).

Also includes `revcli scrape --time-limit <minutes>`: stop collecting after
the given time and save the reviews collected so far, with a warning.

Closes #33
@marcinobel
marcinobel merged commit c7eb9c5 into develop Nov 20, 2026
1 check passed
@marcinobel
marcinobel deleted the feature/issue-33-batch-timeout branch November 20, 2026 08:08
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.

1 participant