Railway Function with Headless Puppeteer crashes
djulian
PROOP

2 months ago

Hi,

I'm using a simple Railway Function to convert some Handlebars (.hbs) file to PDF. I'd say I have a success rate of 95% so far on thousands of documents but sometimes, the process crashes with either of those errors:
```
Error processing request: WebSocket connection to 'ws://127.0.0.1:39117/devtools/browser/a474123a-722a-407e-a90f-5c2218a72906' failed: Connection ended
```
```
Error processing request: Failed to launch the browser process!

[12787:12813:0113/150809.412403:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory

[12787:12813:0113/150809.419057:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")

[12787:12813:0113/150809.419216:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory

[12787:12813:0113/150809.419365:ERROR:dbus/bus.cc:406] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory

[12787:12814:0113/150809.419583:ERROR:base/threading/platform_thread_posix.cc:162] pthread_create: Resource temporarily unavailable (11)

[12787:12820:0113/150809.419608:ERROR:base/threading/platform_thread_posix.cc:162] pthread_create: Resource temporarily unavailable (11)

[12787:12787:0113/150809.421282:ERROR:base/threading/platform_thread_posix.cc:162] pthread_create: Resource temporarily unavailable (11)

[0113/150809.429662:ERROR:third_party/crashpad/crashpad/util/file/file_io_posix.cc:145] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)

[0113/150809.429731:ERROR:third_party/crashpad/crashpad/util/file/file_io_posix.cc:145] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)

TROUBLESHOOTING: https://pptr.dev/troubleshooting

```
```
13531:13531:0113/151544.772895:ERROR:base/threading/platform_thread_posix.cc:162] pthread_create: Resource temporarily unavailable (11)

[0113/151544.780875:ERROR:third_party/crashpad/crashpad/util/file/file_io_posix.cc:145] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)

[0113/151544.780941:ERROR:third_party/crashpad/crashpad/util/file/file_io_posix.cc:145] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
```
```
Error processing request: Failed to launch the browser process!

/usr/bin/chromium: 123: Cannot fork

[13592:13592:0113/151604.452563:FATAL:third_party/crashpad/crashpad/util/posix/spawn_subprocess.cc:237] posix_spawn /usr/lib/chromium/chrome_crashpad_handler: Resource temporarily unavailable (11)

/usr/bin/chromium: 5: /etc/chromium.d/extensions: Cannot fork

/usr/bin/chromium: 121: Cannot fork

/usr/bin/chromium: 123: Cannot fork
```

Then after sometimes, the server will restart and all will go smoothly again.

Here are some code overview if it helps:
```typescript
let browser: Browser | null = null;

try {

browser = await puppeteer.launch({

headless: true,

args: [

'--no-sandbox',

'--disable-gpu',

'--disable-dev-shm-usage',

'--disable-setuid-sandbox',

'--no-first-run',

'--no-zygote',

'--disable-extensions',

'--disable-background-networking',

'--disable-default-apps',

'--disable-sync',

'--disable-translate',

'--hide-scrollbars',

'--metrics-recording-only',

'--mute-audio',

'--ignore-certificate-errors'

]

});

const page = await browser.newPage();

await page.setCacheEnabled(false);

await page.setContent(hbsToHtml, {

waitUntil: 'networkidle0'

});

const pdf = await page.pdf({

format: 'A4',

printBackground: true,

});

// save pdf in S3 bucket and send URI
// .....

} catch (error: any) {

console.errorError processing request: ${error.message})

return c.json({ error: error.message }, 500)

} finally {

if (browser) {

const pages = await browser.pages()

await Promise.all(pages.map(page => page.close()))

await browser.close()

}

}
```

Let me know if you guys have any idea why this is going on. :)

$10 Bounty

1 Replies

2 months ago

Hi! I am not really familiar with HandleBars but I think this is probably not a Puppeteer bug and not really related to the PDF generation logic.

It seems as resource-exhaustion problem in your Railway container.

The connection fails because the function is unable to launch chromium.

1. pthread_create: Resource temporarily unavailable (11)

2. Cannot fork

3. posix_spawn ... Resource temporarily unavailable

4. WebSocket to DevTools disconnects

Probably because the container can’t create new threads or processes. That's why after restarting and clearing zombie Chromium processes everything works fine again. ¿Could you share more details?

In the meantime verify that the function is not launching a chromium on every request. Or even concurrency, for example, prevent 10 requests from launching 10 Chromiums at once. Could try using PQueue.

let browserPromise: Promise<Browser> | null = null;

async function getBrowser() {

if (!browserPromise) {

browserPromise = puppeteer.launch({

headless: true,

args: [

'--no-sandbox',

'--disable-gpu',

'--disable-dev-shm-usage',

'--disable-setuid-sandbox',

'--no-first-run',

'--no-zygote',

'--disable-extensions',

'--disable-background-networking',

'--disable-default-apps',

'--disable-sync',

'--disable-translate',

'--hide-scrollbars',

'--metrics-recording-only',

'--mute-audio',

'--ignore-certificate-errors'

'--single-process' // Maybe using ?

]

});

}

return browserPromise;

}

then

const browser = await getBrowser();

const page = await browser.newPage();

try {

your logic...

} finally {

await page.close();

}

Or finally, maybe try increasing Railway resources if possible.

Hope this somehow helps. If not, we can learn together. slightly_smiling_face emoji


Loading...