Sublime Forum

Plugin_unloaded() is not called

#1

The API states that if a plugin has a module level function called plugin_unloaded() then it is run just before the plugin is unloaded.

However plugin_unloaded() is only called if the plugin is unload manually, e.g. by deleting the plugin’s file while ST is still running, it DOES NOT get called when the exit command is run and ST is closed.

The sister function plugin_loaded() works perfectly when ST is started and I was expecting the same behaviour for plugin_unloaded() - a callback which is run just before ST is closed. Isn’t that it’s intended purpose? Otherwise it is only called when removing or updating a plugin which, while being desirable, is not half as useful.

The function on_api_ready(), in sublime_plugin.py, controls calling each module’s plugin_loaded() function - which works fine. But there is no equivalent to on_api_ready(), defined in sublime_plugin.py, for the exit application process.

I see this issue was raised in a SublimeTextIssues/Core issue back in 2013; “on_application_close” addition.

Probably not but… Is there any way of running some code when ST exits so that a plugin’s state can be restored on startup?

0 Likes

#2

Sadly I don’t think so. I’d love to have a hook like this myself but all my experiments to determine when the application is exiting lead to a dead end.

0 Likes

#3

I don’t think so. It sounds potentially problematic to allow a plugin to delay program exit.

What kind of state were you looking to restore?

1 Like

#4

Plugins aren’t unloaded when st exists. The plugin host is simply terminated.

You either need to save state periodically or have an external process that monitors its parent process and does stuff on-exit, depending on what you need. You could try python’s atexit module, because I don’t think I ever tried that, but no idea if that works.

2 Likes