skip to content

A Cloudflare Worker rendering thousands of invoice PDFs a day with Browser Run gets 429 errors at peaks and a browser-hours bill far above its render time; why, and what fixes it?

level: seniorimportance: should knowfreq 25%

answer

  1. launch rate versus concurrency
  2. launch per request
  3. BrowserIdle versus NormalClosure
  4. close in finally, then reuse

basics

~20 s

Each invoice launches a new browser, so peaks exceed the new-browser rate, and error paths skip browser.close(), so browsers idle until their timeout and bill it. Close in finally, reuse sessions, honour Retry-After, or use the /pdf Quick Action.

solid answer

~50 s

Two things are happening. A Worker that calls `puppeteer.launch()` per invoice hits the **new-browser rate** at peaks: on Workers Paid the defaults are 3 new browsers per second and 200 concurrent browsers, and a launch over the rate is answered with `429` and a `Retry-After` header. The bill comes from browsers that are never closed: an exception before `browser.close()` leaves the session open until its idle timeout (60 seconds, or up to 10 minutes with `keep_alive`), and that idle time is billed. `puppeteer.history()` and the dashboard's Runs tab show those sessions as `BrowserIdle` rather than `NormalClosure`. The fix: close in a `finally` block, reuse one browser across invoices with `connect()` and a context per job (tabs count against neither limit), back off on `Retry-After`, and consider the `/pdf` Quick Action, which manages the browser and bills browser hours only.

code

typescript · 16 lines
typescript
import puppeteer from "@cloudflare/puppeteer";

interface Env {
  MYBROWSER: Fetcher;
}

export async function renderInvoicePdf(env: Env, html: string): Promise<Uint8Array> {
  const browser = await puppeteer.launch(env.MYBROWSER);
  try {
    const page = await browser.newPage();
    await page.setContent(html, { waitUntil: "networkidle0" });
    return await page.pdf({ format: "A4", printBackground: true });
  } finally {
    await browser.close(); // runs after a successful render and after any thrown error
  }
}

go deeper

for a junior

Recall that an unclosed browser keeps running until its idle timeout and that the time is billed, so browser.close() goes in finally.

for a middle

Explain the two Browser Sessions limits, concurrency and new browsers per second, and how a launch per request hits the second at peaks.

for a senior

Diagnose from evidence: BrowserIdle close reasons in history(), the Runs tab, limits() fields and Retry-After, then fix with finally, reuse, a queue and back-off.

for a principal

Decide whether invoice rendering should stay on Browser Sessions at all, given that the /pdf Quick Action removes lifecycle code and bills browser hours only.

## Two limits, not one Browser Run limits Browser Sessions (Puppeteer, Playwright, CDP) on two axes, concurrency and launch rate, plus a daily cap on the Free plan: | Limit | Workers Free | Workers Paid (default) | |---|---|---| | Concurrent browsers per account | 3 | 200 | | New browser instances | 1 every 20 seconds | 3 per second | | Browser time | 10 minutes per day | 10 hours a month included, then $0.09 an hour | | Idle timeout | 60 seconds, up to 10 minutes with `keep_alive` | the same | A Worker that launches one browser per invoice spends one **new browser instance** per request. When the morning batch sends forty invoices in a second, the launch rate runs out long before concurrency does: launches over it fail with HTTP `429` and a `Retry-After` header until the rate allows another. On the Free plan a different 429 also exists: `Browser time limit exceeded for today`, which lasts until the next UTC day. Paid limits are defaults. Cloudflare can raise them on request, but that does not fix a design that launches per request. ## Where the extra browser hours come from Browser time counts from launch until the session ends, not just while a page renders. A session ends on `browser.close()`, on an idle timeout, or when Browser Run closes it. Now consider the common bug: ```ts const browser = await puppeteer.launch(env.MYBROWSER); const page = await browser.newPage(); await page.setContent(html); // throws on malformed input const pdf = await page.pdf(); await browser.close(); // never reached on the error path ``` Every failed invoice leaves a browser idling for 60 seconds, or for up to 10 minutes if the team raised `keep_alive` while experimenting. A two-second render can bill a full minute or more. Those idle browsers also occupy concurrency slots. Billing then sums the seconds per day and rounds the monthly total to the nearest hour. An illustrative estimate shows the scale. Suppose 3,000 invoices a day, each rendering in about 3 seconds, and 5% of them failing on malformed input before `browser.close()`: | Sessions | Count per day | Time each | Browser time per day | |---|---|---|---| | Rendered and closed | 2,850 | about 3 s | about 2.4 hours | | Failed and left idle | 150 | about 60 s (the idle timeout) | 2.5 hours | The failures, one invoice in twenty, account for more than half of the bill. With `keep_alive` raised to 10 minutes, the same 150 failures would idle for 25 hours a day. The numbers are made up for illustration, but the shape is the one teams find in their own dashboards. ## Diagnosing it 1. Call `puppeteer.history()` (or `playwright.history()`): each recent session carries a `closeReasonText`. Many `BrowserIdle` entries mean browsers were not closed; `NormalClosure` means `browser.close()` ran. 2. Open the **Runs** tab on the dashboard's Browser Run page, which lists sessions and Quick Actions with their close reasons. 3. Call `puppeteer.limits()`: `allowedBrowserAcquisitions` and `timeUntilNextAllowedBrowserAcquisition` show whether the launch rate is the wall being hit. 4. Read the 429's `Retry-After` header for how long to wait. ## Fixing it - **Close on every path**: wrap the session in `try` / `finally` so `browser.close()` runs after errors too. - **Reuse instead of launching**: keep a browser alive and attach each invoice with `puppeteer.connect()`, giving each job its own browser context. Tabs and contexts inside a session count against neither the concurrency nor the launch limit, though they share the browser's memory. - **Smooth the peaks**: put invoices on a queue and let the consumer render them at a steady rate; the Queues material covers batching and retries. - **Back off**: on a 429, wait for `Retry-After` before trying again instead of retrying at once. - **Shorten idle time**: do not raise `keep_alive` beyond what reuse needs; idle time is billed. ## When the /pdf Quick Action is the better answer An invoice is one HTML string in and one PDF out. The `/pdf` Quick Action, called as `env.BROWSER.quickAction("pdf", { html })`, manages browser lifecycle itself, is billed for browser hours only, and is limited by requests per second (30 on Workers Paid) instead of concurrent browsers. If the Worker only needs that one step, moving to it removes the leak class entirely.

  • Would raising the account's concurrent-browser limit stop these 429s?
    Not necessarily. A per-request launch design at peak is usually limited by the new-browser rate, 3 per second on Workers Paid, not by the 200 concurrent browsers. Leaked idle sessions also eat concurrency. Closing sessions, reusing one browser across jobs and smoothing the peak address the cause; a raised limit only moves the wall.
  • The team upgraded from Workers Free but still sees 'Browser time limit exceeded for today'; what should they do?
    Browser Run's documentation says to redeploy the Worker so its usage is associated with the new plan. The 10-minutes-a-day cap belongs to Workers Free; Workers Paid has no daily cap and bills browser hours beyond the 10 included each month.

  • Browser time counts only while a page is rendering
  • Every Browser Run 429 means the concurrent-browser limit was reached
  • Retrying a 429 at once is fine because limits reset every request
  • A longer keep_alive makes idle browsers cheaper
  • Raising the account's limits fixes a launch-per-request design

Assumes Browser Run limits as of 2026-10-10: on 2026-08-20 the Workers Paid defaults rose to 200 concurrent browsers, 3 new browsers per second and 30 Quick Actions requests per second.