question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TargetClosedException after Page.CloseAsync()

See original GitHub issue

First of all, thank you for your tremendous effort that went into this cool project!

I’m trying to generate PDF file, as you will see in the snippet below and although everything works fine, even when I fire multiple concurrent requests that are opening multiple instances of chromium, I’m seeing some exceptions in the log files.

I’ve been able to narrow down that the issue occurs when trying to close the connection. The following exception is being thrown:

  • PuppeteerSharp.TargetClosedException: 'Navigation failed because browser has disconnected! (Target.detachedFromTarget)'

Example reproducing the issue:

            Stream stream;
            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

            var browserOptions = new LaunchOptions
            {
                Headless = true,
                Args = new[] { "--no-sandbox" },
            };

            var browser = await Puppeteer.LaunchAsync(browserOptions, loggerFactory);
            var page = await puppeteerService.GetPage(browser);

            await page.SetContentAsync("<div>Testing</div>", puppeteerService.GetNavigationOptions());
            stream = await page.PdfStreamAsync(puppeteerService.GetPdfOptions());

            await page.CloseAsync(); // after this line the exception is thrown
            await browser.CloseAsync();

Versions

PuppeteerSharp: 1.12.1 .NET: netcoreapp2.1 Platform: Windows 10

Attached log file (LogLevel.Trace): puppeteer-sharp.log-2019-03-14.txt

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
R2D221commented, Jul 16, 2019

I created a pull request (https://github.com/kblok/puppeteer-sharp/pull/1218)

I think I found the issue. The problem is when disposing an object, if it has unhandled tasks, it goes to the event. So, what I did was take them and handle them myself in the finalizer.

Let me know what you think.

0reactions
kblokcommented, Jun 9, 2022

@Emersonbg I was about to suggest that. One thing is the browser opening a big page and another is the browser receiving a massive CDP message. On the other hand, SetContentAsync is no more than an evaluate setting the HTML content of the document. You could implement that and send the content in chunks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TargetClosedException after Page.CloseAsync() #1016
The following exception is being thrown: PuppeteerSharp.TargetClosedException: 'Navigation failed because browser has disconnected! (Target.
Read more >
Use PuppeteerSharp.TargetClosedException in Puppeteer ...
Want to learn how to use TargetClosedException class in PuppeteerSharp package for your next ... CloseAsync();14 }15 }16}17var browser = await Puppeteer.
Read more >
Use PuppeteerSharp.TargetCrashedException. ...
TargetClosedException : Protocol error(IO.read): Target closed. (Page failed to process Inspector.targetCrashed. Exception of type 'PuppeteerSharp.
Read more >
Class Browser
CloseAsync(). Closes Chromium and all of its pages (if any were opened). The browser object itself is considered disposed and cannot be used...
Read more >
Disposing a Page causes a warning. Is this an issue?
I have the following code that creates, then closes a Chrome page via PuppeteerSharp: var options = new LaunchOptions() {Headless = true, Args...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found