Skip to content

[Bug]: page.screenshot can hang and timeout option doesn't make it timed out #36702

Closed

Description

Version

1.54.1

Steps to reproduce

  1. Clone my repo https://github.com/mtsmfm/playwright-screenshot-hang
  2. npm install
  3. node test.mjs

Expected behavior

I expect page.screenshot({timeout: 1000}) gets timed out after 1000ms with any website

Actual behavior

page.screenshot({timeout: 1000}) can hang if a target website doesn't respond

Additional context

No response

Environment

$ npx envinfo --preset playwright

  System:
    OS: Linux 6.6 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 5.38 GB / 15.54 GB
    Container: Yes
  Binaries:
    Node: 20.18.1 - ~/.local/share/mise/installs/node/20.18.1/bin/node
    npm: 11.1.0 - ~/.local/share/mise/installs/node/20.18.1/bin/npm
    pnpm: 10.5.2 - ~/.local/share/mise/installs/node/20.18.1/bin/pnpm
  IDEs:
    VSCode: 1.102.0 - /home/mtsmfm/.vscode-server/bin/cb0c47c0cfaad0757385834bd89d410c78a856c0/bin/remote-cli/code
  Languages:
    Bash: 5.1.16 - /usr/bin/bash
  npmPackages:
    playwright: ^1.54.1 => 1.54.1

Activity

  1. changed the title [-][Bug]: page.screenshot can hang and `timeout` option doesn't make it timed oout[/-] [+][Bug]: page.screenshot can hang and `timeout` option doesn't make it timed out[/+] on Jul 17, 2025
  2. Skn0tt commented on Jul 17, 2025

    @Skn0tt
    Contributor

    From what I can tell, this behaviour has been in place since at least 1.48. I agree we should throw an error here instead of silently timing out, if possible.

    I can make the error appear on every iteration by lowering the setTimeout to 10ms on my machine. Also, if I insert a 100ms wait after the "Start infinite loop" click, then we're throwing the error correctly. So it looks like the problem is that the event loop gets blocked somewhere in the middle of Playwright taking a snapshot, and we don't race the timeout properly in that place. Like in #19374.

    I'll take a look at what we can do, but this is also a pretty trivial example. Fumiaki MATSUSHIMA (@mtsmfm) could you elaborate on the real-world page you're experiencing this with? Why is it blocking the event loop, and why is it a problem for you that page.snapshot hangs?

  3. Skn0tt commented on Jul 17, 2025

    @Skn0tt
    Contributor

    Here's a minimal reproduction test:

    browserTest('should not hang when event loop is blocked', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36702' } }, async ({ page }) => {
        browserTest.setTimeout(1000);
        await page.evaluate(() => {
          setTimeout(() => {
            while (true) {}
          }, 10);
        });
        await expect(page.screenshot({ timeout: 100 })).rejects.toThrow(/page.screenshot: Timeout 100ms exceeded/);
      });

    We're correctly triggering the timeout, but before we can throw the error we try cleaning up:

    progress.cleanupWhenAborted(() => this._restorePageAfterScreenshot());

    That cleanup call is blocked by the event loop. I'm not sure what we can do here, will discuss with Dima.

  4. mxschmitt commented on Jul 23, 2025

    @mxschmitt
    Contributor

    Relates #13253

  5. mtsmfm commented on Jul 23, 2025

    @mtsmfm
    ContributorAuthor

    Simon Knott (@Skn0tt)

    I'll take a look at what we can do, but this is also a pretty trivial example. @mtsmfm could you elaborate on the real-world page you're experiencing this with? Why is it blocking the event loop, and why is it a problem for you that page.snapshot hangs?

    I can't provide the actual URL as it's not a public website and not owned/maintained by us. But it seems the test target website hangs occasionally. I couldn't find any reliable way to reproduce the hang but I guess if a website doesn't respond (e.g. Aw, Snap! etc) it occurs.

  6. removed their assignment
    on Jul 24, 2025
  7. DirkRichter commented on Aug 20, 2025

    @DirkRichter

    We had similar problems when taking large fullscreen screenshots.
    Reason is a chomium bug: https://issues.chromium.org/issues/41347676
    Solution for us: disable fullPage screenshots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions