Segmentation Fault on AWS Lambda

When using AWS Lambda in a Linux container and calling the render method concurrently many times, it sometimes causes the following exception.

Exception:
Error: Runtime exited with error: signal: segmentation fault Runtime.ExitError

Solutions

The solution is to call the GC.Collect method after the PDF document is rendered. We haven't encountered this issue on any other cloud platform. This only started happening in the recent version; the old one with .NET 6 using Amazon Linux 2 is still working.

// Create an instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render the HTML string to a PDF document
PdfDocument document = renderer.RenderHtmlAsPdf(htmlString);

// Explicitly trigger garbage collection to help manage memory usage
GC.Collect();
// Create an instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render the HTML string to a PDF document
PdfDocument document = renderer.RenderHtmlAsPdf(htmlString);

// Explicitly trigger garbage collection to help manage memory usage
GC.Collect();
' Create an instance of ChromePdfRenderer
Dim renderer As New ChromePdfRenderer()

' Render the HTML string to a PDF document
Dim document As PdfDocument = renderer.RenderHtmlAsPdf(htmlString)

' Explicitly trigger garbage collection to help manage memory usage
GC.Collect()
$vbLabelText   $csharpLabel