puppeteer is killing my page when I call setContent
.
How can I debug why this is happening?
code:
chrome = await puppeteer.launch();
page = await chrome.newPage();
await page.setUserAgent(ua);
await page.setViewport({width: Number(width), height: Number(height)});
console.warn("here4");
if (input) {
console.warn("input found");
await page.setContent(input);
}
console.warn("here5");
log:
2025-01-20T00:54:47.099Z puppeteer:browsers:launcher Launching /home/user/.cache/puppeteer/chrome-headless-shell/linux-132.0.6834.83/chrome-headless-shell-linux64/chrome-headless-shell --allow-pre-commit-input --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-crash-reporter --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-hang-monitor --disable-infobars --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-search-engine-choice-screen --disable-sync --enable-automation --export-tagged-pdf --force-color-profile=srgb --generate-pdf-document-outline --metrics-recording-only --no-first-run --password-store=basic --use-mock-keychain --disable-features=Translate,AcceptCHFrame,MediaRouter,OptimizationHints,ProcessPerSiteUpToMainFrameThreshold,IsolateSandboxedIframes --enable-features=PdfOopif --headless --hide-scrollbars --mute-audio about:blank --remote-debugging-port=0 --user-data-dir=/tmp/puppeteer_dev_chrome_profile-BQX8Ok { detached: true, env: {}, stdio: [ 'pipe', 'ignore', 'pipe' ] }
2025-01-20T00:54:47.116Z puppeteer:browsers:launcher Launched 2692983
here4
input found
2025-01-20T00:54:47.490Z puppeteer:browsers:launcher Trying to kill 2692983
2025-01-20T00:54:47.490Z puppeteer:browsers:launcher Browser process 2692983 exists
/home/user/v14-staging/apps/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:80
this._reject(callback, new Errors_js_1.TargetCloseError('Target closed'));
EDIT: The function that prints "Trying to kill" is kill() on the Process class https://github.com/puppeteer/puppeteer/blob/89f4f3b1079874b2dd91c727bc058c1b6e09096e/packages/browsers/src/launch.ts#L408 However, it looks like this is simply reacting to the target being closed.
In the protocol trace, after I do page.setViewport
I see:
2025-01-21T00:02:12.267Z puppeteer:protocol:SEND ► [
'{"method":"Target.closeTarget","params":{"targetId":"F8275C626B6FC4F650B5D03DA59FECD9"},"id":4}'
]
But I'm not sending that in my code
There are only two places in the code that send this, webworker and page. Most logical place is page here: https://github.com/puppeteer/puppeteer/blob/89f4f3b1079874b2dd91c727bc058c1b6e09096e/packages/puppeteer-core/src/cdp/Page.ts#L1143
So looks like something is calling page.close() after I call page.setViewport
EDIT2: Having messed around with the code it looks like something is calling page.close() if I just wait after opening the page
It looks like puppeteer doesn't like you calling page.setViewport
before having any content loaded.
If you do it looks like page.close() is called.
If anyone is trying to debug problems with puppeteer I recommend starting it with this line from a shell:
env DEBUG="puppeteer:*" node puppeteer_script.js "$@" <&0
This should help you find the problem.
EDIT:
Actually scratch my above answer.
I think the real reason for the early termination was that I was using an async function to do a screenshot, but forgot the await
on the call.
So the parent function was closing the browser before the promise resolved.