Repository: godot
Issue: Bizarre plugin bug with autoload singletons, or just lack of documentation
Labels: bug,topic:editor,topic:plugin,needs testing

### Godot version

3.4.stable

### System information

Windows 10/11 at least

### Issue description

I'm opening this issue here because I'm not even sure where to begin to debug this, and I was recommended to do so by mhilbrunner (thanks for the suggestion! not at-ing because don't want to ping for everything that follows). Essentially, a pull request was opened against my Firebase plugin to fix an issue with Godot itself. The pull request can be found here: https://github.com/GodotNuts/GodotFirebase/pull/273

One odd part of it is I cannot tell if this would produce a memory leak or not when you close the editor, but the oddest part is the user's claim that Godot won't call to remove the singleton sometimes, _even if_ the call to `_exit_tree`, where the call to remove the singleton is made, is made. I don't understand how that's at all possible, given how simple the code is, but the suggested fix in the PR is to add the adding/removal of the singleton to the `enable_plugin` and `disable_plugin` overrides, respectively. The problem with that? These methods aren't even mentioned in the docs anywhere outside of just listing them as functions, with no information about them at all, as far as I can tell, in the sections on how to make plugins. It feels really awkward using it if I can't be guaranteed it's correct because the docs missed it, so I'd rather ask for help here if I can, especially if `disable_plugin` isn't called when the editor is closed, and hence a memory leak is happening.

### Steps to reproduce

It seems that all projects that use plugins which add autoload singletons to them will have this issue, and having multiples will sometimes cause things to not function correctly. However, I cannot determine what the steps are, in part because this doesn't seem to happen consistently at all. There are a few links in the PR I linked above, which is very small, so feel free to take a look and let me know. This isn't really intended to be a bug that the team necessarily needs to fix - I just can't figure out what on earth is going on in the first place, and not sure why an autoload singleton would ever be only sometimes removed, and sometimes not, when the code is in `_exit_tree`, but not when it's in `disable_plugin` (it is removed at all times when placed in there, evidently, but not when the editor is closed, to my knowledge, so it doesn't reorder the singletons like `_exit_tree` would).

### Minimal reproduction project

I don't have one at the moment, but can probably have someone post one if absolutely needed. As best I can tell, all you need to do is add the WAT plugin and the Firebase plugin, place the Firebase singleton first in the autoload section, then close and open the editor one or more times and it will eventually place Firebase second again. (Note: the problem with that is often-times, people will put their own singletons in there after the Firebase plugin so the singletons can use said Firebase plugin - if it is moved to last, those other singletons will no longer recognize the Firebase plugin at compile time due to the nature of the ordering of the autoloads.)