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] ScreenshotAsync timeouts while using the new Chromium headless mode #34515

Open
aleksander-szczepan opened this issue Jan 28, 2025 · 2 comments

Comments

@aleksander-szczepan
Copy link

aleksander-szczepan commented Jan 28, 2025

Version

1.49.0

Steps to reproduce

Execute the following C# code, which is using the new headless mode.
It used to work fine with the old headless mode.

using Microsoft.Playwright;

int width = 468;
int height = 60;
var problematicUrl = "https://api.bannerflow.com/preview/731073/10760085/preview?access-token=[access_token_removed]&snapshot=12689920&autoplay=off&preload=false&seek=preload-frame&fadein=false&env=image-generator";
var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
    {
        Channel = "chromium",
        Args = ["--headless"]
        
    });

var page = await browser.NewPageAsync(new BrowserNewPageOptions
{
    ScreenSize = new ScreenSize
    {
        Height = height,
        Width = width
    }
});

// removing the SetViewportSizeAsync call fixes the timeout issue
await page.SetViewportSizeAsync(width, height);

Console.WriteLine("navigating to url");
await page.GotoAsync(problematicUrl);

Console.WriteLine("waiting 10 seconds for the page to load");
await Task.Delay(10000);

Console.WriteLine("Taking screenshot (this will timeout in 30s)");
_ = await page.ScreenshotAsync();

Console.WriteLine($"screenshot taken!");
await page.CloseAsync();
Console.ReadLine();

project file:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net9.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Microsoft.Playwright" Version="1.49.0" />
    </ItemGroup>

</Project>

Environment

Docker

FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base
WORKDIR /app


ARG BUILD_CONFIGURATION=Release

# Install node so that we can install playwright
RUN apt-get update && apt-get --no-install-recommends install  -y \
	ca-certificates \
	gnupg \
	wget \
	&& rm -rf /var/lib/apt/lists/*

RUN wget -qO- --max-redirect=0 https://deb.nodesource.com/setup_18.x | bash -

RUN apt-get --no-install-recommends install -y nodejs \
	&& rm -rf /var/lib/apt/lists/*

# install playwright
RUN npx [email protected] install  --with-deps chromium --no-shell

RUN mkdir -p /home/app/.cache/ms-playwright
RUN chown -R $APP_UID /home/app/.cache/ms-playwright

RUN cp -r /root/.cache/ms-playwright/* /home/app/.cache/ms-playwright/
RUN chown -R $APP_UID /home/app/.cache/ms-playwright

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY ["PlaywrightIssue.csproj", "./"]
RUN dotnet restore "PlaywrightIssue.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "PlaywrightIssue.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "PlaywrightIssue.csproj" -c Release -o /app/publish /p:UseAppHost=false
RUN chown -R $APP_UID /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
USER $APP_UID

ENTRYPOINT ["dotnet", "PlaywrightIssue.dll"]

Expected behavior

Screenshot is taken

Actual behavior

Unhandled exception. System.TimeoutException: Timeout 30000ms exceeded.
2025-01-28T09:01:10.408058028Z Call log:
2025-01-28T09:01:10.408061179Z   -   - taking page screenshot
2025-01-28T09:01:10.408063638Z   -   - waiting for fonts to load...
2025-01-28T09:01:10.408066003Z   -   - fonts loaded
2025-01-28T09:01:10.408068476Z    at Microsoft.Playwright.Transport.Connection.InnerSendMessageToServerAsync[T](ChannelOwner object, String method, Dictionary`2 dictionary, Boolean keepNulls) in /_/src/Playwright/Transport/Connection.cs:line 206
2025-01-28T09:01:10.408072276Z    at Microsoft.Playwright.Transport.Connection.WrapApiCallAsync[T](Func`1 action, Boolean isInternal) in /_/src/Playwright/Transport/Connection.cs:line 535
2025-01-28T09:01:10.408075101Z    at Microsoft.Playwright.Core.Page.ScreenshotAsync(PageScreenshotOptions options) in /_/src/Playwright/Core/Page.cs:line 690
2025-01-28T09:01:10.408077688Z    at Program.<Main>$(String[] args) in /home/aleks/RiderProjects/PlaywrightIssue/Program.cs:line 33
2025-01-28T09:01:10.408080571Z    at Program.<Main>(String[] args)

Additional context

Works fine when the call to page.SetViewportSizeAsync(width, height) is removed

@agg23
Copy link
Contributor

agg23 commented Jan 28, 2025

You accidentally included an API key in your example URL, I removed it for you.

Can you please provide a public reproducible issue? bannerflow.com appears to be down at the current moment, and in any case I cannot access a URL with your API key.

I also need more information about your working environment. It seems you are using Docker. What image are you using, what OS/version?

It's likely that the issue is related to #33330.

@aleksander-szczepan
Copy link
Author

The provided access-token is valid only for this specific URL, so it's fine to share.
The url works for me, please make sure that you have ad blocker turned off:

https://api.bannerflow.com/preview/718371/10558999/preview?access-token=c1a709be4ab1fcc53b86e9711c7ab71c3862c5f55dc08c228b5bb9a995b2c57c&snapshot=12485003

I have attached the dockerfile in the bug report, it's using
mcr.microsoft.com/dotnet/runtime:9.0

The issue is reproducible while running this docker image both in AKS and Ubuntu 24.04 locally.

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

No branches or pull requests

2 participants