puppeteer icon indicating copy to clipboard operation
puppeteer copied to clipboard

[Bug]: Using the sessions API causes hanging queue

Open cloudkite opened this issue 9 months ago • 8 comments

Minimal, reproducible example

import { WorkerEntrypoint } from "cloudflare:workers";
import puppeteer, { type Browser } from "@cloudflare/puppeteer";

export default class extends WorkerEntrypoint<Env> {
  async queue(_batch: Batch) {
    let browser: Browser | null = null;
    try {
      let sessions = await puppeteer.sessions(this.env.BROWSER);
      let sessionIds = sessions.filter((v) => !v.connectionId).map((v) => v.sessionId);
      let sessionId = sessionIds[Math.floor(Math.random() * sessionIds.length)];
      browser = sessionId
        ? await puppeteer.connect(this.env.BROWSER, sessionId)
        : await puppeteer.launch(this.env.BROWSER, { keep_alive: 60_000 * 10 });
    } finally {
      browser?.disconnect();
      browser = null;
    }
  }
}

Expectation

Should complete processing queue.

Reality

It seems like there is something unresolved thats keeping the queue handler from cleaning up and completing.

wall time recorded by worker observability is always very long see below and outcome is either canceled or exceededCPU

{
  "$workers": {
    "diagnosticsChannelEvents": [],
    "truncated": false,
    "event": {
      "queue": "ingestor-load-queue-staging",
      "batchSize": 3
    },
    "scriptName": "ingest-worker-staging",
    "outcome": "canceled",
    "eventType": "queue",
    "scriptVersion": {
      "id": "ef9da0da-763d-4da4-a1a1-c13ac771f1e7"
    },
    "executionModel": "stateless",
    "requestId": "U7MCVAW5DJTLV1LM",
    "wallTimeMs": 318910,
    "cpuTimeMs": 11
  },
  "level": "info",
  "message": "ingestor-load-queue-staging",
  "$metadata": {
    "now": "2025-03-03T00:13:16.867Z",
    "id": "01JNCNJ5FWXYJNH1PB1JHWWQRY",
    "requestId": "U7MCVAW5DJTLV1LM",
    "service": "ingest-worker-staging",
    "message": "ingestor-load-queue-staging",
    "account": "0fdad119d66661d1a9bf49bcca658a9c",
    "type": "cf-worker-event",
    "fingerprint": "ac92b9809358fa0702817fcae5a287d3",
    "origin": "queue"
  }
}

cloudkite avatar Mar 03 '25 00:03 cloudkite

@Refaerds sorry to ping you directly, but it's been over a month with no response from cloudflare. Its pretty much unusable at the moment

cloudkite avatar Apr 03 '25 18:04 cloudkite

hi @cloudkite we've just upgraded our fork to puppeteer v22.8.2, where browser.disconnect() now returns a promise. Try to install @cloudflare/puppeteer latest version and add await to browser.disconnect()

Refaerds avatar Apr 04 '25 17:04 Refaerds

@Refaerds thanks for the reply. I just tried updating to the latest version of @cloudflare/puppeteer and adding an await to browser.disconnect. But it does not fix the issue

see "wallTimeMs": 899977

{
  "source": {
    "level": "info",
    "message": "ingestor-load-queue-staging",
    "$cloudflare": {
      "$metadata": {
        "id": "01JR3QAF80PGWVSS04E7APAXJV",
        "type": "cf-worker-event"
      }
    }
  },
  "dataset": "cloudflare-workers",
  "timestamp": "2025-04-05T19:22:26.031Z",
  "$workers": {
    "diagnosticsChannelEvents": [],
    "truncated": false,
    "event": {
      "queue": "ingestor-load-queue-staging",
      "batchSize": 15
    },
    "scriptName": "ingest-worker-staging",
    "outcome": "exceededCpu",
    "eventType": "queue",
    "scriptVersion": {
      "id": "9526d7f8-d382-4280-90cb-18cfabc15678"
    },
    "executionModel": "stateless",
    "requestId": "2PVXDFN05PRN9CYK",
    "wallTimeMs": 899977,
    "cpuTimeMs": 1297
  },
  "$metadata": {
    "id": "01JR3QAF80PGWVSS04E7APAXJV",
    "requestId": "2PVXDFN05PRN9CYK",
    "trigger": "ingestor-load-queue-staging",
    "service": "ingest-worker-staging",
    "level": "info",
    "message": "ingestor-load-queue-staging",
    "account": "0fdad119d66661d1a9bf49bcca658a9c",
    "type": "cf-worker-event",
    "fingerprint": "ac92b9809358fa0702817fcae5a287d3",
    "origin": "queue"
  },
  "links": []
}

cloudkite avatar Apr 05 '25 21:04 cloudkite

@Refaerds any update on this?

cloudkite avatar Apr 18 '25 23:04 cloudkite

@cloudkite

I encountered the same issue. After many hours of debugging, we finally gave up and switched to browser-rendering the REST API, and so far it’s working without any issues.

For anyone trying to use browser rendering in a queue, here are some things I discovered during debugging:

  1. Wrap everything related to browser in a try-catch block.
  2. Set timeouts. 3.Do NOT ever call .close() inside the queue — it will hang for the next 20 minutes.
  3. Don’t forget to acknowledge messages.

BartoszTrading avatar Jun 15 '25 11:06 BartoszTrading

Enabling the compatibility flag queue_consumer_no_wait_for_wait_until seems to fix this issue.

cloudkite avatar Jul 08 '25 06:07 cloudkite

@cloudkite @BartoszTrading @Refaerds I encountered the same issue, and adding this flag resolved it for me as well. That said, I’m not entirely sure if this is a bug, but ideally the queue should function correctly without requiring any additional flags—this should be the default behavior.

I spent days trying to debug my code, suspecting various unrelated issues. In the end, discovering this flag saved us a significant amount of time and confusion. Without it, we likely would have continued troubleshooting in the wrong direction.

nabualhawa avatar Jul 16 '25 21:07 nabualhawa

We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm or reproduce it. The issue will be closed if no further activity occurs within the next 7 days. cc @puppeteer/bug-triage

github-actions[bot] avatar Sep 16 '25 14:09 github-actions[bot]