playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG] Playwright can not close the browser when running with Chrome (no-headless mode)

Open honglax opened this issue 5 years ago • 28 comments

Context:

  • Playwright Version: 1.8.0
  • Operating System: macOS 11.2
  • Node.js version: 12.18.4
  • Browser: Chrome (version 88.0.4324.146)
  • Extra: None

Code Snippet

const playwright = require('playwright')

(async () => {
  const browser = await playwright['chromium'].launch({
    executablePath:
      '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    headless: false,
  })
  const context = await browser.newContext()
  const page = await context.newPage()
  await page.goto('https://google.com', { waitUntil: 'load' })
  const cookies = await page.context().cookies()
  await browser.close()
})()

Describe the bug When running the test with no-headless Chrome (also happens with Chrome Dev, and Chrome Canary), the test is hung at await browser.close() and can not close the browser.

honglax avatar Feb 05 '21 13:02 honglax

I also got this issue

phileba avatar Feb 05 '21 13:02 phileba

Note that Playwright is only guaranteed to work with bundled browsers, so I expect some internal protocol mismatch. That said, if you could generate protocol logs for us, we would look into them for more details. For that, run with two environment variables and copy mylog.txt here.

$ DEBUG=pw:protocol DEBUG_FILE=mylog.txt node myscript.js

dgozman avatar Feb 05 '21 23:02 dgozman

Note that Playwright is only guaranteed to work with bundled browsers, so I expect some internal protocol mismatch. That said, if you could generate protocol logs for us, we would look into them for more details. For that, run with two environment variables and copy mylog.txt here.

$ DEBUG=pw:protocol DEBUG_FILE=mylog.txt node myscript.js

Here it is. chrome-bug.txt

honglax avatar Feb 07 '21 07:02 honglax

Logs indicate there were no issues while handling the Browser.close command. I also tried this script with Chrome Canary (version 90.0.4412.0) on MacOS 10.15 and everything works.

Did you try playwright v1.6? It roughly corresponds to Chromium 88.0.4316.0, so I expect things to work there. Overall, you can find out the version of bundled browsers in our (release notes)[https://github.com/microsoft/playwright/releases]. We usually expect similar version to work with Playwright, but older versions might break.

dgozman avatar Feb 08 '21 19:02 dgozman

Same issue here for me (await browser.close() hangs):

  • Chrome Version 88.0.4324.150 (Official Build) (64-bit)
  • Python playwright 1.8.0.a1
  • non-headless
  • Windows 10 Prof (64-bit) 1909

pohlt avatar Feb 11 '21 08:02 pohlt

I had the same problem until I downgraded to Playwright v1.6.0. That seems to work with Chrome binary on Mac OS and closes a headfull browser.

tommulkins avatar May 04 '21 14:05 tommulkins

I also have this with v1.11.0 with headfull, note headless is fine. This is the setup.ts file for my suites:

import { Browser, chromium, Page } from "playwright";

let browser: Browser;
let page: Page

before(async () => {
  browser = await chromium.launch({ headless: false });
});

after(async () => {
  await browser.close();
});

beforeEach(async () => {
  page = await browser.newPage();
});

afterEach(async () => {
  await page.close();
});

export { page, browser }

jonny7 avatar May 14 '21 18:05 jonny7

+1 same issue with headful on v1.11.0. Process just hangs forever even with calling browser.close(); This should be considered a major bug that impacts all headful workflows imo.

delimmy avatar May 22 '21 17:05 delimmy

This is happening when I'm using the "chrome" stable channel (so it launches real Chrome), but I'm not seeing the issue with Chromium.

delimmy avatar May 22 '21 19:05 delimmy

Actually, I wasn't closing the browser properly, but it is still odd that the process hangs on chrome stable but not when "channel" is not specified when launching the browser.

delimmy avatar May 22 '21 21:05 delimmy

I also observed the same issue with Playwright 1.12.3, and Chrome 91.0.44.72.101 (32 bit). Test execution was fast but it looks like browser closure is taking ages before forcefully killing it.

pw:api => browserContext.close started +1ms
  pw:api <= browserContext.close succeeded +40ms
  pw:api => browser.close started +16ms
  pw:browser [pid=54900] <gracefully close start> +3s
  pw:browser [pid=54900] <kill> +30s
  pw:browser [pid=54900] <will force kill> +1ms
  pw:browser [pid=54900] taskkill output: SUCCESS: The process with PID 53636 (child process of PID 54900) has been terminated.
  pw:browser SUCCESS: The process with PID 54480 (child process of PID 54900) has been terminated.
  pw:browser SUCCESS: The process with PID 56632 (child process of PID 54900) has been terminated.
  pw:browser SUCCESS: The process with PID 11660 (child process of PID 54900) has been terminated.
  pw:browser SUCCESS: The process with PID 54480 (child process of PID 54900) has been terminated.
  pw:browser SUCCESS: The process with PID 54900 (child process of PID 54996) has been terminated.
  pw:browser +516ms
  pw:browser [pid=13148] starting temporary directories cleanup +0ms
  
  1 passed(37s)

mailtogagan avatar Jul 20 '21 03:07 mailtogagan

I got the same problem on Playwright Python 1.15.2. The browser context close() hanged when the context was launched in the headful mode by browser_type.launch_persistent_context() with a record_har_path to record a HAR archive. The context closed normally without the record_har_path (set to None). A workaround is to open a new tab, then close all other tabs, then close the browser context.

ducalpha avatar Oct 03 '21 20:10 ducalpha

I have the same issue with Playwright 1.19.2 (TypeScript), when I enable channel: "chrome", the tests are failing because browser.close() "hangs" and playwright is not able to start new browser for next test scenario.

TimurNurlygayanov avatar Apr 06 '22 11:04 TimurNurlygayanov

We are experiencing the same issues. Locally and within a Github Action.

Node: 14.18.2 playwright/test: 1.21.1 Ubuntu 20.04 I believe this is both Chrome & Firefox.

In Headless: False mode. I can see a browser not close and remain open until the suite complete and all closes as expected.

In headless: True the workflow completes what it can per set workers/browsers, and then when all else is done, the workflow just sits there.

In github, when I cancel to workflow/test, in the post clean up output I see something like this:

Cleaning up orphan processes
Terminate orphan process: pid (10634) (sh)
Terminate orphan process: pid (10647) (node)
Terminate orphan process: pid (10659) (sh)
Terminate orphan process: pid (10660) (node)
Terminate orphan process: pid (10689) (node)
Terminate orphan process: pid (13358) (firefox)
Terminate orphan process: pid (14095) (WebExtensions)

This is what leads me to believe this is also a firefox issue.

NickDuBois avatar Apr 29 '22 18:04 NickDuBois

I have the same issue with chromium. I am using:

  • CucumberJs
  • "@playwright/test": "1.26.0" (TypeScript)
  • headless = false

The browser.close(); takes too long.

beloquintana avatar Sep 25 '22 12:09 beloquintana

Facing same issue. browser is not closing and test timesout. For me if I remove recordHar it works properly

akhilputhiry avatar Dec 16 '22 07:12 akhilputhiry

I have the same issue with chromium browser chrome channel (no issue for msedge). I am using:

  • Playwright Version: 1.35.0
  • Operating System: Windows 11 Enterprise (no issue in linux docker container for dotnet playwright)
  • .NET version: 6.0
  • Browser: Chrome (version 114.0.5735.199)
  • Headless: false and also "--headless=new" (no issue for "--headless=old" option)

await IBrowser.CloseAsync() takes 30 seconds and then times out.

Attaching logs for pw:protocol and pw:browser mylog_protocol.txt

aandrzejczuk avatar Jul 06 '23 13:07 aandrzejczuk

This is still happening for all Chromium based browser macOS:

My setup:

  • macOS: 13.5 (22G74)
  • Node.js 20
  • "@playwright/test": "^1.37.0"
  • Worth noting that my Chrome is managed by Chrome Enterprise

I'm running the following script:

example.js
const { chromium, webkit, firefox } = require('@playwright/test');

async function main() {
  const msedge = await chromium.launch({
    channel: 'msedge',
    headless: false,
  });
  console.log('\n\n*** MS Edge ***');
  await test(msedge);

  const chrome = await chromium.launch({
    channel: 'chrome',
    headless: false,
  });
  console.log('\n\n*** Chrome ***');
  await test(chrome);

  const chromiumBrowser = await chromium.launch({
    channel: 'chrome',
    headless: false,
  });
  console.log('\n\n*** Chromium ***');
  await test(chromiumBrowser);

  const webkitBrowser = await webkit.launch({
    headless: false,
  });
  console.log('\n\n*** Webkit ***');
  await test(webkitBrowser);

  const firefoxBrowser = await firefox.launch({
    headless: false,
  });
  console.log('\n\n*** Firefox ***');
  await test(firefoxBrowser);
}

async function test(browser) {
  const context = await browser.newContext();
  const page = await context.newPage();

  console.log(new Date().toLocaleTimeString(), 'Navigating to https://example.com');
  await page.goto('https://example.com', { waitUntil: 'load' });

  console.log(new Date().toLocaleTimeString(), 'Closing browser...');
  console.time('Closing browser took');
  await browser.close();
  console.timeEnd('Closing browser took');
}

process.on('unhandledRejection', (reason, promise) => {
  console.log('Unhandled Rejection at:', reason.stack || reason);
});

process.on('uncaughtException', (error) => {
  console.log('Uncaught Exception thrown', error);
});

process.on('exit', (code) => {
  console.log(new Date().toLocaleTimeString(), `About to exit with code: ${code}`);
});

main()
  .then(() => {
    console.log(new Date().toLocaleTimeString(), 'Script finished');
  })
  .catch((err) => {
    console.error('Error during playwright script', err);
  });

I'm getting the following output:

*** MS Edge ***
1:51:03 PM Navigating to https://example.com
1:51:12 PM Closing browser...
Closing browser took: 30.035s


*** Chrome ***
1:51:43 PM Navigating to https://example.com
1:51:44 PM Closing browser...
Closing browser took: 30.068s


*** Chromium ***
1:52:14 PM Navigating to https://example.com
1:52:15 PM Closing browser...
Closing browser took: 30.048s


*** Webkit ***
1:52:46 PM Navigating to https://example.com
1:52:46 PM Closing browser...
Closing browser took: 18.404ms


*** Firefox ***
1:52:48 PM Navigating to https://example.com
1:52:48 PM Closing browser...
Closing browser took: 2.054s
1:52:50 PM Script finished
1:52:50 PM About to exit with code: 0

Closing a Chromium browser takes exactly 30 seconds! There must be a timeout somewhere... Other browsers (Firefox and Webkit) are working correctly.

context.close does not help

Closing all contexts before browser.close() does not fix the issue:

  const contexts = await browser.contexts();
  await Promise.all(contexts.map((ctx) => ctx.close()));
  await browser.close();

mohsen1 avatar Aug 23 '23 11:08 mohsen1

@mohsen1 Enterprise policies may interfere with Playwright controlling the browser. We consider this case to be out of scope, so it's unlikely things will improve in this area.

dgozman avatar Aug 23 '23 15:08 dgozman

Thank you @dgozman, in a Mac without Chrome Enterprise the close times are around 2 seconds. It's surprising that Chrome Enterprise is impacting all of Chromium based browsers. I understand that this is outside of the scope of Playwright but do you have any suggestions where can I look for possible solutions?

mohsen1 avatar Aug 23 '23 20:08 mohsen1