asticode / go-astilectron

Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
MIT License
4.9k stars 344 forks source link

Close vs Quit - Windows bug? #232

Closed chinenual closed 4 years ago

chinenual commented 4 years ago

I'm cross-compiling from macos for windows. The application works in windows the first time I launch it, but if I close the main window with the Windows [X] close button, the application cannot be restarted until I reboot Windows. If I close the application from the Menu (the quit option bound to MenuItemRoleQuit, the application can be relaunched without rebooting Windows. For what it's worth the windows close button works fine on MacOS. Ideas?

{
           // Override the "Quit Electron" label
           Label: astikit.StrPtr("Quit MyApp"),
           Role: astilectron.MenuItemRoleQuit,
},

I'm using the following environment string in bundler.json:

...
        {"arch" : "amd64", "os" : "windows"},
...
asticode commented 4 years ago

Mmmm off the bat I don't see where that would be coming from... I'll have to investigate even though I won't have much time in the coming months

chinenual commented 4 years ago

I'd like to help diagnose. But at this point I don't have much to go on. I can't find a way to even get logs -- while the following logs to both stderr and a local file in macos, I get no logging at all on Windows:

    multi := io.MultiWriter(os.Stderr,
        &lumberjack.Logger{
            Filename:   "myapp.log",
            MaxSize:    5, // megabytes
            MaxBackups: 2,
            Compress:   false, 
        })
    log.SetOutput(multi)
    l = log.New(log.Writer(), log.Prefix(), log.Flags() | log.Lshortfile)

I'll at least see if I can reproduce this on the astilectron-demo project

chinenual commented 4 years ago

Good news -- the Windows [X] button works fine on the astilectron-demo project using the same bundler.json as in the problem project. So it must be something I've done to myself :)

I was also able to determine why the MultiWriter log isnt working on Windows - apparently the write to stderr fails, so the MultiWriter just stops -- by changing the order of the two writers in the MultiWriter setup, I can get logs out of windows (write to the file works, then tries to write to stderr and fails - but that's ok, I've got info in the file...

Will report back if I can glean anything interesting from the bad project's log

chinenual commented 4 years ago

Well - not a lot of anything in the log. Not sure this helps:

2020/04/22 14:02:16 logger.go:78: astikit: starting worker...
2020/04/22 14:02:16 logger.go:66: Skipping restoring resources...
2020/04/22 14:02:16 logger.go:66: Starting...
2020/04/22 14:02:16 logger.go:66: Provisioning...
2020/04/22 14:02:16 logger.go:67: Astilectron has already been provisioned to version 0.36.0, moving on...
2020/04/22 14:02:16 logger.go:67: Electron has already been provisioned to version 7.1.10, moving on...
2020/04/22 14:02:16 logger.go:66: Listening...
2020/04/22 14:02:16 logger.go:66: Executing...
2020/04/22 14:02:16 logger.go:67: Starting cmd C:\Users\steve\AppData\Roaming\Synergize\vendor\electron-windows-amd64\electron.exe C:\Users\steve\AppData\Roaming\Synergize\vendor\astilectron\main.js 127.0.0.1:49786 true
2020/04/22 14:02:16 logger.go:67: Stdout says: 
2020/04/22 14:02:16 logger.go:66: App has crashed
2020/04/22 14:02:16 logger.go:66: Stopping...
2020/04/22 14:02:16 logger.go:78: astikit: stopping worker...
2020/04/22 14:02:16 logger.go:66: Closing...
2020/04/22 14:02:16 logger.go:73: accept tcp 127.0.0.1:49786: use of closed network connection while TCP accepting
2020/04/22 14:02:16 logger.go:66: Stopping...
2020/04/22 14:02:16 main.go:266: running bootstrap failed: creating window failed: context canceled
chinenual commented 4 years ago

looks like when I close via the titlebar [X], Electron isnt shutting down cleanly. I see lingering Electron and MyAPP executables in the task manager. From the log, here's what quit looks like when clean (from the astilectron.MenuItemRoleQuit menu):

2020/04/22 14:24:15 logger.go:78: astikit: worker is now waiting...
2020/04/22 14:24:19 logger.go:67: Astilectron says: {"name":"app.cmd.quit","targetID":"app"}
2020/04/22 14:24:19 logger.go:66: Stopping...
2020/04/22 14:24:19 logger.go:78: astikit: stopping worker...
2020/04/22 14:24:19 logger.go:67: Astilectron says: {"name":"window.event.closed","targetID":"1"}
2020/04/22 14:24:24 logger.go:73: 'C:\Users\steve\AppData\Roaming\Synergize\vendor\electron-windows-amd64\electron.exe' exited with code: 1
2020/04/22 14:24:24 logger.go:66: App has closed
2020/04/22 14:24:24 logger.go:66: Closing...
2020/04/22 14:24:24 logger.go:66: Stopping...

here's what it looks like from titlebar [X]:

2020/04/22 14:22:16 logger.go:78: astikit: worker is now waiting...
2020/04/22 14:22:19 logger.go:67: Astilectron says: {"name":"window.event.closed","targetID":"1"}
chinenual commented 4 years ago

OK - I've isolated the problem to the fact that my app creates a second hidden window with HideOnClose. Note: this window is hidden when I [X] the main window. But apparently it's preventing the app from shutting down all the way. If I comment out the second window in the bootstrap Windows option, [X] works correctly.

Do I have to write some sort of event handler to intercept the Quit event and "unhide" or manually destroy the hidden window so the app can shut down? Or is this a bug?

asticode commented 4 years ago

This all makes perfect sense then. Basically this event is not fired up when you [X] the main window since your second window is still opened, therefore the app doesn't quit.

What you can do is intercept the closed event for you window, and call a.Quit() :

w.On(EventNameWindowEventClosed, func(e Event) (deleteListener bool) {
    a.Quit()
    return true
})
chinenual commented 4 years ago

Thanks for the confirmation! That, of course, works perfectly.