The record work just one Time. Get protocol error from second time using
I am using puppeteer-stream to record in headless mode. The application run on docker.
It work fines for each first time the docker start, but then from second time, It stuck at getStream line and after some time, it throw this error:
ProtocolError: Runtime.callFunctionOn timed out. Increase the 'protocolTimeout' setting in launch/connect calls for a higher timeout if needed.2025-04-07T12:29:06.3985816Z at <instance_members_initializer> (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:93:14)2025-04-07T12:29:06.3985841Z at new Callback (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:97:16)2025-04-07T12:29:06.3985861Z at CallbackRegistry.create (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/CallbackRegistry.js:22:26)2025-04-07T12:29:06.3985880Z at Connection._rawSend (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/Connection.js:89:26)2025-04-07T12:29:06.3985898Z at CdpCDPSession.send (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/CDPSession.js:66:33)2025-04-07T12:29:06.3985917Z at #evaluate (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:358:50)2025-04-07T12:29:06.3985936Z at ExecutionContext.evaluate (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:273:36)2025-04-07T12:29:06.3985975Z at IsolatedWorld.evaluate (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/IsolatedWorld.js:99:30)2025-04-07T12:29:06.3985994Z at CdpFrame.evaluate (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Frame.js:338:43)2025-04-07T12:29:06.3986011Z at CdpFrame.<anonymous> (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/util/decorators.js:98:27)
My code:
const { launch, getStream, wss } = require("puppeteer-stream");
const Xvfb = require("xvfb");
// Capture the video using ffmpeg
const videoName = `${uuidv4()}_${Date.now()}.mp4`;
const outputVideoPath = path.join(recordsFolderPath, videoName);
logger.info("Create virtual display . . .");
const xvfb = new Xvfb({
silent: false,
xvfb_args: ["-screen", "0", "1920x1080x30", "-ac"],
});
xvfb.start((err) => {
if (err) {
logger.error(`XVFB ${err}`);
}
});
logger.info(
`Create virtual display success with display: ${xvfb._display}`
);
logger.info("Launching browser . . .");
const browser = await launch({
headless: true,
executablePath:
process.env.PUPPETEER_EXECUTABLE_PATH || "/usr/bin/chromium-browser",
// or on linux: "google-chrome-stable"
// or on mac: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
defaultViewport: {
width: 1920,
height: 1080,
},
args: [
// "--use-fake-ui-for-media-stream",
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
// "--single-process",
"--no-zygote",
"--start-fullscreen",
"--display=" + xvfb._display,
"--window-size=1920,1080",
],
protocolTimeout: 240000, // 240 giây
});
logger.info("Launch browser success!");
logger.info("Init new page . . .");
const page = await browser.newPage();
logger.info("Init page success!");
const targetURL = `${process.env.FE_URL}?meetingLink=${encodeURIComponent(
meetingLink
)}`;
let startTime;
let endTime;
const file = fs.createWriteStream(outputVideoPath);
await page.goto(targetURL, { waitUntil: "load", timeout: 60000 * 4 });
logger.info("Go to page success");
const stream = await getStream(page, { audio: true, video: true });
For the stop code:
if (stream) {
try {
await stream.destroy();
} catch (error) {
logger.error(`Destroy stream error ${error}`);
}
}
try {
file.close();
await browser.close();
(await wss).close();
} catch (error) {
logger.error(`Close puppeteer error ${error}`)
}
try {
xvfb.stop((err) => {
logger.error(`Stop xvfb error ${err}`);
});
} catch (error) {
logger.info(`Catch xvfb close process error ${error}`)
}
Version: "puppeteer-extra": "^3.3.6", "puppeteer-extra-plugin-stealth": "^2.11.2", "puppeteer-stream": "^3.0.19",
Does anyone get same error like me? Thanks
I'm not just why it happens, but it could be because your server is overloaded? have a look at #185
Does it only happen in docker? If it also occurs when running native does it only happen on docker vps or also on your docker pc?
@SamuelScheit Thanks for answer, it happend both in my vps and my docker pc. I have tested, in the first start of docker:
- First I run the process like in my code
- WHile the first process still running and not close page, browser, stream yet, I start second process, and then third process same like the first on. They all working well and pass through the getStream. which mean that the error only happen in process which run after I destroy stream , wss, etc in first run.
@SamuelScheit Hi, I found another things that. the (await wss).close
I've tried comment that and the error not occur anymore. So what that purpose ?. Can I remove that wss close code
yes you can remove it. it is used to close the puppeteer-stream server. this is done, when you are finished with everything and want to exit to program
@SamuelScheit i am building a project that create virtual bot join meeting. So in my case, I dont need to close the wss for each meeting session end ? Does that will causing any problem ? And is there anyway if i close the wss but then I can open it again so my flow still work ?
no problem, you don't need to close the wss server, only if you are force stopping your program
@SamuelScheit even in one time I will have multiple process running seperately (different meeting from different users) right ?
that is fine, the server will automatically choose a free port
@SamuelScheit Okay many thanks