Skip to content

Commit 282e58a

Browse files
committed
fix(Playwright): Properly clean up event listeners to prevent hanging
- Remove all page event listeners (crash, dialog, load, console, requestfinished) in _setPage - Remove all browser event listeners in _stopBrowser before closing - Add error handling for listener cleanup to prevent failures - Wrap browser.close() and browserContext.close() in try-catch blocks This should help prevent the Node.js event loop from staying alive due to uncleaned event listeners, though additional investigation may be needed for complete resolution of process hanging after tests complete.
1 parent 4968282 commit 282e58a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lib/helper/Playwright.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,9 @@ class Playwright extends Helper {
10561056
try {
10571057
this.page.removeAllListeners('crash')
10581058
this.page.removeAllListeners('dialog')
1059+
this.page.removeAllListeners('load')
1060+
this.page.removeAllListeners('console')
1061+
this.page.removeAllListeners('requestfinished')
10591062
} catch (e) {
10601063
console.warn('Warning cleaning previous page listeners:', e.message)
10611064
}
@@ -1284,9 +1287,32 @@ class Playwright extends Helper {
12841287
this.context = null
12851288
this.frame = null
12861289
popupStore.clear()
1287-
if (this.options.recordHar) await this.browserContext.close()
1290+
1291+
// Remove all event listeners to prevent hanging
1292+
if (this.browser) {
1293+
try {
1294+
this.browser.removeAllListeners()
1295+
} catch (e) {
1296+
// Ignore errors if browser is already closed
1297+
}
1298+
}
1299+
1300+
if (this.options.recordHar && this.browserContext) {
1301+
try {
1302+
await this.browserContext.close()
1303+
} catch (e) {
1304+
// Ignore errors if context is already closed
1305+
}
1306+
}
12881307
this.browserContext = null
1289-
await this.browser.close()
1308+
1309+
if (this.browser) {
1310+
try {
1311+
await this.browser.close()
1312+
} catch (e) {
1313+
// Ignore errors if browser is already closed
1314+
}
1315+
}
12901316
this.browser = null
12911317
this.isRunning = false
12921318
}

0 commit comments

Comments
 (0)