Repository navigation
fix(pool): a lease ends at its deadline, whether or not its holder returns it - #13
Merged
Merged
Conversation
…turns it
produceBudgetMs used to answer only the caller: withBudget rejected on time
and trusted the work to finish and release on its own. One step never
finished. `await Promise.all(bodies)` waits for every JSON body the page
started, and a body whose headers arrive but whose stream stalls leaves
Playwright's response.json() pending for as long as the page lives. Each
such hang lost one context for good.
Production, 2026-09-19: the second loss emptied the pool of two. Every
request after it, a single track included, waited in acquire() for the
whole budget and came back 504 after exactly 150s, while /health said ok,
until a restart. The container had seen exactly two timeouts, one per lost
context.
extract() now makes one AbortSignal from the budget and hands it to
acquire(). The pool uses it three ways:
- A queued caller whose deadline passes leaves the queue, rather than being
handed the next free context after it has stopped listening.
- A lease still held at the deadline is revoked. The pool closes its
context, which rejects every Playwright call pending on it, and refills
the slot without waiting for the holder. The holder's own release() is
then a no-op.
- A context close is abandoned after CONTEXT_CLOSE_TIMEOUT_MS (default 5s),
so a wedged renderer cannot hold a release open instead.
The two unbounded waits also get bounds: the body wait and each pagination
window's in-page fetch both take ENTITY_DATA_TIMEOUT_MS. A stall now costs
seconds and keeps the listing that did arrive. /metrics gains
sleevenote_pool_contexts{state} and sleevenote_pool_waiting, read from the
pool at scrape time.
generic-pool, tarn, Crawlee's browser-pool, puppeteer-cluster and
playwright-cluster were checked before writing this. None takes a resource
back from a borrower that hangs without either a Puppeteer rewrite or a
context per job. docs/design-notes.md records why.
Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
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.
Bumps to 0.4.1. This is the first of two stacked PRs; the second, the browser context manager (0.5.0), builds on this one.
The incident
Production, 2026-09-19. Every cracktunes
/playon a Spotify link hung on "Searching..." for 2.5 minutes, then failed. Sleevenote's log showed a 504 after almost exactly 150 000 ms on every request, a single track included. The container sat near 0% CPU, and/healthsaidokthe whole time. A restart cleared it; Redis is a separate container, so the cache survived.The pool had starved. Its two contexts had been lost one at a time and never returned: one on 09-15 at 00:29, the other on 09-19 at 11:59. The container had logged exactly two
ExtractionTimeoutErrors since it started, one per lost context.Root cause
produceBudgetMsanswered only the caller.withBudgetrejected on time, then trusted the work to finish and release its lease on its own. One step never finished.await Promise.all(bodies)waits for every JSON body the page started. When a body's headers arrive but its stream stalls, Playwright'sresponse.json()stays pending for as long as the page lives. The in-page paginationfetchhad no bound either. Each such hang lost one context for good, and withPOOL_SIZE=2the second loss emptied the pool. Every request after that waited inacquire()for the whole budget.The fix
extract()now makes oneAbortSignalfrom the budget and hands it toacquire(). The pool uses it three ways:release()is then a no-op.CONTEXT_CLOSE_TIMEOUT_MS(default 5 s), so a wedged renderer cannot hold a release open instead.The two unbounded waits get bounds. The body wait and each pagination window's in-page
fetchboth takeENTITY_DATA_TIMEOUT_MS, so a stall now costs seconds and keeps the part of the listing that did arrive./metricsgainssleevenote_pool_contexts{state="free"|"leased"}andsleevenote_pool_waiting, read from the pool at scrape time. The next starvation shows up on a graph instead of in a user report.Why not a library.
generic-pool,tarn, Crawlee'sbrowser-pool,puppeteer-clusterandplaywright-clusterwere checked first. None takes a resource back from a borrower that hangs, short of a Puppeteer rewrite or a context per job.docs/design-notes.mdrecords why.Verification
tsc --noEmitclean. Each of the 9 new tests was seen failing first, then sabotaged.page.routecan only fulfil whole bodies. Its first draft passed on the broken code: its 500 ms entity wait expired before the fetches began, so it never reached the body wait. Raising that wait to 2500 ms made it fail on the old code and pass on the fix.edab5ef).Deploy note
Node is PID 1 in the image and never reaps Chromium's exited children, which is where the zombie
chrome-headlessprocesses on the bots VM came from. The TuneTitan hotfix addsinit: trueto the service. The homelab compose files should carry it permanently along with the pin bump.Stacked on this
#14, the browser context manager (0.5.0), builds on this PR: crash relaunch, blue/green recycling and load shedding. It targets this branch. Once this one merges I'll rebase it onto master and retarget it, so each lands as its own commit.
🤖 Generated with Claude Code
https://claude.ai/code/session_01F8PrF3gizKqXCStjCuWL3d