From caa8dd8ec2c9478516624245c2aba3547a92b706 Mon Sep 17 00:00:00 2001 From: Ali Razmjoo Date: Sat, 23 Sep 2023 13:11:02 +0200 Subject: [PATCH 1/3] close the browser after getBrowser() --- server/monitor-types/real-browser-monitor-type.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/monitor-types/real-browser-monitor-type.js b/server/monitor-types/real-browser-monitor-type.js index 34ee18482..609a48ff3 100644 --- a/server/monitor-types/real-browser-monitor-type.js +++ b/server/monitor-types/real-browser-monitor-type.js @@ -221,6 +221,9 @@ class RealBrowserMonitorType extends MonitorType { await context.close(); + // Close the browser instance + await browser.close(); + if (res.status() >= 200 && res.status() < 400) { heartbeat.status = UP; heartbeat.msg = res.status(); From 617634f2afc7fc6a628b71184f36e13dfb949783 Mon Sep 17 00:00:00 2001 From: Ali Razmjoo Date: Mon, 10 Jun 2024 16:45:56 +0200 Subject: [PATCH 2/3] Update real-browser-monitor-type.js --- .../real-browser-monitor-type.js | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/server/monitor-types/real-browser-monitor-type.js b/server/monitor-types/real-browser-monitor-type.js index 3c233050f..46cd63487 100644 --- a/server/monitor-types/real-browser-monitor-type.js +++ b/server/monitor-types/real-browser-monitor-type.js @@ -228,6 +228,22 @@ async function testRemoteBrowser(remoteBrowserURL) { throw new Error(e.message); } } + +/** + * Cleanup function to terminate all browser processes and clear cache after each monitoring check. + * @returns {Promise} + */ +async function cleanupBrowser() { + if (browser) { + const contexts = browser.contexts(); + for (const context of contexts) { + await context.clearCookies(); + await context.clearPermissions(); + await context.close(); + } + } +} + class RealBrowserMonitorType extends MonitorType { name = "real-browser"; @@ -251,10 +267,7 @@ class RealBrowserMonitorType extends MonitorType { path: path.join(Database.screenshotDir, filename), }); - await context.close(); - - // Close the browser instance - await browser.close(); + await cleanupBrowser(); // Ensure cleanup is called after each monitoring check if (res.status() >= 200 && res.status() < 400) { heartbeat.status = UP; From 4e667c9adb93aaa6eddb19bf1e617c2d059dfb0d Mon Sep 17 00:00:00 2001 From: Ali Razmjoo Date: Tue, 11 Jun 2024 13:20:00 +0200 Subject: [PATCH 3/3] close the current page context --- .../monitor-types/real-browser-monitor-type.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/server/monitor-types/real-browser-monitor-type.js b/server/monitor-types/real-browser-monitor-type.js index 46cd63487..03977a520 100644 --- a/server/monitor-types/real-browser-monitor-type.js +++ b/server/monitor-types/real-browser-monitor-type.js @@ -231,16 +231,14 @@ async function testRemoteBrowser(remoteBrowserURL) { /** * Cleanup function to terminate all browser processes and clear cache after each monitoring check. + * @param {import ("playwright-core").Page} page The page to close * @returns {Promise} */ -async function cleanupBrowser() { - if (browser) { - const contexts = browser.contexts(); - for (const context of contexts) { - await context.clearCookies(); - await context.clearPermissions(); - await context.close(); - } +async function cleanupBrowser(page) { + if (page) { + await page.context().clearCookies(); + await page.context().clearPermissions(); + await page.close(); } } @@ -267,7 +265,7 @@ class RealBrowserMonitorType extends MonitorType { path: path.join(Database.screenshotDir, filename), }); - await cleanupBrowser(); // Ensure cleanup is called after each monitoring check + await cleanupBrowser(page); // Ensure cleanup is called after each monitoring check if (res.status() >= 200 && res.status() < 400) { heartbeat.status = UP;