0

I am tryng to execute the following code:

            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
#if DEBUG
            var chromeLocation = Path.Combine(context.FunctionDirectory, @"../.local-chromium/Win64-706915/chrome-win/chrome.exe");
#endif
            stage = "Start Browser";
            var browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = true,
                Args = new[] { "--no-sandbox", "--shm-size=1gb" }
#if DEBUG
                ,
                ExecutablePath = chromeLocation,
                IgnoreHTTPSErrors = true
#endif
            });
            var page = await browser.NewPageAsync();
            var headers = new Dictionary<string, string> { { "Authorization", $"Bearer {authToken}" } };
            await page.SetExtraHttpHeadersAsync(headers);
            stage = "Load Page";
            await page.GoToAsync(printToPdfRequest.TargetUrl);
            stage = "Load Page 1";
            Thread.Sleep(5000);
            // This waits unti the loader has disappeared or times-out after 30 seconds
            await page.WaitForFunctionAsync("() => document.getElementsByClassName('loader').length > 0 && document.getElementsByClassName('loader')[0].style.length > 0 && document.getElementsByClassName('loader')[0].style[0] === 'display' && document.getElementsByClassName('loader')[0].style['display'] === 'none'");

This works locally but fails on the last line when deployed to a docker container. It fails with the following message:

Failed PrintToPdf at stage Load Page 1 with message Protocol error (Runtime.callFunctionOn): Session closed. Most likely the Page has been closed.Close reason: NetworkManager failed to process Network.requestWillBeSent. Value cannot be null. (Parameter 'key'). at System.Collections.Concurrent.ConcurrentDictionary2.ThrowKeyNullException() at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at PuppeteerSharp.Helpers.MultiMap2.Add(TKey key, TValue value) at PuppeteerSharp.Helpers.AsyncDictionaryHelper`2.GetItemAsync(TKey key) at PuppeteerSharp.NetworkManager.OnRequestAsync(RequestWillBeSentPayload e, String interceptionId) at PuppeteerSharp.NetworkManager.OnRequestWillBeSentAsync(RequestWillBeSentPayload e) at PuppeteerSharp.NetworkManager.Client_MessageReceived(Object sender, MessageEventArgs e)

What does this error mean and how can I fix/get around it?

This is running in an azure function backed by an azure container instance if it makes any difference

johnstaveley
  • 1,400
  • 1
  • 22
  • 46

1 Answers1

0

In case this helps anyone. I am guessing this is what happens: It appears puppeteer has some dependency on the operating system it is deployed to. This causes some pages to silently crash 'Page has been closed.'. You only see this error when you next try to access the page. The error I received above means basically 'no page is loaded', because the previous access caused chrome to disappear. This isn't a problem with pupeteersharp specifically but also with pupeteer, a quick search on the internet showed this.

I didn't have time to go into what the dependency was that missing so I resolved the problem by moving the code from a docker container to a windows server. Same code. New environment. No problems.

If I'd had more time I would have put some diagnostics into chrome to try and work out how to set up the docker container with everything it needed so all of the web pages worked fine.

johnstaveley
  • 1,400
  • 1
  • 22
  • 46
  • observed the same thing when developing on windows vs deploying on k8s with browserless/chrome image. Thought/still think it is my wrong first k8s config – Fritz Nov 20 '22 at 13:27