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

Can't close browser window when detached devtools window is opened (electron 10) #25012

Open
Treverix opened this issue Aug 18, 2020 · 21 comments
Open

Comments

@Treverix
Copy link

@Treverix Treverix commented Aug 18, 2020

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:

v10.0.0-beta.21

  • Operating System:

Windows 10 (1909)

  • Last Known Working Electron version:

v9.1.0

Expected Behavior

If I have a browser window and a detached devtools window for that opened, then I can close the browser window and the devtools window closes also.

Actual Behavior

I can't close the browser window. I have to manually close the detached devtools window first, then I can close the browser window.

@sofianguy sofianguy added this to Unsorted Issues in 10-x-y Aug 18, 2020
@sofianguy sofianguy moved this from Unsorted Issues to Does Not Block Stable in 10-x-y Aug 19, 2020
@zcbenz
Copy link
Member

@zcbenz zcbenz commented Aug 20, 2020

I'm unable to reproduce this issue with following steps:

  1. Open default app
  2. Open devtools and detach it
  3. Close the main window

Does this issue only happen within your app?

@Treverix
Copy link
Author

@Treverix Treverix commented Aug 21, 2020

Well. The quick-start application works for me too (updated to 10.0.0-beta.21)

But my application still has that effect and, maybe it's related, the DOM element hightlighting does not work anymore in my app. Like when I hover over a node in the Elements view in inspect mode, there's no highlighting, no tooltips on the browser window.

I try to replicate it now on the quick start by adding code that I use in my app one by one, checking if that cause the trouble but I'm pretty lost this time, as I don't really know which feature/API could potentially block the communication between browser window and devtools. Could be some electron code or it could also be an incompatible npm module.

At least, it's on all of my browser windows, of which some have svelte apps and some (most) simply show web pages from our external intranet applications. So it's one thing all browser windows have in common or it's something with the general electron app setup.

@Treverix
Copy link
Author

@Treverix Treverix commented Aug 21, 2020

Found the issue - it's the custom-electron-titlebar which is not compatible with electron 10 and to make things worse, they just archived it so I need to fix it myself :/

To reproduce:

  • use the electron quickstart
  • remove the content security metas from the index.html
  • add custom-electron-titlebar via package.json
  • add enableRemoteModule: true to the webPreferences (it uses the remote module)
  • add this to the preload, inside the listener:
  const {Color, Titlebar} = require("custom-electron-titlebar");
  new Titlebar({
    backgroundColor: Color.fromHex('#f5f5f5'),
    unfocusEffect: false,
    minimizable: true,
    maximizable: true,
    menu: null,
    shadow: false,
  });

Now, if we set frame:false on the BrowserWindow options, we can't close the windows while devtools are open. Otherwise, with frame: true, it works.

Note that typing window.close() in the devtools console also does not close the window. There is no error message observed. As if some close listener was setup with 'prevent default'

I can't tell if this is an incompatible 3rd party lib or if that lib reveals a bug in electron 10, so I keep this issue open.

Unfortunately, this doesn't seem to be responsible for the 'highlight on hover' issue that I also have with electron 10.

@Treverix
Copy link
Author

@Treverix Treverix commented Aug 21, 2020

Finally I found the cause and it's an error in electron. This show the problem w/o a custom library. To reproduce:

  1. create a project with the quickstart app
  2. add enableRemoteModule: true to the webPreferences (it uses the remote module)
  3. set frame: false on the browser window options
  4. add a button on the index.html with id closeButton
  5. for the preload, use the following script:
const {remote} = require('electron');

function onFocus() {
  console.log('dummy');
}
remote.getCurrentWindow().on('focus', onFocus);

window.addEventListener('beforeunload', () => {
  // removing listeners from the browser window that we get via remote
  // prevents closing the window only if the dev tools are shown.
  remote.getCurrentWindow().removeListener('focus', onFocus);
})

window.addEventListener('DOMContentLoaded', () => {
  document.getElementById('closeButton').addEventListener('click', () => {
    remote.getCurrentWindow().close();
  })
})

Start the app, open the dev tools (any mode).

Expected behavior:
if we click the button on the screen, the window is closed

Observed behavior
if we click the button on the screen, the window is not closed. We need to close the devtools first.

Hope that helps!

@zcbenz
Copy link
Member

@zcbenz zcbenz commented Aug 26, 2020

Thanks for debugging this issue, I can confirm it is a bug of Electron.

@zcbenz zcbenz assigned zcbenz and unassigned zcbenz Aug 26, 2020
@zcbenz
Copy link
Member

@zcbenz zcbenz commented Aug 26, 2020

Also confirmed it is a Windows-only bug, it can be reproduced with this app:
https://gist.github.com/zcbenz/d8a36146cf57706959cf00252746d4a1

@rpsfonseca
Copy link

@rpsfonseca rpsfonseca commented Oct 2, 2020

I may look into this, is that ok for you?

@RedHoodJT1988
Copy link

@RedHoodJT1988 RedHoodJT1988 commented Oct 5, 2020

So this is an issue only for the Windows OS or does this happen on other OS's as well? I wouldn't mind helping out if it's Windows only as I use Windows daily. Let me know.

@pravichandran15
Copy link

@pravichandran15 pravichandran15 commented Nov 10, 2020

In case if you are handling the close in Windows may be below can help,

const currentWindow = window.require('electron').remote.getCurrentWindow();
if (currentWindow.isDevToolsOpened()) {
currentWindow.closeDevTools();
}
currentWindow.close();

@PandaTsui
Copy link

@PandaTsui PandaTsui commented Nov 16, 2020

In case if you are handling the close in Windows may be below can help,

const currentWindow = window.require('electron').remote.getCurrentWindow();
if (currentWindow.isDevToolsOpened()) {
currentWindow.closeDevTools();
}
currentWindow.close();

Wow. This is an excellent solution in a temporary development environment

@backboy
Copy link

@backboy backboy commented Nov 16, 2020

I am new to electron but have C++ and Java experience. Can I take this issue?

@willium
Copy link

@willium willium commented Jan 10, 2021

In case if you are handling the close in Windows may be below can help,

const currentWindow = window.require('electron').remote.getCurrentWindow();
if (currentWindow.isDevToolsOpened()) {
currentWindow.closeDevTools();
}
currentWindow.close();

It seems these methods aren't documented/typed anywhere. Is there a good place to look them up?

@stanleyxu2005
Copy link

@stanleyxu2005 stanleyxu2005 commented Jan 17, 2021

Thank you @Treverix for bringing this up! Different to you, I dont see the problem with v10.x but I can confirm this bug exists in v11.0, v11.1, v11.2 on Windows.

This is unfortunately the major issue prevents me to upgrade projects to latest Electron, until I created my version of workaround:

function fixBrowserWindowCannotCloseWhenDevToolsIsOpened(win) {
  // bug that main browser window cannot close, if devtools window is showing
  // https://github.com/electron/electron/issues/25012
  win.on('close', () => {
    if (win.webContents.isDevToolsOpened()) {
      win.webContents.closeDevTools()
    }
  })
}

Include it, call it and enjoy it!

@bromix
Copy link

@bromix bromix commented Mar 18, 2021

Same issue for me with Electon 11.3.0 and frame = false. I used also the function to fix the problem...but I also need to call window.destroy().
If I create the electron application in staging or production, this problem doesn't occur.

@kutu
Copy link

@kutu kutu commented Apr 13, 2021

fix for me was to set show: false, and then on ready-to-show event show the window, after that if devtools opened window closes as expected and without delay

electron version 12.0.2

@cawa-93
Copy link

@cawa-93 cawa-93 commented Apr 29, 2021

I can confirm this issue. Electron v12.0.5. Windows.

BrowserWindow with autoHideMenuBar: true or frame: false doest close if devTools opened (detached or attached).

The window does not close either with native system controls or on call require('@electron/remote').getCurrentWindow().close().

Workarounds that work for me:

  1. I see a delay (about a second) between clicking the "close" button and actually closing the window.
// main.js
  win.on('close', () => {
    if (win.webContents.isDevToolsOpened()) {
      win.webContents.closeDevTools()
    }
  })
  1. The window closes instantly
// main.js
  mainWindow = new BrowserWindow({ show: false });
  mainWindow.addListener('ready-to-show', () => mainWindow?.show());
cawa-93 added a commit to cawa-93/vite-electron-builder that referenced this issue May 3, 2021
@Migushthe2nd
Copy link

@Migushthe2nd Migushthe2nd commented May 15, 2021

I am also facing this issue, however with the frame enabled:

{
    frame: true,
    autoHideMenuBar: true,
}

Changing frame to false has no effect. Setting autoHideMenuBar to false fixes the issue and I am once again able to close the window by clicking the X button. Only a single BrowserWindow is opened and devtools is opened (attached/windowed).
Like @cawa-93 said, there still is around a second of delay before the window closes.

Tested electron versions: 12.0.2, 12.0.7

fanly pushed a commit to fanly/fanlymenu that referenced this issue May 19, 2021
@cawa-93
Copy link

@cawa-93 cawa-93 commented May 26, 2021

This Issue Still relevant for v13.0.1.
@zcbenz, May you update project and labels?

@ksshim86
Copy link

@ksshim86 ksshim86 commented May 27, 2021

In case if you are handling the close in Windows may be below can help,

const currentWindow = window.require('electron').remote.getCurrentWindow();
if (currentWindow.isDevToolsOpened()) {
currentWindow.closeDevTools();
}
currentWindow.close();

thank you!!

@asakiasako
Copy link

@asakiasako asakiasako commented May 30, 2021

fix for me was to set show: false, and then on ready-to-show event show the window, after that if devtools opened window closes as expected and without delay

electron version 12.0.2

I think this is a better workaround, it works for my version 12.0.0

@quenchaman
Copy link

@quenchaman quenchaman commented May 30, 2021

Can’t we fix this? These work-arounds cost time and money to our guild.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
10-x-y
Does Not Block Stable
Linked pull requests

Successfully merging a pull request may close this issue.

None yet