Skip to content

[updater] doesn't shutdown app gracefully on Windows #1014

Closed

Description

@Legend-Master

This skips app clean up code, for example kill sidecar / save windows states / save store

std::process::exit(0);

Activity

  1. changed the title [-]Updater doesn't shutdown app gracefully on Windows[/-] [+][updater] doesn't shutdown app gracefully on Windows[/+] on Mar 4, 2024
  2. Legend-Master commented on Mar 7, 2024

    @Legend-Master
    ContributorAuthor

    We probably need to have an API like "on post exit" for it to let the app clean up first and then run the installer/updater

    A quick dirty fix would be run app.exit and run updater on exit event, and make sure we're the last registered plugin...

    By the way, the updater still shows a powershell window, maybe we should migrate to use ShellExecute instead

  3. amrbashir commented on Mar 12, 2024

    @amrbashir
    Member
  4. Legend-Master commented on Mar 12, 2024

    @Legend-Master
    ContributorAuthor

    Totally forgot it, that should do the job

  5. Legend-Master commented on Mar 13, 2024

    @Legend-Master
    ContributorAuthor

    Wait, cleanup_before_exit only cleans up tray icon and resources, it doesn't run the functions listening on Exit event, so things like other plugin's on exit cleanup will not get ran

    I'll take a look tomorrow, maybe we just need to also send an Exit event

  6. amrbashir commented on Mar 13, 2024

    @amrbashir
    Member

    We can't send an exit event, this can only be sent by tauri.

  7. amrbashir commented on Mar 13, 2024

    @amrbashir
    Member

    you should use the new before_exit callback to run your own cleanup

  8. Legend-Master commented on Mar 13, 2024

    @Legend-Master
    ContributorAuthor

    So how one could run other plugin's clean up functions?

  9. amrbashir commented on Mar 13, 2024

    @amrbashir
    Member

    I guess they will need to also expose a cleanup method that users can call

    or maybe change the spec of a tauri plugin to have a cleanup hook so they run cleanup there instead of on Exit event and so tauri cleanup_befor_exit can run that hook

  10. Legend-Master commented on Mar 13, 2024

    @Legend-Master
    ContributorAuthor

    I feel like the clean up hook shouldn't be on the Exit event listener, when you run cleanup_before_exit, you would expect it runs all plugin clean up functions

  11. Legend-Master commented on Mar 13, 2024

    @Legend-Master
    ContributorAuthor

    I like the clean up hook idea, if we need to make it, it's better to do it before going stable

  12. FabianLars commented on Mar 13, 2024

    @FabianLars
    Member

    devs are expected to call app.restart() on macos and linux, right? If so, then this will trigger Exit and ExitRequest so why don't we just call app.exit() on windows which does the same? Maybe we can set some tauri::State so that users can check in ExitRequested why it restarts instead of another clean up hook.

  13. amrbashir commented on Mar 13, 2024

    @amrbashir
    Member

    that will make the updater dependent on storing an AppHandle which is not something I am quite fan of, maybe we could instead remove before_exit and replace it with exit_function and fallback to std::process::exit if was not provided.

  14. Legend-Master commented on Mar 14, 2024

    @Legend-Master
    ContributorAuthor

    Note that the clean up hook need to be run before we run the installer or the installer will kill the app

    Maybe we could try to make installer send a signal to the running app and request it to shutdown instead?

  15. Legend-Master commented on Mar 14, 2024

    @Legend-Master
    ContributorAuthor

    I'm not sure what triggers an Exit event, I think log out / shutdown Windows doesn't even do it (tested on an app with tray icon and hidden window)

  16. Legend-Master commented on Mar 14, 2024

    @Legend-Master
    ContributorAuthor

    Or maybe we do app.exit and wait for it to shutdown itself in the installer?

  17. amrbashir commented on Mar 14, 2024

    @amrbashir
    Member

    this is tricky indeed, we can't wait for the installer to send a signal, we should already exit before the installer runs.

    I think I am leaning towards keeping the new before_exit hook and recommending users that they should ensure all intermediate work for their app has been saved or cleaned up before calling install or download_and_install, this could include running cleanup methods of plugins (if they provide one).

    In addition to that we should look into adding a on_destroy hook for plugins to run their cleanup there and modify app.cleanup_before_exit to run these destroy hooks.

  18. Legend-Master commented on Mar 14, 2024

    @Legend-Master
    ContributorAuthor

    Migrate plugin clean up code (and maybe even app clean up) to a standalone hook would be great

    An alternative would be making cleanup_before_exit to send an Exit event

    Just took a look on how electron handles it, they seem to be sending a before-quit-for-update event and followed by closing all windows and that triggers a before-quit event

    Another popular updater package electron updater will detect running app, and wait for it to exit gracefully for 1 second, and if the app is still running, it just kills it, and the spawning code is similar to

    spawn_installer()
    app.exit(0)

    https://github.com/electron-userland/electron-builder/blob/c2392de71a8f7abc092a00452eac63dd24b34e88/packages/app-builder-lib/templates/nsis/include/allowOnlyOneInstallerInstance.nsh#L50-L118

    So another alternative would be we call app.exit(0), and wait for it to shutdown in the installer, similar to #1014 (comment)

  19. amrbashir commented on Mar 14, 2024

    @amrbashir
    Member

    An alternative would be making cleanup_before_exit to send an Exit event

    That's not its purpose though

    Just took a look on how electron handles it, they seem to be sending a before-quit-for-update event and followed by closing all windows and that triggers a before-quit event

    This is similar to the new cleanup_before_exist hook, just as an event

  20. Legend-Master commented on Mar 14, 2024

    @Legend-Master
    ContributorAuthor

    Migrate to clean up hook

    • More work, and breaking changes
    • Can't catch run installer errors

    Run app.exit(0) and make the installer wait

    • Maybe lesser clean?
    • I'm not sure how it would work on the msi installer

    So what do you think?

  21. Legend-Master commented on Mar 14, 2024

    @Legend-Master
    ContributorAuthor

    To be honest, I don't know how Chrome and Firefox can apply new updates without closing the app first (they're the most seamless updates I know about), most other programs on Windows can't, even programs like PowerToys, vscode from Microsoft

  22. Legend-Master commented on Nov 16, 2025

    @Legend-Master
    ContributorAuthor

    Seeing tauri-apps/tauri#14443 really made me rethink about this again now

    The focus of that PR itself seems to be on killing the side car tree which I'm not sure a hook on App::cleanup_before_exit would really help, we currently only skip the Exit event in updater plugin's install on Windows which I think it's more of a problem there

    #1014 (comment)

    I feel like this might be what we would want, if the worry here was mainly about storing AppHandle in the Updater, we could do

    maybe we could instead remove before_exit and replace it with exit_function and fallback to std::process::exit if was not provided.

    A quick summary of the options here:

    • Use app.exit here, 2 sub options (no matter the options, this function will return instead of the current behavior which I think it's fine since it matches other platforms here, one thing to note is that if the user's using request_restart here, it might conflict with us as that makes the next exit request spawns a restart instance...):
      • Spawns the updater before we run app.exit and let the updater wait for the app to exit
      • Or store the update data in a state, and run it on Exit event (this is hard, since we must ensure we're the last one to run)
    • Introduce a new method/hook cleanup_before_exit
      • Good:
        • We don't have to worry about the Exit event handling
      • Bad:
        • We need to ensure the hook to not run plugins that didn't reach initialize yet
        • Migrate all the plugins and let users know that they should move the clean up code to the new hook
      • The new hook will run in the thread that calls it, this is not always on main thread nor not on main thread, not sure if it's going to cause problems
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions