[BUG] Page.screenshot method hangs indefinitely
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
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
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?
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...
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.
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?
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.
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
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.
Worked for me as well thank you @azrafe7
@Rafiot @charlesloubao would it be possible to share a repro with us?
I get that fairly consistently on liberation.fr and trigger a full page screenshot (not just the viewport).
@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();
})();
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).
I don't seem to be able to reproduce it anymore.
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.
@pavelfeldman as you have implemented this "wait for fonts" stuff in #28226, can you briefly explain how it's supposed to work?
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!