playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG] Page.screenshot method hangs indefinitely

Open mat926 opened this issue 1 year ago • 8 comments

System info

  • Playwright Version: v1.40.0
  • Operating System: All
  • Browser: Chromium
  • Other info: 32GB RAM

Source code

from playwright.sync_api import sync_playwright

print("Starting...")

with sync_playwright() as p:
    try:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
        for index in range(10000):
            print("Iteration:", index)
            page.goto('https://google.com')


    except Exception as error:
        if page:
            print("Error: ", {error})
            page.screenshot(path='error.png', timeout=0)
            print("Took a screenshot")

  • [x] I provided exact source code that allows reproducing the issue locally.

Link to the GitHub repository with the repro

Test file (self-contained)

Steps

  • Run the code
  • Wait until the for loop crashes at about 2000-3000 iterations

Expected

The for loop runs all the way through and not crash. The page.screenshot runs normally.

Actual At the 2652 iteration, a Timeout exception gets caught. Chrome gets an Out of Memory error. The code hangs at the page.screenshot line and never completes.

Screenshot

image

mat926 avatar Jan 13 '24 02:01 mat926

I can repro, hangs for me with:

await page.goto("chrome://crash").catch(e => console.log(e));
await page.screenshot({ path: `crash.png` })

at:

   pw:api waiting for fonts to load... +250ms

mxschmitt avatar Jan 16 '24 19:01 mxschmitt

I have the same issue, when I pass PLAYWRIGHT_CHROMIUM_USE_HEADLESS_NEW=1 (which is very similar to headless=False, if I'm not mistaken).

And this issue has probably the same root cause: https://github.com/microsoft/playwright/issues/29968

If you have an idea how to somehow force a timeout on loading the fonts?

Rafiot avatar Mar 21 '24 17:03 Rafiot

Hi. I'm facing similar issue - sometimes my tests are getting stuck when i'm tryigng to take screenshot. I'm using Playwright 1.42.0. I've investigated logs using DEBUG=pw:* and i can see that right after calling page.screenshot playwright logs

pw:api waiting for fonts to load...

which is followed by a bunch of pw:protocol SEND {"id": XYZ (...) "document.fonts.ready" (...)

usually these SEND requests recieve immediate response like:

pw:protocol RECV {"id" XYZ (...)

but sometimes it takes several minutes - this is clearly causing problems and leads to hanging screenshot method.

Ofc screenshot method throws a timeout but subsequent screenshot calls from the same browser context seem to be affected and it's basically not possible to capture screenshot anymore.

It seems to be somewhat related with #29968

I hope that this will help to find the root cause and fix the problem...

LucaLis avatar Apr 09 '24 12:04 LucaLis

I have a similar issue. In "headless" mode there is no problem, but in "headed" mode every test stucks at taking a screenshot (interestingly, a week ago it was working more or less fine - the tests only stucked on one screenshot and all you had to do was give locator.focus() there, but it is not working).

Now, in my case, a workaround works - I gave this before each test

test.beforeEach(async ({ page }) => {
    await page.route('**/*.css', (route) => { route.abort(); });
});

and it looks like everything is working fine.

marian51 avatar Apr 23 '24 11:04 marian51

I am also experiencing this "waiting for fonts to load" bug in headed mode that leads to a hanging page.screenshot command, but can confirm that in headless mode everything works fine. However in some cases I need to be in headed mode -- is there any way to fix this bug?

jonisrael avatar Apr 24 '24 21:04 jonisrael

except Exception as error:

      raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None

E Call log: E taking page screenshot E - waiting for fonts to load... E - fonts loaded

Still getting this issue.. the same methods works in some pages of headed, but stucks at some of the new test cases.

BaseerAzhar avatar May 01 '24 02:05 BaseerAzhar

Was getting this too (and only on some pages, while on others worked)... Setting the env variable PW_TEST_SCREENSHOT_NO_FONTS_READY to 1 seems to have fixed the problem for me.

Found out about it by inspecting the screenshotter code at: https://github.com/microsoft/playwright/blob/c3d8b22198244a692eb059db47c7d80bc9eefed5/packages/playwright-core/src/server/screenshotter.ts#L267

azrafe7 avatar May 01 '24 14:05 azrafe7

It would be really amazing to have a list of these env variables as they're pretty amazing but you only find them if you dig through the source code.

I can also confirm is solves my problems.

Rafiot avatar May 01 '24 21:05 Rafiot

Worked for me as well thank you @azrafe7

charlesloubao avatar Jun 04 '24 12:06 charlesloubao

@Rafiot @charlesloubao would it be possible to share a repro with us?

mxschmitt avatar Jun 04 '24 17:06 mxschmitt

I get that fairly consistently on liberation.fr and trigger a full page screenshot (not just the viewport).

Rafiot avatar Jun 05 '24 10:06 Rafiot

@Rafiot could you help us a with a repro? I tried the following and it works for me:

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://liberation.fr');
  await page.waitForTimeout(10_000);
  await page.screenshot({ fullPage: true, path: 'screenshot.png' });

  await context.close();
  await browser.close();
})();

mxschmitt avatar Jun 06 '24 20:06 mxschmitt

I'll not have time to work on that today, sorry. Hopefully early next week. If that's important, I'm using the new headless (which is very similar to headfull, afaict).

Rafiot avatar Jun 07 '24 15:06 Rafiot

I don't seem to be able to reproduce it anymore.

Rafiot avatar Jun 12 '24 13:06 Rafiot

Glad someone else managed to give you a proper test, and I confirm I can reproduce too (even without slomo setting).

I'm super curious to know what's causing that.

Rafiot avatar Jun 13 '24 09:06 Rafiot

@pavelfeldman as you have implemented this "wait for fonts" stuff in #28226, can you briefly explain how it's supposed to work?

derdeka avatar Jun 13 '24 13:06 derdeka

We landed a fix in Canary, feel free to try or wait for 1.45. I'll close it for now, for further bug reports in Canary / 1.45 (to be released) please file new bug reports - thank you for your understanding!

mxschmitt avatar Jun 14 '24 05:06 mxschmitt