Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Electron firstWindow() timeouts unless you open the devtools #21117

Closed
christianlundberg opened this issue Feb 22, 2023 · 6 comments
Closed

Comments

@christianlundberg
Copy link

christianlundberg commented Feb 22, 2023

Context:

System:

  • OS: macOS 12.6
  • Memory: 118.43 MB / 16.00 GB

Binaries:

  • Node: 14.19.2 - ~/.nvm/versions/node/v14.19.2/bin/node
  • npm: 6.14.17 - ~/.nvm/versions/node/v14.19.2/bin/npm

Languages:

  • Bash: 3.2.57 - /bin/bash

npmPackages:

  • playwright: 1.31.0 => 1.31.0
  • playwright-core: 1.31.0 => 1.31.0

Code Snippet

Help us help you! Put down a short code snippet that illustrates your bug and
that we can run and debug locally. For example:

export const startApp = async () => {
  const buildDir = findLatestBuild();
  const { executable, main } = parseElectronApp(buildDir);

  const electronApp = await electron.launch({
    args: [main],
    executablePath: executable,
    env: {
      PLAYWRIGHT: 'true',
    },
  });

  const firstWindow = await electronApp.firstWindow();

  return { electronApp, firstWindow };
};

Describe the bug

When I run npx playwright test , Playwright correctly launches the app, but the promise returned by firstWindow() never resolves and so the tests ultimately timeout. If I manually or programatically open the devtools, the Promise immediately resolves.

I found these previous issues which describe the exact same thing, but even after downgrading to versions 1.29.1 and 1.27.1, the problem remains.

#18928

#19246

Logs when running in debug mode:

 pw:api => selectors.setTestIdAttribute started +0ms
  pw:api => electron.launch started +88ms
  pw:api <= selectors.setTestIdAttribute succeeded +4ms
  pw:browser <launching> [redacted] -r [redacted] --inspect=0 --remote-debugging-port=0 [redacted] +0ms
  pw:browser <launched> pid=86831 +8ms
  pw:browser [pid=86831][err] Debugger listening on ws://127.0.0.1:64551/4f7be882-ed65-469b-b3be-63c13d231754 +266ms
  pw:browser [pid=86831][err] For help, see: https://nodejs.org/en/docs/inspector +0ms
  pw:browser <ws connecting> ws://127.0.0.1:64551/4f7be882-ed65-469b-b3be-63c13d231754 +1ms
  pw:browser <ws connected> ws://127.0.0.1:64551/4f7be882-ed65-469b-b3be-63c13d231754 +20ms
  pw:browser [pid=86831][err] Debugger attached. +0ms
  pw:browser [pid=86831][err]  +742ms
  pw:browser [pid=86831][err] DevTools listening on ws://127.0.0.1:64553/devtools/browser/d42f8797-d407-48d6-9645-6d16fea58ed0 +0ms
  pw:browser <ws connecting> ws://127.0.0.1:64553/devtools/browser/d42f8797-d407-48d6-9645-6d16fea58ed0 +1ms
  pw:browser <ws connected> ws://127.0.0.1:64553/devtools/browser/d42f8797-d407-48d6-9645-6d16fea58ed0 +398ms
  pw:api <= electron.launch succeeded +1s
  pw:api => electronApplication.firstWindow started +3ms
  pw:api <= electronApplication.firstWindow failed +30s
  pw:browser [pid=86831] <kill> +30s
  pw:browser [pid=86831] <will force kill> +0ms
  pw:browser [pid=86831] starting temporary directories cleanup +1ms
  pw:browser [pid=86831] finished temporary directories cleanup +

Logs when opening the devtools manually:

  pw:api => selectors.setTestIdAttribute started +0ms
  pw:api => electron.launch started +86ms
  pw:browser <launching> [redacted]-r [redacted] --inspect=0 --remote-debugging-port=0 [redacted] +0ms
  pw:browser <launched> pid=87003 +9ms
  pw:api <= selectors.setTestIdAttribute succeeded +15ms
  pw:browser [pid=87003][err] Debugger listening on ws://127.0.0.1:64630/b569d0c1-09db-41a1-adb1-2e8e050f8150 +138ms
  pw:browser [pid=87003][err] For help, see: https://nodejs.org/en/docs/inspector +0ms
  pw:browser <ws connecting> ws://127.0.0.1:64630/b569d0c1-09db-41a1-adb1-2e8e050f8150 +1ms
  pw:browser <ws connected> ws://127.0.0.1:64630/b569d0c1-09db-41a1-adb1-2e8e050f8150 +18ms
  pw:browser [pid=87003][err] Debugger attached. +1ms
  pw:browser [pid=87003][err]  +660ms
  pw:browser [pid=87003][err] DevTools listening on ws://127.0.0.1:64632/devtools/browser/10d9f12d-ca38-4292-b5db-ef36787a2579 +0ms
  pw:browser <ws connecting> ws://127.0.0.1:64632/devtools/browser/10d9f12d-ca38-4292-b5db-ef36787a2579 +1ms
  pw:browser <ws connected> ws://127.0.0.1:64632/devtools/browser/10d9f12d-ca38-4292-b5db-ef36787a2579 +343ms
  pw:api <= electron.launch succeeded +1s
  pw:api => electronApplication.firstWindow started +3ms
  pw:api   "commit" event fired +8s
  pw:api   "domcontentloaded" event fired +1ms
  pw:api   "load" event fired +1ms
  pw:api <= electronApplication.firstWindow succeeded +33ms
  pw:browser [pid=87003] <kill> +8s
  pw:browser [pid=87003] <will force kill> +0ms
  pw:browser [pid=87003] starting temporary directories cleanup +0ms
  pw:browser [pid=87003] finished temporary directories cleanup +
@yury-s
Copy link
Member

yury-s commented Feb 22, 2023

Which electron version are you using? I cannot reproduce it on mac with electron 23.1.1, can you shared a repro?

@christianlundberg
Copy link
Author

@yury-s our app is using v21.2.2, but even after upgrading to the latest v23.0.0 the issue remains. I unfortunately cannot share a repro, is there any other information I can give you?

@christianlundberg
Copy link
Author

christianlundberg commented Feb 24, 2023

We've enabled the following fuses: EnableCookieEncryption and OnlyLoadAppFromAsar, I don't know if that helps, but I'm trying to think of very custom code that might be affecting this, because we used to have EnableNodeCliInspectArguments enabled as well which made electron.launch timeout.

@christianlundberg
Copy link
Author

christianlundberg commented Feb 24, 2023

@yury-s Hey, after a ton of debugging I just wanted to let you know I think I found what the issue is.

When we open our main window, we first use the is-online package to determine if there's a connection and if not we just load a fallback page. Our code more or less looks like this:

window.loadURL(await isOnline({
  timeout: 3000,
})) ? mainPageUrl : pathToLocalFallbackPage);

If I replace the isOnline function with a hardcoded true value, Playwright finally is able to resolve the firstWindow promise.

I'm not sure if this is a bug with Playwright though.

@yury-s
Copy link
Member

yury-s commented Feb 27, 2023

Thanks, that's useful information. How long does isOnline take? Can you reduce the code to something that can be shared? I tried with the following simple app and it worked just fine:

const { app, BrowserWindow } = require('electron')
const path = require('path')

async function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  await new Promise(f => setTimeout(f, 1000));
  await win.loadURL('file:///Users/user/sandbox/el/app/index.html');
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

@pavelfeldman
Copy link
Member

We need more information to act on this report. As long as we can't repro it, it is unlikely with can make progress with it. Allow me to close it, but please file a new one and link to this issue when you get back to it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants