Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Page.screenshot method hangs indefinitely #28995

Closed
1 task done
mat926 opened this issue Jan 13, 2024 · 19 comments
Closed
1 task done

[BUG] Page.screenshot method hangs indefinitely #28995

mat926 opened this issue Jan 13, 2024 · 19 comments

Comments

@mat926
Copy link

mat926 commented Jan 13, 2024

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")

  • 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

@mxschmitt mxschmitt transferred this issue from microsoft/playwright-python Jan 15, 2024
@mxschmitt
Copy link
Member

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

@Rafiot
Copy link

Rafiot commented Mar 21, 2024

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: #29968

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

@LucaLis
Copy link

LucaLis commented Apr 9, 2024

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...

@marian51
Copy link

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.

@jonisrael
Copy link

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?

@BaseerAzhar
Copy link

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.

@azrafe7
Copy link

azrafe7 commented May 1, 2024

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:

if (!process.env.PW_TEST_SCREENSHOT_NO_FONTS_READY) {

@Rafiot
Copy link

Rafiot commented May 1, 2024

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.

@charlesloubao
Copy link

Worked for me as well thank you @azrafe7

@mxschmitt
Copy link
Member

mxschmitt commented Jun 4, 2024

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

@Rafiot
Copy link

Rafiot commented Jun 5, 2024

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

@mxschmitt
Copy link
Member

@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();
})();

@Rafiot
Copy link

Rafiot commented Jun 7, 2024

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
Copy link

Rafiot commented Jun 12, 2024

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

@Rafiot
Copy link

Rafiot commented Jun 13, 2024

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.

@derdeka
Copy link

derdeka commented Jun 13, 2024

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

mxschmitt added a commit that referenced this issue Jun 13, 2024
… frame / page main frame (#31295)

**Investigation**

~~We use `nonStallingEvaluateInExistingContext` as of today, which does
`eval()` inside (from our utilityScript) which breaks for some sites. It
causes a hang, since the returned `Promise` of `eval()` hangs. We don't
know as of today why this happens. Without wrapping it ini `eval()` it
does not hang.~~

~~Workaround: Do a plain Runtime.evaluate instead.~~

workaround: Only wait on main frame.

Relates #28995 (keeping it
open until they confirm that it helps)
@mxschmitt
Copy link
Member

mxschmitt commented Jun 14, 2024

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!

@coader
Copy link

coader commented Oct 29, 2024

I use 1.48 but still got this error

@You-Honey
Copy link

I use "@crawlee/playwright": "3.9.1" and have the problem. @azrafe7's fix works though.

Ping @mxschmitt

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

No branches or pull requests