electron: [Bug]: 'close' event is not triggered for sandboxed windows

Preflight Checklist

Electron Version

12.0.1, 11.3.0, 10.4.1

What operating system are you using?

Other Linux

Operating System Version

Linux workstation 5.11.6-arch1-1 #1 SMP PREEMPT Thu, 11 Mar 2021 13:48:23 +0000 x86_64 GNU/Linux

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

close event should be triggered when the webpage closes itself with window.close(), as it is for non-sandboxed contexts.

Actual Behavior

close event is not triggered on window.close() from inside the webpage for sandboxed contexts.

Testcase Gist URL

https://gist.github.com/cecc82a9d8ee56d01aa97b4c716eea9e

Testcase:

const { app, BrowserWindow } = require('electron')
app.whenReady().then(() => {
  const win = new BrowserWindow({
    width: 800, height: 600,
    webPreferences: {
      // sandbox: false, // works
      sandbox: true, // broken
    }
  })
  win.loadFile('about:blank')
  win.on('close', () => { console.log('close') }) // not triggered if broken
  win.on('closed', () => { console.log('closed') })
  setTimeout(() => {
    win.webContents.executeJavaScript('window.close()') // broken
    // win.close() // works
  }, 0)
})

Here, executeJavaScript is not necessary – the page can do the same itself, or that could be alternatively done from devtools.

  • If the window is closed externally (via .close() method of BrowserWindow instance, or via Alt-F4 or other OS/DE method), close event is always triggered.

  • If sandbox is not enabled, close event is always triggered.

  • closed (as opposed to close) event is also triggered if the window was closed.

  • If the window closes itself with window.close() and it’s sandboxed – close event doesn’t get triggered.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 19 (9 by maintainers)

Most upvoted comments

Interestingly, win.webContents.on("close") does get emitted.

NB. I copied your repro to a Fiddle-compatible gist for easy Fiddling. I can indeed reproduce this.