Repository: puppeteer
Issue: [Bug]: Received signal 11 SEGV_MAPERR when generating PDF in Docker on Cloud Run
Labels: bug,upstream,confirmed,disable-analyzer

### Bug expectation

I expected the PDF to be generated as normal.

What happened instead was Chrome segfaulted and returned an error.

We are having issues generating PDFs using Puppeteer in Docker running on Cloud Run. The issue we're getting is that sometimes when attempting to generate a PDF, Chrome will instead segfault and produce an error. From this point onwards, Chrome will fail to generate any further PDFs and will segfault each time a request to do so is made. Even restarting the browser appears to have no effect, it will still segfault.

There are a few facts which make this issue tricky to diagnose:
- It only happens in a container running in Docker on Cloud Run, not locally on our machines
- It happens non-deterministically. Sometimes a container instance will start up fine and with no issues, Chrome being able to produce PDFs as expected. However, at other times, an instance will start up and then immediately segfault on the first request and on every further request. However, this is not a rare issue, it will always appear within the first few minutes of a new revision being deployed.
- It is not repeatable for the same input. Requests which caused a failure the first time have been retried and gone through with no issues a second time, implying the issue is not with the input data.

An interesting thing to note is that the segfault always occurs on the first request to the instance, suggesting that the issue could be something to do with the startup behaviour of Chrome, perhaps some aspect of it not being quite ready when the request comes in?

Here is the exact error and stacktrace we are getting:
```
Received signal 11 SEGV_MAPERR 000000000000
#0 0x2a3243986832 base::debug::CollectStackTrace()
#1 0x2a32438f5b53 base::debug::StackTrace::StackTrace()
#2 0x2a3243986351 base::debug::(anonymous namespace)::StackDumpSignalHandler()
#3 0x3ee75fd3c730 (/lib/x86_64-linux-gnu/libpthread-2.28.so+0x1272f)
#4 0x2a3247145e18 blink::LoaderFactoryForFrame::CreateURLLoader()
#5 0x2a3242bc4e93 blink::ResourceFetcher::CreateURLLoader()
#6 0x2a3242bd4fd3 blink::ResourceLoader::Start()
#7 0x2a3242bc4a6f blink::ResourceFetcher::StartLoad()
#8 0x2a3242bc5be2 blink::ResourceFetcher::StartLoad()
#9 0x2a3246ad86ed blink::RemoteFontFaceSource::BeginLoadIfNeeded()
#10 0x2a3246acaff0 blink::CSSFontFace::MaybeLoadFont()
#11 0x2a3246ad221f blink::FontFaceList::ForEachReverseUntilTrue()
#12 0x2a3246ad20c3 blink::CSSSegmentedFontFace::WillUseFontData()
#13 0x2a3246f64cb1 blink::LayoutText::StyleDidChange()
#14 0x2a3246f1425b blink::LayoutObject::SetStyle()
#15 0x2a324699c890 blink::LayoutTreeBuilderForText::CreateLayoutObject()
#16 0x2a324699af9b blink::Text::AttachLayoutTree()
#17 0x2a3247673cc8 blink::ContainerNode::AttachLayoutTree()
#18 0x2a324771cfd8 blink::Element::AttachLayoutTree()
#19 0x2a3247673cc8 blink::ContainerNode::AttachLayoutTree()
#20 0x2a324771cfd8 blink::Element::AttachLayoutTree()
#21 0x2a3247673cc8 blink::ContainerNode::AttachLayoutTree()
#22 0x2a324771cfd8 blink::Element::AttachLayoutTree()
#23 0x2a3247673cc8 blink::ContainerNode::AttachLayoutTree()
#24 0x2a324771cfd8 blink::Element::AttachLayoutTree()
#25 0x2a3247673cc8 blink::ContainerNode::AttachLayoutTree()
#26 0x2a324771cfd8 blink::Element::AttachLayoutTree()
#27 0x2a3246868e41 blink::Node::ReattachLayoutTree()
#28 0x2a32477215df blink::Element::RebuildLayoutTree()
#29 0x2a3247675133 blink::ContainerNode::RebuildChildrenLayoutTrees()
#30 0x2a3247721732 blink::Element::RebuildLayoutTree()
#31 0x2a3247675133 blink::ContainerNode::RebuildChildrenLayoutTrees()
#32 0x2a3247721732 blink::Element::RebuildLayoutTree()
#33 0x2a32469ab22b blink::StyleEngine::RebuildLayoutTree()
#34 0x2a32469ab822 blink::StyleEngine::UpdateStyleAndLayoutTree()
#35 0x2a3247682172 blink::Document::UpdateStyle()
#36 0x2a324768156f blink::Document::UpdateStyleAndLayoutTreeForThisDocument()
#37 0x2a3246841c37 blink::LocalFrameView::UpdateStyleAndLayoutInternal()
#38 0x2a324683bfb5 blink::LocalFrameView::UpdateStyleAndLayout()
#39 0x2a3247681a25 blink::Document::UpdateStyleAndLayout()
#40 0x2a324684205b blink::LocalFrameView::ForceLayoutForPagination()
#41 0x2a324681e76d blink::LocalFrame::SetPrinting()
#42 0x2a32471c78b1 blink::PrintContext::BeginPrintMode()
#43 0x2a3246c1ecf6 blink::WebLocalFrameImpl::PrintBegin()
#44 0x2a32494ca575 printing::PrintRenderFrameHelper::PrintHeaderAndFooter()
#45 0x2a32494d1874 printing::PrintRenderFrameHelper::PrintPageInternal()
#46 0x2a32494d3b64 printing::PrintRenderFrameHelper::PrintPagesNative()
#47 0x2a32494ce90a printing::PrintRenderFrameHelper::PrintPages()
#48 0x2a32494ce697 printing::PrintRenderFrameHelper::PrintWithParams()
#49 0x2a324286f30d printing::mojom::PrintRenderFrameStubDispatch::AcceptWithResponder()
#50 0x2a3243cae25e mojo::InterfaceEndpointClient::HandleValidatedMessage()
#51 0x2a3243cb34a7 mojo::MessageDispatcher::Accept()
#52 0x2a3243caf6aa mojo::InterfaceEndpointClient::HandleIncomingMessage()
#53 0x2a324404e158 IPC::(anonymous namespace)::ChannelAssociatedGroupController::AcceptOnEndpointThread()
#54 0x2a3243cb0e66 base::internal::Invoker<>::RunOnce()
#55 0x2a324393f1e1 base::TaskAnnotator::RunTaskImpl()
#56 0x2a3243955a6a base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWorkImpl()
#57 0x2a32439554cf base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWork()
#58 0x2a3243956195 base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWork()
#59 0x2a32438fda4a base::MessagePumpDefault::Run()
#60 0x2a32439564d3 base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::Run()
#61 0x2a324392074f base::RunLoop::Run()
#62 0x2a32493f332a content::RendererMain()
#63 0x2a32434301af content::RunZygote()
#64 0x2a3243430ad2 content::RunOtherNamedProcessTypeMain()
#65 0x2a324343195d content::ContentMainRunnerImpl::Run()
#66 0x2a324342e856 content::RunContentProcess()
#67 0x2a324342f16f content::ContentMain()
#68 0x2a324348eb69 headless::(anonymous namespace)::RunContentMain()
#69 0x2a324348e9e5 headless::RunChildProcessIfNeeded()
#70 0x2a324348d657 headless::HeadlessShellMain()
#71 0x2a323f89a1e6 ChromeMain
#72 0x3ee75e66409b __libc_start_main
Uncaught signal: 11, pid=68, tid=68, fault_addr=0.
#73 0x2a323f89a02a _start
  r8: 00002c9403201940  r9: 00003ed90773b0d8 r10: 0000000000000000 r11: 00002c940334af50
 r12: 00003ed90773b4d0 r13: 00000a1b001ebbf0 r14: 00002c940334af50 r15: 0000000000000000
  di: 00000a1b00161000  si: 00000a1b001624c0  bp: 00003ed90773b5f0  bx: 00002c940334af60
  dx: 40af3403942c0000  ax: 00002c940038e0c0  cx: 00002a324716ce00  sp: 00003ed90773b3d0
  ip: 00002a3247145e18 efl: 0000000000010202 cgf: 002b000000000033 erf: 0000000000000000
 trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000
[end of stack trace]
```
and here is the rough Dockerfile we're using:
```Dockerfile
FROM node:18

RUN apt-get update \
    && apt-get install -y gnupg wget ... \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
EXPOSE 6003
CMD [ "npm", "start" ]
```
Any help is appreciated, thank you.

### Bug behavior

- [ ] Flaky
- [x] PDF

### Minimal, reproducible example

```TypeScript
// This is roughly all we are doing with Puppeteer
// The HTML is being generated by React's server-side rendering
import puppeteer from 'puppeteer'

const html = '[html]';
const headerTemplate = '[header]';
const footerTemplate = '[footer]';

const browser = await puppeteer.launch({
	headless: true,
	args: [
		'--disable-gpu',
		'--disable-dev-shm-usage',
		'--disable-setuid-sandbox',
		'--no-sandbox',
	]
});

const page = await browser.newPage();

await page.setContent(html, { waitUntil: 'load' });

await page.pdf({
	path: 'out.pdf',
	format: 'A4',
	scale: 2/3,
	displayHeaderFooter: true,
	headerTemplate,
	footerTemplate,
	margin: {
		bottom: 105,
		left: 0,
		right: 0,
		top: 120,
	},
	printBackground: true,
	timeout: 300000,
});
```


### Error string

Received signal 11 SEGV_MAPERR 000000000000

### Puppeteer configuration

None

### Puppeteer version

19.2.2

### Node version

18.14.0

### Package manager

npm

### Package manager version

9.3.1

### Operating system

Linux