Skip to content

fix(pool): a lease ends at its deadline, whether or not its holder returns it - #13

Merged
cycle-five merged 2 commits into
masterfrom
fix/pool-lease-deadline
Sep 19, 2026
Merged

cycle-five merged 2 commits into
masterfrom
fix/pool-lease-deadline

Conversation

@cycle-five

@cycle-five cycle-five commented Sep 19, 2026 •

Copy link
Copy Markdown
Owner

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 /play on 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 /health said ok the 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

produceBudgetMs answered only the caller. withBudget rejected 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's response.json() stays pending for as long as the page lives. The in-page pagination fetch had no bound either. Each such hang lost one context for good, and with POOL_SIZE=2 the second loss emptied the pool. Every request after that waited in acquire() for the whole budget.

The fix

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. Before, it was handed the next free context after it had stopped listening, ahead of callers still waiting.
  • A lease still held at the deadline is revoked. The pool closes the lease's 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 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 fetch both take ENTITY_DATA_TIMEOUT_MS, so a stall now costs seconds and keeps the part of the listing that did arrive.

/metrics gains sleevenote_pool_contexts{state="free"|"leased"} and sleevenote_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's browser-pool, puppeteer-cluster and playwright-cluster were 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.md records why.

Verification

  • 156 passed / 9 skipped (147 at the branch point), tsc --noEmit clean. Each of the 9 new tests was seen failing first, then sabotaged.
  • 🪤 The stalled-body repro needs a real HTTP server, because page.route can 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.
  • Live smoke against real Spotify: 3/3.
  • Running on TuneTitan now as a locally built hotfix image of this exact commit (edab5ef).

Deploy note

Node is PID 1 in the image and never reaps Chromium's exited children, which is where the zombie chrome-headless processes on the bots VM came from. The TuneTitan hotfix adds init: true to 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

…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)
package-lock.json had said 0.1.0 since the first release; it now agrees
with package.json.

Co-Authored-By: Claude & Lothrop (cycle.five@proton.me)
@cycle-five
cycle-five merged commit a8e2ab5 into master Sep 19, 2026
2 checks passed
@cycle-five
cycle-five deleted the fix/pool-lease-deadline branch September 19, 2026 21:24
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