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.

ScreenshotDataAsync method never returns if a Page object is being created on a separate thread.

See original GitHub issue

Description

ScreenshotDataAsync method never returns if the code creates a new browser page during that operation.

The purpose for this is to create an auto-refillable ConcurrentQueue of pages that would provide the next available page to a client that asks for it. The idea is to create 20 pages via NewPageAsync and hand them out to clients who ask for a page. When the amount of available pages gets down to 3, create more pages.

Complete minimal example reproducing the issue

The code starts the browser, creates a page, goes to a new thread to create the screenshot. Meanwhile, the main thread creates 20 more new pages.

void Main(string[] args) {
    var options = new LaunchOptions() {
        // replace with your own or downllad
        ExecutablePath = @"C:\Projects\net2\Tools\WidgetImages\chromium\chrome.exe",
        Headless = false
    };
    Browser browser = Puppeteer.LaunchAsync(options).Result;
    Page page = browser.NewPageAsync().Result;

    Task.Run(() => TakeScreenshot(page, "https://www.google.com"));

    // create a bunch of pages
    var pagesQueue = new ConcurrentQueue<Page>();
    for (int i = 0; i < 20; i++) {
        Debug.WriteLine(DateTime.Now.Ticks + $" CreatePage {i}");
        pagesQueue.Enqueue(browser.NewPageAsync().Result);
    }

    Console.ReadKey();
}

public static void TakeScreenshot(Page page, string url) {
    var viewportOptions = new ViewPortOptions {
        Width = 800,
        Height = 800
    };
    page.SetViewportAsync(viewportOptions).Wait();
    var webPage = page.GoToAsync(url).Result;

    // get the screenshot
    Debug.WriteLine("ScreenshotDataAsync start");
    var imageBytes = page.ScreenshotDataAsync().Result;
    Debug.WriteLine("ScreenshotDataAsync End");
    
    var ms = new MemoryStream(imageBytes);
    Image img = Image.FromStream(ms);

    string desktopDir = Environment.GetFolderPath(SpecialFolder.DesktopDirectory);
    string path = Path.Combine(desktopDir, "img1.png");
    img.Save(path);
}

Expected behavior:

The ScreenshotDataAsync method generates a screenshot.

Actual behavior:

The ScreenshotDataAsync method never returns.

Versions

  • PuppeteerSharp 1.14
  • .NET Core 2.2.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mschuttercommented, Oct 8, 2020

Just found out it was caused by something else. Screenshot is working fine. sorry for the trouble.

0reactions
mcopurcommented, Jan 21, 2021

Just found out it was caused by something else. Screenshot is working fine. sorry for the trouble.

Hi, How did you solve it or what was the problem? I have the same problem, could you help me with that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ScreenshotDataAsync method never returns if a Page ...
The code starts the browser, creates a page, goes to a new thread to create the screenshot. Meanwhile, the main thread creates 20...
Read more >
Use PuppeteerSharp.ElementHandle. ...
Use the ScreenshotDataAsync method in PuppeteerSharp package in your next Puppeteer-sharp project with LambdaTest Automation Testing Advisor.
Read more >
Convert HTML string to image
Now I want to create an image from this string content which actually holds the HTML markup. I don't want to create the...
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