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

Puppeteer-sharp no longer seems to work for me since v9.0 #2280

Closed
brechtvhb opened this issue Aug 14, 2023 · 11 comments
Closed

Puppeteer-sharp no longer seems to work for me since v9.0 #2280

brechtvhb opened this issue Aug 14, 2023 · 11 comments

Comments

@brechtvhb
Copy link

brechtvhb commented Aug 14, 2023

Everything is working fine on version 8.0.

But when I try to upgrade to 9.0 (to 10.0) I get this error when trying to create a page:

System.Collections.Generic.KeyNotFoundException: The given key 'E26AB5BB01D3CBE6669B41402055804A' was not present in the dictionary.
   at System.Collections.Concurrent.ConcurrentDictionary`2.ThrowKeyNotFoundException(TKey key)
   at System.Collections.Concurrent.ConcurrentDictionary`2.get_Item(TKey key)
   at PuppeteerSharp.Browser.CreatePageInContextAsync(String contextId) in C:\projects\puppeteer-sharp\lib\PuppeteerSharp\Browser.cs:line 307

When I upgrade to 10.1.0 or higher awaiting await Puppeteer.ConnectAsync(options); times out

I am connecting to a browserless chrome instance on AKS (browserless/chrome:1-puppeteer-1.20.0)

with these settings:

KEEP_ALIVE : true
MAX_CONCURRENT_SESSIONS : 2
PREBOOT_CHROME : true

The code to connect is:

            var options = new ConnectOptions
            {
                BrowserWSEndpoint = url,

            };
            wait Puppeteer.ConnectAsync(options);
@brechtvhb brechtvhb changed the title Puppeteersharp no longer seems to work for me since v9.0 Puppeteer-sharp no longer seems to work for me since v9.0 Aug 14, 2023
@kblok
Copy link
Member

kblok commented Aug 14, 2023

Do you have any code I can use to reproduce the issue?

@brechtvhb
Copy link
Author

Here's the code that is used to create a PDF from HTML. Let me know if you need anything else.

using Microsoft.Extensions.Logging;
using PuppeteerSharp;
using System;
using System.Threading.Tasks;
using SI.TemplateBuilder.Events.In.Processor.Exceptions;

namespace SI.TemplateBuilder.Events.In.Processor.Pdf
{
    public class HeadlessPdfBuilder : IPdfBuilder
    {
        private readonly ILogger<HeadlessPdfBuilder> _logger;
        private IBrowser _browser;

        public HeadlessPdfBuilder(ILogger<HeadlessPdfBuilder> logger)
        {
            _logger = logger;
            _browser = null;
        }

        private async Task<IBrowser> InitializePuppeteer()
        {
            _logger.LogInformation("Starting to initialize puppeteer");

            var url =  "*service_ip_within_aks_cluster*";

            var options = new ConnectOptions
            {
                BrowserWSEndpoint = url,

            };

            _logger.LogInformation("Puppeteer initialized");
            _logger.LogInformation("Connecting to headless chrome at {uri}", url);
            return await Puppeteer.ConnectAsync(options);
        }

        public Task<Byte[]> GetPdfAsync(String html)
        {
            return RenderPdf(html);
        }

        private async Task<byte[]> RenderPdf(String html)
        {
            var bytes = Array.Empty<byte>();

            if (_browser == null || !_browser.IsConnected || _browser.IsClosed)
            {
                _logger.LogInformation("Initializing new Puppeteer browser");
                _browser = await InitializePuppeteer();

                if (_browser == null)
                    return bytes;
            }

            IPage page = null;
            try
            {
                page = await _browser.NewPageAsync();
            }
            catch (NullReferenceException ex) // #8900
            {
                _logger.LogError(ex, "Failed to create new browser page");
            }
            await using (page)
            {
                try
                {
                    await page.SetContentAsync(html);
                    _logger.LogInformation("Put html content in Puppeteer");
                    //javascript function must be in html
                    await page.WaitForExpressionAsync("window.IsPageLoaded");

                    bytes = await page.PdfDataAsync(new PdfOptions
                    {
                        PreferCSSPageSize = true,
                    });

                    await page.CloseAsync();
                    _logger.LogInformation("Generated PDF");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "error while rendering pdf");
                }
            }
            return bytes;
        }
    }
}

@kblok
Copy link
Member

kblok commented Aug 14, 2023

Any chance you can send me the server to my email?

@brechtvhb
Copy link
Author

brechtvhb commented Aug 14, 2023

No, it's not available to the public internet.

I can send you logs of the browserless chrome intance if that would help.

@kblok
Copy link
Member

kblok commented Aug 14, 2023

Maybe the browser version you have on the other side is not compatible?

@brechtvhb
Copy link
Author

I am using this docker image: https://hub.docker.com/layers/browserless/chrome/1-puppeteer-1.20.0/images/sha256-bf82032782dfc6de7d24ca65ac94ec117f89dc35f72df3800c599bd201120b23?context=explore

What changed between puppeteer sharp 8, 9 and 10 that would make a browser no longer compatible?

@kblok
Copy link
Member

kblok commented Aug 15, 2023

@brechtvhb The FrameManager changed quite a lot. I'm building #2281 for you to see if that help.
Did you try using browserless/chrome:latest?

@kblok
Copy link
Member

kblok commented Aug 15, 2023

I just shipped v10.1.4. Can you try that version?

@brechtvhb
Copy link
Author

v10.1.4 does not solve the issue. Using browserless/chrome:latest (or browserless/chrome:1-chrome-stable-112.0.5615.49) fixed the issue. I set it to browserless/chrome:1-puppeteer-1.20.0 around new year because the new headless mode they introduced was causing issues back then.

@kblok
Copy link
Member

kblok commented Aug 16, 2023

Nice! Can we close this?

@brechtvhb
Copy link
Author

Yeah, that's OK for me. Thanks for helping me out!

@kblok kblok closed this as completed Aug 16, 2023
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