admin管理员组

文章数量:1208153

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 .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: .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

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

Share Improve this question edited Jan 21 at 0:40 racitup asked Jan 20 at 1:17 racitupracitup 4544 silver badges12 bronze badges 2
  • Please share a complete, runnable minimal reproducible example--which input and browser launch settings are causing this, or is this on any input? If it's any input, that's not normall, so system details would likely be relevant. Does this fail on goto as well? Please isolate the problem and provide a package.json. Thanks. – ggorlen Commented Jan 20 at 2:21
  • If you notice the question I ask "How can I debug why this is happening?" and not "Please debug this for me" – racitup Commented Jan 20 at 23:15
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: puppeteerbrowserslauncher Trying to killStack Overflow