I’m getting a timeout exception when trying to create PDFs with PuppeteerSharp in production.
Here’s my PDF generation function:
public async Task<IActionResult> CreateDocument(string htmlContent)
{
try
{
var chromeBrowser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true,
});
var webPage = await chromeBrowser.NewPageAsync();
// Wait up to 60 seconds for content to load
await webPage.SetContentAsync(htmlContent, new NavigationOptions { Timeout = 60000 });
var outputPath = $"D:/Reports/{Guid.NewGuid()}_{ClientId}.pdf";
await webPage.PdfAsync(outputPath);
// Clean up browser instance
await chromeBrowser.CloseAsync();
return Json(outputPath);
}
catch (Exception error)
{
LogError(error);
return Problem($"PDF creation failed: {error.Message}");
}
}
I download the browser during app startup:
// Get Chromium browser when app starts
await new BrowserFetcher().DownloadAsync();
This works fine on my development and staging servers but fails in production with a 180000ms timeout error. The stack trace shows it’s timing out during the PDF generation process.
Has anyone encountered this issue before? What could be causing the timeout only in production?
Had the same issue a few months ago with PuppeteerSharp in production. Turned out our Docker container didn’t have proper sandbox config. Chrome needs specific permissions and shared memory settings in containers. Add --no-sandbox and --disable-dev-shm-usage to your LaunchOptions if you’re using Docker. Also check your production server’s /tmp space - Chrome dumps temp files there during PDF generation. My /tmp directory was almost full, which caused the timeout. I’d also add a retry mechanism with exponential backoff since production can have random resource issues that don’t happen in dev.
This sounds like Chrome’s process management acting up in production. I hit the same issue last year - zombie Chrome processes were piling up over time. When browser instances don’t close properly (crashes, high load, whatever), they eat up system resources and new launches start timing out. I’d add a cleanup mechanism that kills any leftover Chrome processes before spinning up new ones. Also try using a singleton pattern for your browser instance instead of creating fresh ones per request - that cut my timeouts way down. Check your production server’s process list when it’s timing out to see if you’ve got multiple Chrome instances running at once.
for sure, it’s prob the resource limits in prod. sometimes diff envs react weirdly. maybe increase memory in LaunchOptions? also, add some logs to the pdf gen part, could give better insight on where it’s failing.