playwright
playwright copied to clipboard
[BUG] exception thrown => undefined: Browser has been closed when attempting to close the browser
Context:
- Playwright Version: [what Playwright version do you use?] 1.23.4
- Operating System: [e.g. Windows, Linux or Mac] Windows
- Node.js version: [e.g. 12.22, 14.6]
- Browser: [e.g. All, Chromium, Firefox, WebKit] chromium
- Extra: We are running our tests using BuildKite => Docker => Browserstack combo
System:
- OS: Windows 10 10.0.22000
- Memory: 641.53 MB / 15.79 GB
Binaries:
- Node: 17.7.1 - C:\Program Files\nodejs\node.EXE
- Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD
- npm: 8.5.2 - C:\Program Files\nodejs\npm.CMD
Languages:
- Bash: 5.0.17 - C:\Windows\system32\bash.EXE
Code Snippet
Describe the bug
Hi,
We recently attempted to upgraded to PW 1.23.4 and started encountering random issues below when the tests attempts to close the browser using await browser.close() within our playwright_global_setup.ts file :

pw:api ^ browserContext.storageState succeeded +292ms
pw:api =* browser.close started +812ms
Waiting for the debugger to disconnect...
node :internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
undefined: Browser has been closed
at captureRawStack (c : \_repo\/j^M. M ■ ■■■ ' p t\node_modules\playwright-core\lib\utils\stackTrace. js:62:17)
Initially our code to close the browser looked like this:
await browser.close();
We tried all sorts of stuff to try and catch the exception being:
using async/await ...
if (browser.isConnected()) {
console.log(
'🚀 browser.isConnected() > attempting to close',
browser.isConnected()
);
try {
await browser.close();
} catch (error) {
console.log(`/r/n❌ browser.close() failed > error: ${error}`);
}
}
Using promises ...
if (browser.isConnected()) {
console.log('🚀 browser.isConnected() > attempting to close', browser.isConnected());
browser
.close()
.then(() => {
console.log('✅ browser closed successfully');
})
.catch((error) => {
console.log(`/r/n❌ browser.close() failed > error: ${error}`);
});
}
But it did not work :(
Unfortunately, our setup is quite convoluted to replicate. We are running our tests using BuildKite => Docker => Browserstack combo
As for the tests, it is not really doing anything special. Login => Navigate to some pages => Close the browser
I also have seen "Browser has been closed" while closing, with Browserstack. It is not reproducible, just happens sometimes. We use Playwright Java, 1.22.0.
@ryanrosello-og If this reproduces for you consistently, could you please try running with @playwright/test@next version and PWDEBUGIMPL=1 environment variable, and copy the output here? Hopefully, we get better error details and will be able to diagnose the issue.
hey @dgozman , I seem to be encountering a different error from above using @playwright/test@next
<html>
<body>
<!--StartFragment-->
Running 55 tests using 4 workers, shard 1 of 4
--
| Renewing session state by re-login
| ℹ️ Executing in BrowserStack against windows 11
| :browserstack: running playwright_global_setup shard:1 in BrowserStack with build name 1a. PRD:8873
| ✅ Connection to BrowserStack for playwright_global_setup shard:1 took 6 seconds
|
| 🔃 Retrying global setup... attempt: 1
| Error: Error: browser.newContext: Error: requestContext: expected channel APIRequestContext
<!--EndFragment-->
</body>
</html>```
@dgozman I then rolled back to 1.23.4, rerun the tests with the PWDEBUGIMPL set, here's the output
🚀 browser.isConnected() > attempting to close true
--
| node:internal/process/promises:279
| triggerUncaughtException(err, true /* fromPromise */);
| ^
|
| undefined: Browser has been closed
| at captureRawStack (/app/node_modules/playwright-core/lib/utils/stackTrace.js:62:17)
| <inner error>
| Error: Browser has been closed
| at Connection.close (/app/node_modules/playwright-core/lib/client/connection.js:226:70)
| at Browser.close (/app/node_modules/playwright-core/lib/client/browser.js:125:64)
| at globalSetup (/app/playwright/playwright_global_setup.ts:256:8)
| at processTicksAndRejections (node:internal/process/task_queues:96:5)
| at _default (/app/playwright/playwright_global_setup.ts:31:7)
| at /app/node_modules/@playwright/test/lib/runner.js:504:9
| at Runner._runAndReportError (/app/node_modules/@playwright/test/lib/runner.js:520:7)
| at Runner._performGlobalSetup (/app/node_modules/@playwright/test/lib/runner.js:490:5)
| at Runner._runFiles (/app/node_modules/@playwright/test/lib/runner.js:427:28)
| at Runner._run (/app/node_modules/@playwright/test/lib/runner.js:224:12)
| at TimeoutRunner.run (/app/node_modules/playwright-core/lib/utils/timeoutRunner.js:53:14)
| at raceAgainstTimeout (/app/node_modules/playwright-core/lib/utils/timeoutRunner.js:113:15)
| at Runner.runAllTests (/app/node_modules/@playwright/test/lib/runner.js:182:20)
| at runTests (/app/node_modules/@playwright/test/lib/cli.js:162:18)
| at Ai.<anonymous> (/app/node_modules/@playwright/test/lib/cli.js:70:7)
@ryanrosello-og Oh, right, using @next will not match the playwright version on the BrowserStack side. I guess we'll need to wait for you and BrowserStack to update to v1.25 that is coming soon.
@ryanrosello-og Could you try again with latest 1.25 version and PWDEBUGIMPL=1 environment variable?
hey @dgozman I raised a ticket with BrowserStack concerning this problem and they acknowledged that this issue on their end. Currently, they have yet to support v1.25 and provided me with no ETA on when they will fix this issue.
@ryanrosello-og Sounds like we should close this issue then, shouldn't we? Also, if you could share a public link to the BrowserStack issue, if there is any, that would help others in the future.
sounds good, if its still problematic after their fix. I will just open a new issue and link to here. Unfortunately, no public link exists for the browserstack issue. All correspondence was done via email