fix: unstick the content pipeline, the quota loop and the outcome retries

Three separate things had the pipeline frozen for 27 hours.

browserCrawler leaked page slots. context.newPage() sat outside the try, so a
throw or a hang there took the slot with it, and after maxConcurrentPages of
those every caller parked in acquirePageSlot forever. That is what it looked
like from outside: content workers alive, no logs, no progress, 13 chromium
renderers still up 10 hours after start. newPage is inside the try now, waiting
for a slot times out instead of blocking forever, and page.close() is raced so a
wedged renderer cant strand the slot on the way out either.

graphWorker had no backoff on quota failures. A blown OpenRouter monthly limit
returns an instant 403, so it retried as fast as the network allowed: 2356
failures in 20 minutes, drowning every other line in the log. Quota and auth
errors now pause resolution for 15 minutes and log once per window rather than
once per attempt.

The outcome worker retried unresolvable predictions forever. Yahoo writes class
shares with a dash, so BRK.B 404s every time, and a failed prediction stays open
and comes straight back on the next poll. Dots are translated to dashes, which
matters beyond this one name because the allowlist is full of dotted symbols,
and a prediction that fails five times is marked unresolvable instead of
spinning.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WnNxwxfXSbeNtjvtz5gayb
This commit is contained in:
ImBenji
2026-08-31 10:02:04 +01:00
co-authored by Claude Opus 5
parent b8b3987e35
commit 42fb9291b0
4 changed files with 93 additions and 10 deletions
+34 -6
View File
@@ -3,6 +3,11 @@ const { chromium } = require('playwright');
const BROWSER_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36';
const MAX_RENDERED_HTML_LENGTH = 1_500_000;
const DEFAULT_REQUEST_TIMEOUT = 20000;
// generous, this is the "something has gone wrong" bound rather than a normal wait
const PAGE_SLOT_WAIT_MS = 120000;
const PAGE_CLOSE_TIMEOUT_MS = 10000;
function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); }
const CONSENT_BUTTON_SELECTORS = [
'button[name="agree"]',
'input[name="agree"]',
@@ -104,15 +109,31 @@ async function buildBrowserSession(options = {}) {
let activePages = 0;
let closed = false;
async function acquirePageSlot() {
// A slot is never waited on forever. If every slot has leaked the callers used
// to park here silently with no log and no progress, which looks exactly like a
// dead worker, so time out and let the caller fail loudly instead.
async function acquirePageSlot(waitMs = PAGE_SLOT_WAIT_MS) {
if (activePages < maxConcurrentPages) {
activePages += 1;
return;
}
await new Promise((resolve) => {
waiters.push(resolve);
});
let waiter;
let timer;
try {
await new Promise((resolve, reject) => {
waiter = resolve;
waiters.push(waiter);
timer = setTimeout(() => {
const index = waiters.indexOf(waiter);
if (index !== -1) waiters.splice(index, 1);
reject(new Error(`timed out after ${waitMs}ms waiting for a browser page slot`
+ ` (${activePages}/${maxConcurrentPages} active)`));
}, waitMs);
});
} finally {
clearTimeout(timer);
}
activePages += 1;
}
@@ -142,10 +163,13 @@ async function buildBrowserSession(options = {}) {
}
await acquirePageSlot();
const page = await context.newPage();
// newPage() used to sit out here. When it threw or hung the slot was gone for
// good, and after maxConcurrentPages of those every caller blocked forever.
let page = null;
const timeout = normalizeTimeout(options.timeout || requestTimeout);
try {
page = await context.newPage();
await page.goto(url, {
waitUntil: 'domcontentloaded',
timeout,
@@ -167,7 +191,11 @@ async function buildBrowserSession(options = {}) {
return html;
} finally {
try {
await page.close();
// a wedged renderer can make close() hang too, and that would strand the
// slot just as badly as the original leak did
if (page) await Promise.race([page.close(), sleep(PAGE_CLOSE_TIMEOUT_MS)]);
} catch (error) {
console.error(`[browser] page close failed for ${url}:`, error.message);
} finally {
releasePageSlot();
}