Using Puppeteer, my CDP session crashes during an ongoing scroll gesture when page.close() is invoked. How can I properly detach the session to shut down the page safely?
const puppLib = require('puppeteer');
(async () => {
const browserInstance = await puppLib.launch();
const currentPage = await browserInstance.newPage();
const cdpControl = await currentPage.target().createCDPSession();
await cdpControl.send('Input.initiateGesture', { startX: 110, startY: 220, scrollDelta: -130 });
await currentPage.close();
})();
hey, try calling await cdp.detach() before page.close() so the ongoing gesture gets terminated properly. it worked for me when i faced similar issues.
In my experience, the problem is not always with the detachment of the CDP session itself but rather with the timing of the close command in relation to an active gesture. I found that allowing the gesture to complete its action or explicitly issuing a command to terminate the gesture can prevent crashes. One workaround was to wait briefly for the gesture to finish before closing the page. Adjusting the shutdown sequence and synchronizing with the gesture events in your code helped maintain session stability in my tests.
In my experience, the issue can be effectively resolved by actively managing the state of the gesture rather than directly detaching the session and closing the page abruptly. I found that introducing a step to explicitly cancel the ongoing gesture was crucial in preventing unexpected crashes. Using a command such as cdp.send(‘Input.cancelGesture’) to safely halt any active input gestures provides added stability. Moreover, wrapping these operations in a try-catch block to handle any potential errors helped me better manage the session state. This approach ensures that the gesture completes or is properly terminated before the page gets closed.
hey, i solved it by adding a short delay after cancelling the gesture. waiting for a tick lets everything drain before page.close() kicks in. try it and see if it works!
Based on my experience, the key is to ensure that active gestures are fully terminated before detaching the CDP session and closing the page. I managed to resolve similar issues by explicitly sending a cancellation command to end any ongoing gesture. After this, I introduce a short delay to give the browser time to process the cancellation. This delay helps avoid any asynchronous race conditions that can lead to a crash. Furthermore, wrapping these steps within proper error handling further improves stability. This sequence of canceling, waiting, and then closing proved effective in my tests.