hardkoded / puppeteer-sharp

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

ChromiumProcessException: Failed to launch Chromium! #1502

Closed DevGenesis95 closed 4 years ago

DevGenesis95 commented 4 years ago

Description

When running the function in AWS lambda getting this exception.

{ "ClassName": "PuppeteerSharp.ChromiumProcessException", "Message": "Failed to launch Chromium! ", "Data": null, "InnerException": null, "HelpURL": null, "StackTraceString": " at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)\n at PuppeteerSharp.ChromiumProcess.State.StartingState.StartCoreAsync(ChromiumProcess p)\n at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)\n at PuppeteerSharp.Launcher.LaunchAsync(LaunchOptions options)\n at ConvertorHelpers.PdfConvertor.ConvertToPdf2(String url, String fileName, String filePath) in ConvertorHelpers\PdfConvertor.cs:line 107", "RemoteStackTraceString": null, "RemoteStackIndex": 0, "ExceptionMethod": null, "HResult": -2146233088, "Source": "System.Private.CoreLib", "WatsonBuckets": null }

Complete minimal example reproducing the issue

The executable path which is formed in lambda function after fetching the browser is "/tmp/chromium/Linux-706915/chrome-linux/chrome"

E.g.

// Arrange
 var options = new LaunchOptions
                {
                    ExecutablePath = executablePath,     
                    Headless = true,
                    Devtools = false,
                    LogProcess = true,
                    Args = new[] { "--no-sandbox" }
                };

var chromiumRevision = BrowserFetcher.DefaultRevision;
  using (var browser = await Puppeteer.LaunchAsync(options)) // Exception line 107

// Act
...

// Assert
...

Expected behavior:

launch and convert HTML to PDF

Actual behavior:

Crasehs

Versions

Additional Information

Its working fine when m executing it locally i.e. on my PC(windows) but on lambda function when running on Linux getting the error.

kblok commented 4 years ago

Hey @DevGenesis95 can you take a look at this https://github.com/litmus/HeadlessChromium.Puppeteer.Lambda.Dotnet and see if that helps?

DevGenesis95 commented 4 years ago

Thanks for the reply @kblok but I have tried that too but unfortunately it is not compiling with my lambda which is in .net core 2.1 as mentioned in the repo. Apart from that, I have tried to execute all the permission through my code but yet the issue still persists.

            var fileInfoexec = new UnixFileInfo(executablePath);
            fileInfoexec.FileAccessPermissions = FileAccessPermissions.AllPermissions |
                                             FileAccessPermissions.AllPermissions;
kblok commented 4 years ago

@DevGenesis95 I think it's not only about a permission fix but which is the chromium-browser AWS supports, see https://github.com/alixaxel/chrome-aws-lambda/tree/master/bin

DevGenesis95 commented 4 years ago

Umm Actually I don't know much about node js..are you taking about the browser revisions? Is that the issue.

kblok commented 4 years ago

It seems AWS won't be able to run a normal chromium release. Those 2 libraries are downloading another chromium version there.

DevGenesis95 commented 4 years ago

The nuget used is PuppeteerSharp version 2.0.3 and the Browserfetcher.DefaultRevision = 706915. Should I degrade the puppeteer version? Any suggestion about what should I do. It would be really appreciated :)

kblok commented 4 years ago

I created this issue https://github.com/litmus/HeadlessChromium.Puppeteer.Lambda.Dotnet/issues/49 :)

DevGenesis95 commented 4 years ago

Thanks for the help 👍

brianfeucht commented 4 years ago

@DevGenesis95 you can specify the version of PuppeterSharp you want to use in your csproj file. You do this by adding a direct reference to the Nuget package instead of inheriting the minimum version set in HeadlessChromium.Puppeteer.Lambda.Dotnet

DevGenesis95 commented 4 years ago

@brianfeucht Yes, PuppeteerError I have downgraded the Pupeeter NuGet package to 1.14.1 which uses the DefaultRevision = 641577 but still, the error persists. Attaching cloudwatch screenshot for this

brianfeucht commented 4 years ago

@DevGenesis95 HeadlessChromium.Puppeteer.Lambda.Dotnet build 1.0.1.39 is chromium 756035 (83.0.4103.0)

I am running this with PuppeteerSharp build 2.0.3 on Lambda .NET 3.1. It should work with Lambda .NET 2.1 but I haven't verified that myself. Would you be willing to try that combo?

DevGenesis95 commented 4 years ago

Thanks for the info actually I can go with .Net 3.1 if that's working. No, it's not working in 2.1 for me. Can you give me a solution where you have integrated it 3.1 lambda that would be really helpful :)

brianfeucht commented 4 years ago

Our project files looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
  </PropertyGroup>
  <ItemGroup>
    <None Remove="aws-lambda-tools-defaults.json" />
    <Content Include="aws-lambda-tools-defaults.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="PuppeteerSharp" Version="2.0.3" />
    <PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.2.0" />
    <PackageReference Include="HeadlessChromium.Puppeteer.Lambda.Dotnet" Version="1.0.1.39" />
  </ItemGroup>
</Project>

The aws-lambda-tools-default.json file looks like:

{
  "configuration" : "Release",
  "framework" : "netcoreapp3.1",
  "function-runtime":"dotnetcore3.1",
  "function-memory-size" : 1536,
  "function-timeout" : 60,
  "function-handler" : "Lambda::LambdaHandler::Handle"
}

We deploy following a process similar to this: https://aws.amazon.com/premiumsupport/knowledge-center/build-lambda-deployment-dotnet/

DevGenesis95 commented 4 years ago

Ok but can you check mine.. image image image

I have created a lambda function(3.1) and referenced your puppeteer library in it and then run the PowerShell script.

The handler looks like this currently I'm resolving the dependencies and will upgrade the ram as well when i test it locally before publishing...for publishing, i use visual studio publish to lambda directly image

brianfeucht commented 4 years ago

Ah I see what is going on here, there is a bit of magic in the creation of the nuget packages that make sure aws.tar.br, chromium.br and swiftshader.tar.br make into the final zip package uploaded to lambda. Since you are including this project this way, that magic is lost and those files do not make it into your Lambda deploy package.

Instead of using the project reference can you use the package reference?

DevGenesis95 commented 4 years ago

Yes, I can use in any way been lost from few days :( ok joke apart

so u want me to include these in my csproj of lambda ryt?

 <PackageReference Include="PuppeteerSharp" Version="2.0.3" />
    <PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.2.0" />
    <PackageReference Include="HeadlessChromium.Puppeteer.Lambda.Dotnet" Version="1.0.1.39" />

But just a bit confused why we including the "HeadlessChromium.Puppeteer.Lambda.Dotnet" this package if pupeetersharp doing all the stuff.

brianfeucht commented 4 years ago

All HeadlessChromium.Puppeteer.Lambda.Dotnet does is repackage a build of chromium for Lambda and handles copying those files to a place executable by .NET. It is meant to be a simple helper library to make PuppeteerSharp easier to use. Building chromium for lambda is a bunch of work so we actually reuse the work done for a NodeJs project

DevGenesis95 commented 4 years ago

ok that's really impressive work u did there.

Update I have created a new lambda(3.1)..can you verify if its correct this time. image

image

brianfeucht commented 4 years ago

@DevGenesis95 yes this looks like it should work

DevGenesis95 commented 4 years ago

oops..got this when on run i think its gonna take a while :( image

brianfeucht commented 4 years ago

@DevGenesis95 not sure the mock lambda will work on windows. Since the chromium build is specific to a Linux Lambda AMI it is likely even if you get past this error you will get a bunch of failures trying to start chrome

DevGenesis95 commented 4 years ago

ok, last shot I'm trying is to run this in the AWS environment.

DevGenesis95 commented 4 years ago

Kudos guys..finally its working ..able to generate pdf from URL

image

Thanks a ton!! @brianfeucht @kblok :)

kblok commented 4 years ago

Awesome!! Thank you @brianfeucht!