Repository: godot
Issue: on OS_Server: attaching to VisualServer frame_post_draw signal causes crash on exit
Labels: bug,platform:linuxbsd,topic:rendering,crash

### Godot version

Godot_v3.3.4-stable_linux_headless.64

### System information

Linux OS_Server

### Issue description

When connecting to the `frame_post_draw` signal running on the server build (aka linux_headless) the signal is never fired, and the engine crashes on exit. It seems like the `GDScriptScriptingLanguage` singleton is cleaned up before the `VisualServer` singleton so any signals still remaining will crash when attempting to clear them.



### Steps to reproduce

Run the attached gdscript with `-s`

```
$ ~/src/godot/bin/godot_server.x11.tools.64 -s frame_post_draw_repro.gd 
Godot Engine v3.3.2.stable.custom_build.7610409b8 - https://godotengine.org
 
VisualServer: VisualServer False
attached
idle_frame
idle_frame (quit)
handle_crash: Program crashed with signal 11
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x41040) [0x7fbda5687040] (??:0)
[2] /lib/x86_64-linux-gnu/libpthread.so.0(pthread_mutex_lock+0x4) [0x7fbda5992654] (??:0)
[3] /home/jpate/src/godot/bin/godot_server.x11.tools.64(+0x134ad4d) [0x55eeadb98d4d] (??:0)
[4] /home/jpate/src/godot/bin/godot_server.x11.tools.64(+0x134ad9d) [0x55eeadb98d9d] (??:0)
[5] std::recursive_mutex::lock() (??:0)
[6] GDScriptFunctionState::~GDScriptFunctionState() (??:0)
[7] void memdelete<Reference>(Reference*) (??:0)
[8] Ref<Reference>::unref() (??:0)
[9] RefPtr::unref() (??:0)
[10] Variant::clear() (??:0)
[11] Variant::~Variant() (??:0)
[12] CowData<Variant>::_unref(void*) (??:0)
[13] CowData<Variant>::~CowData() (??:0)
[14] Vector<Variant>::~Vector() (??:0)
[15] Object::Connection::~Connection() (??:0)
[16] Object::Signal::Slot::~Slot() (??:0)
[17] VMap<Object::Signal::Target, Object::Signal::Slot>::Pair::~Pair() (??:0)
[18] CowData<VMap<Object::Signal::Target, Object::Signal::Slot>::Pair>::_unref(void*) (??:0)
[19] CowData<VMap<Object::Signal::Target, Object::Signal::Slot>::Pair>::~CowData() (??:0)
[20] VMap<Object::Signal::Target, Object::Signal::Slot>::~VMap() (??:0)
[21] Object::Signal::~Signal() (??:0)
[22] HashMap<StringName, Object::Signal, HashMapHasherDefault, HashMapComparatorDefault<StringName>, (unsigned char)3, (unsigned char)8>::Pair::~Pair() (??:0)
[23] HashMap<StringName, Object::Signal, HashMapHasherDefault, HashMapComparatorDefault<StringName>, (unsigned char)3, (unsigned char)8>::Element::~Element() (??:0)
[24] void memdelete<HashMap<StringName, Object::Signal, HashMapHasherDefault, HashMapComparatorDefault<StringName>, (unsigned char)3, (unsigned char)8>::Element>(HashMap<StringName, Object::Signal, HashMapHasherDefault, HashMapComparatorDefault<StringName>, (unsigned char)3, (unsigned char)8>::Element*) (??:0)
[25] HashMap<StringName, Object::Signal, HashMapHasherDefault, HashMapComparatorDefault<StringName>, (unsigned char)3, (unsigned char)8>::erase(StringName const&) (??:0)
[26] Object::~Object() (??:0)
[27] VisualServer::~VisualServer() (??:0)
[28] VisualServerRaster::~VisualServerRaster() (??:0)
[29] void memdelete<VisualServer>(VisualServer*) (??:0)
[30] OS_Server::finalize() (??:0)
[31] Main::cleanup(bool) (??:0)
[32] /home/jpate/src/godot/bin/godot_server.x11.tools.64(main+0x9e) [0x55eeadb972f7] (??:0)
[33] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xd5) [0x7fbda566e565] (??:0)
[34] /home/jpate/src/godot/bin/godot_server.x11.tools.64(_start+0x2e) [0x55eeadb9719e] (??:0)
-- END OF BACKTRACE --
Aborted (core dumped)
```

```
$ ~/bin/Godot_v3.3.4-stable_x11.64 -s frame_post_draw_repro.gd 
Godot Engine v3.3.4.stable.official.faf3f883d - https://godotengine.org
OpenGL ES 3.0 Renderer: NVIDIA GeForce GTX 1070 with Max-Q Design/PCIe/SSE2
OpenGL ES Batching: ON
 
VisualServer: VisualServer True
attached
idle_frame
frame drawn
idle_frame (quit)
```

### Minimal reproduction project

[frame_post_draw_repro.gd.txt](https://github.com/godotengine/godot/files/7671764/frame_post_draw_repro.gd.txt)

```python
extends SceneTree

func _init():
    print('VisualServer: %s %s' %  [VisualServer.get_class(), OS.can_draw()])
    _attach()
    print('attached')
    yield(self, 'idle_frame')
    print('idle_frame')
    yield(self, 'idle_frame')
    print('idle_frame (quit)')
    quit(1)

func _attach():
    # To workaround this you can use
    # if OS.can_draw():
    yield(VisualServer, "frame_post_draw")
    print('frame drawn')

```