Skip to content

Commit

Permalink
Attempt to fix chrome process zombies (#59)
Browse files Browse the repository at this point in the history
* Updates to dispose pattern

* Updated puppeteer and added dumpinit
  • Loading branch information
savpek authored Jan 21, 2020
1 parent 965c8ed commit e9bdeff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ! IMPORTANT: Keep chromium version synced with version from package 'PuppeteerSharp'
# and match it with from https://pkgs.alpinelinux.org/packages
ARG chromium_version=77.0.3865.120-r0
ARG chromium_version=79.0.3945.88-r0

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.100-alpine3.10 as dotnetBuild
ARG chromium_version
Expand Down Expand Up @@ -50,7 +50,9 @@ RUN \
chromium=${chromium_version} \
libgdiplus \
qpdf \
icu-libs
icu-libs \
procps \
dumb-init

# https://github.com/dotnet/SqlClient/issues/81 (icu-libs is part of this too)
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
Expand All @@ -65,4 +67,5 @@ COPY --from=dotnetBuild /out/ /app/

EXPOSE 5000

ENTRYPOINT ["dotnet", "Pdf.Storage.dll"]
# dump-init fixes zombie (defunct) process problem with chrome
ENTRYPOINT ["dumb-init", "dotnet", "Pdf.Storage.dll"]
2 changes: 1 addition & 1 deletion Pdf.Storage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<!-- When you update PuppeteerSharp you must also find corresponding version
in alpine at Dockerfile. Alpine limits available chromium versions available so don't update
this without check them out. Chromium version must be about same as puppeteer expects -->
<PackageReference Include="PuppeteerSharp" Version="1.19.0" />
<PackageReference Include="PuppeteerSharp" Version="2.0.0" />

<!-- Remove this once testing library is updated -->
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.0" />
Expand Down
34 changes: 17 additions & 17 deletions Pdf/PdfQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ private async Task<byte[]> GeneratePdfDataFromHtml(Guid id, string html)
{
_logger.LogDebug($"Generating pdf from {id}");

var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
ExecutablePath = _chromiumPath,
Headless = true,
IgnoreHTTPSErrors = true,
Args = new[] { "--no-sandbox", "--disable-dev-shm-usage", "--incognito", "--disable-gpu", "--disable-software-rasterizer" },
EnqueueTransportMessages = false
});

byte[] result;
Browser browser = default;
Page page = default;

try
{
var page = await browser.NewPageAsync();
browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
ExecutablePath = _chromiumPath,
Headless = true,
IgnoreHTTPSErrors = true,
Args = new[] { "--no-sandbox", "--disable-dev-shm-usage", "--incognito", "--disable-gpu", "--disable-software-rasterizer" },
EnqueueTransportMessages = false
});

page = await browser.NewPageAsync();

await page.SetContentAsync(html,
new NavigationOptions
Expand All @@ -75,8 +76,7 @@ await page.SetContentAsync(html,
WaitUntil = new[] { WaitUntilNavigation.Load, WaitUntilNavigation.DOMContentLoaded }
});

result = await page.PdfDataAsync(new PdfOptions { Format = PaperFormat.A4 });
await page.CloseAsync();
return await page.PdfDataAsync(new PdfOptions { Format = PaperFormat.A4 });
}
catch (Exception e)
{
Expand All @@ -85,11 +85,11 @@ await page.SetContentAsync(html,
}
finally
{
await browser.CloseAsync();
await page?.CloseAsync();
page?.Dispose();
await browser?.CloseAsync();
browser?.Dispose();
}


return result;
}
}
}
5 changes: 4 additions & 1 deletion appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"DbType": "inMemory",
"MqType": "inMemory",
"PdfStorageType": "inMemory"
"PdfStorageType": "inMemory",
"Hangfire": {
"AllowedIpAddresses": ["*"]
}
}

0 comments on commit e9bdeff

Please sign in to comment.