hardkoded / puppeteer-sharp

Headless Chrome .NET API
https://www.puppeteersharp.com
MIT License
3.41k stars 442 forks source link

PuppeteerSharp.ProcessException: Failed to launch browser! #2593

Open jfgreco opened 7 months ago

jfgreco commented 7 months ago

Description

Trying to download and run a browser is resulting in an exception being thrown.

Code being called

using var browserFetcher = new BrowserFetcher(new BrowserFetcherOptions 
            { 
                Platform = Platform.MacOSArm64,
            });
            var revision =await browserFetcher.DownloadAsync("123.0.6312.86");
            await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                ExecutablePath = revision.GetExecutablePath(),
                Headless = true
            });

Expected behavior:

Launch browser and connect to page

Actual behavior:

PuppeteerSharp.ProcessException: Failed to launch browser! at PuppeteerSharp.States.ProcessStartingState.StartCoreAsync(LauncherBase p) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/States/ProcessStartingState.cs:line 83 at PuppeteerSharp.States.ProcessStartingState.StartCoreAsync(LauncherBase p) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/States/ProcessStartingState.cs:line 89 at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Launcher.cs:line 65 at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Launcher.cs:line 98

Versions

.Net 8.0.3 Pupeteer.Sharp - Tried 15, 15.1 & 16 all with the same result.

kblok commented 7 months ago

What’s the inner exception?

jfgreco commented 7 months ago

It is null:

image

kblok commented 7 months ago

It seems it’s unabled to launch the process. Could you try running what’s in the executable path manually? Another chance is that the download is corrupted. Would could try deleting the directory and force the download again.

jfgreco commented 7 months ago

It doesn't seem to be downloading correctly. I tried to override the path as well to save it somewhere else. I additionally tried to installed Chromium via brew and use that in place of downloading. It will only error no matter what I do. This code was working just the other day, that is what is puzzling.

sdzunenko commented 7 months ago

Have absolutely same error, running it from Azure App service (linux). Seeing that download executed correctly, but failed to run downloaded chrome itself.

PuppeteerSharp.ProcessException:
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at PuppeteerSharp.States.ProcessStartingState+<StartCoreAsync>d__6.MoveNext (PuppeteerSharp, Version=16.0.0.0, Culture=neutral, PublicKeyToken=db12f80f85d8ba23: /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/States/ProcessStartingState.cs:83)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at PuppeteerSharp.States.ProcessStartingState+<StartCoreAsync>d__6.MoveNext (PuppeteerSharp, Version=16.0.0.0, Culture=neutral, PublicKeyToken=db12f80f85d8ba23: /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/States/ProcessStartingState.cs:89)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at PuppeteerSharp.Launcher+<LaunchAsync>d__8.MoveNext (PuppeteerSharp, Version=16.0.0.0, Culture=neutral, PublicKeyToken=db12f80f85d8ba23: /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Launcher.cs:66)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at PuppeteerSharp.Launcher+<LaunchAsync>d__8.MoveNext (PuppeteerSharp, Version=16.0.0.0, Culture=neutral, PublicKeyToken=db12f80f85d8ba23: /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Launcher.cs:99)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
jfgreco commented 7 months ago

@kblok with confirmation of the issue from @sdzunenko any way we can get this resolved?

tnurmi82 commented 2 months ago

Same exception here. I'm trying to use PuppeteerSharp in AWS AppRunner (Linux). Any news, anyone?

kblok commented 2 months ago

@tnurmi82 can you share the innerException of the error you're getting?

tnurmi82 commented 2 months ago

@kblok PuppeteerSharp.ProcessException: Failed to launch browser! /app/out/Chrome/Linux-127.0.6533.88/chrome-linux64/chrome: error while loading shared libraries: libdbus-1.so.3: cannot open shared object file: No such file or directory

kblok commented 2 months ago

@tnurmi82 some googling shows that you might need to install some packages https://stackoverflow.com/questions/25829915/libdbus-1-so-3-cannot-open-shared-object-file-no-such-file-or-directory

tnurmi82 commented 2 months ago

@kblok seems so indeed. I just kind of assumed that PuppeteerSharp NuGet package itself contains all that is needed. I'll have to study how I can install additional stuff in AWS AppRunner.

kblok commented 2 months ago

@kblok seems so indeed. I just kind of assumed that PuppeteerSharp NuGet package itself contains all that is needed. I'll have to study how I can install additional stuff in AWS AppRunner.

PuppeteerSharp will help you with the browser download. But there are thousands of scenarios to make a browser work. And it's even more complex when you want to run a browser in the cloud. Each scenario is different. Having a good docker image will help you with that.

tnurmi82 commented 2 months ago

Thanks @kblok , I'm starting to see this might require some effort to get it going in AWS AppRunner. I found this for material: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix

tnurmi82 commented 2 months ago

Hi again @kblok - I've managed to install all dependencies required by the Chrome that is automatically downloaded by PuppeteerSharp. But I'm still stuck at "Failed to launch browser!" which now happens due to "httpConnect"" symbol missing.

2024-08-30 11:23:50.0901|ERROR|Microsoft.AspNetCore.Server.Kestrel|Connection id "0HN68SLPBB9LF", Request id "0HN68SLPBB9LF:00000002": An unhandled exception was thrown by the application.|PuppeteerSharp.ProcessException: Failed to launch browser! /app/out/Chrome/Linux-127.0.6533.88/chrome-linux64/chrome: symbol lookup error: /app/out/Chrome/Linux-127.0.6533.88/chrome-linux64/chrome: undefined symbol: httpConnect2

   at PuppeteerSharp.States.ProcessStartingState.StartCoreAsync(LauncherBase p) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/States/ProcessStartingState.cs:line 83
   at PuppeteerSharp.States.ProcessStartingState.StartCoreAsync(LauncherBase p) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/States/ProcessStartingState.cs:line 89
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Launcher.cs:line 72
   at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Launcher.cs:line 105

I'm not really a Linux guy so I'm somewhat struggling trying to undestand this error. Is this purely a Chrome-related issue, or does PuppeteerSharp have something to do with this?

EDIT: Well of course just after commenting I found this: https://issues.chromium.org/issues/357648039

kblok commented 2 months ago

@tnurmi82 according to that link it seems to be a bug in Chrome. Maybe you could try an older version?

tnurmi82 commented 2 months ago

@kblok yeah: I tried with newer 128.0.6613.113 but funnily enough it suffers from another missing symbol: ippValidateAttributes. I guess I'll need to try some older versions and hopefully find a working one.

kblok commented 2 months ago

@tnurmi82 feel free to post all your findings here. It might help other users :)

tnurmi82 commented 2 months ago

@kblok downgrading to Chrome 126.0.6478.126 worked and now I finally have working PuppeteerSharp running in AWS App Runner, on Amazon Linux 2023.

To share my experience:

I'm specifically using the "Source Repository" type or App Runner service, which in App Runner means that Github webhook triggers a new build when I commit to 'main' branch. Using App Runner this way means that even if include a Dockerfile in my repository, App Runner will not use it. Instead they employ their own Dockerfile into which they pull commands from my apprunner.yaml (which helps with IaC as you don't need to do mouse clicking in App Runner Console).

The next part was what took some head scratching and trial-and-error: AWS App Runner seems to be lagging behind with their .NET support. For other runtimes they have what they call "revised"/better build process which does things in better way. But as described in documentation (https://docs.aws.amazon.com/apprunner/latest/dg/service-source-code-dotnet-releases.html) for .NET they still use "original" build process. The "original" process is bizarre in a way that it runs the 'pre-build' once, 'build' phase twice, and 'post-build' once.

The gotcha, which I cannot find mentioned in any of App Runner's docs: I tried to install the extra Chrome dependencies in 'pre-build', and according to logs all installs fine. But the install is done on an "intermediate" docker container. What is installed there never makes it to the "final" docker container. So the installs need to be done in 'build' phase - Which fires twice. On the first run log states that actually most libraries (those needed by Chrome) are already installed and not needed. However, on the second 'build' run the install commands are ran on the "final" container, and then they are indeed all installed.

Relevant part from my apprunner.yaml:

runtime: dotnet6
build:
  commands:
    pre-build:
    build:
      - sudo yum install -y dbus-libs atk at-spi2-atk cups-libs libdrm at-spi2-core libX11 libXcomposite libXdamage libXext libXfixes libXrandr mesa-libgbm libxcb libxkbcommon pango cairo alsa-lib
      - dotnet publish -c Release -o out
    post-build:

So the pre-build and post-build are empty, as they are useless for installing dependencies. The list for yum installs I figured by doing ldd for /app/out/Chrome/Linux-126.0.6478.126/chrome-linux64/chrome - Which is the Chrome downloaded by PuppeteerSharp.

Relevant part of my C# code:

var browserFetcher = new BrowserFetcher();
var revision = await browserFetcher.DownloadAsync("126.0.6478.126");
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
  ExecutablePath = revision.GetExecutablePath(),
  Args = new string[] { "--no-sandbox" },
  Headless = true
});

With these, voilà, I have PuppeteerSharp working nicely in App Runner. Thanks @kblok for support :)

satviktechie1986 commented 2 weeks ago

getting error System.IO.IOException: Read-only file system : '/var/task/chrome-linux64.zip while downloading the chrome