### Godot version

v4.1.1.stable.mono.official [bd6af8e0e]

### System information

Godot v4.1.1.stable.mono - macOS 13.4.1 - Vulkan (Forward+) - integrated Apple M1 - Apple M1 (8 Threads)

### Issue description

After creating a `@tool` script that is autoloaded and uses Semaphores and a Thread, when exiting the editor it freezes and stops responding on MacOS.

Editor not responding after closing the editor:
![image](https://github.com/godotengine/godot/assets/29845000/1a08e748-5ca9-4ed1-9bf0-a1399aab7629)
![image](https://github.com/godotengine/godot/assets/29845000/fa60171c-3123-4e85-b3ca-ab806d4f7bde)

The editor must then be force quit and produces the following error information:
https://gist.github.com/Trikzon/dc3e1ed7a258fa9cc2c363b18b43c353

A full minimal reproducible product is provided, but the important snippets are:
```
@tool
extends Node

var mutex: Mutex
var semaphore: Semaphore
var thread: Thread
var exit_thread := false


func _ready():
    mutex = Mutex.new()
    semaphore = Semaphore.new()
    exit_thread = false
    
    thread = Thread.new()
    thread.start(_thread_function)

func _thread_function():
    while true:
        semaphore.wait()
        
        mutex.lock()
        var should_exit = exit_thread
        mutex.unlock()
        
        if should_exit:
            break
        
       # Actual thread work goes here

func _exit_tree():
    mutex.lock()
    exit_thread = true
    mutex.unlock()
    
    semaphore.post()
    
    thread.wait_to_finish()
```

At first I thought that `_exit_tree()` was not being called when the editor closed, but I found that it is because placing a `ResourceSaver.save()` in `_exit_tree()` creates a file when the editor closes.

### Steps to reproduce

Create a `@tool` script using Semaphores and a Thread and set it as an autoloading script in-editor.

The [Using Multiple Threads#Semaphores](https://docs.godotengine.org/en/stable/tutorials/performance/using_multiple_threads.html#semaphores) guide can be used as a base.

### Minimal reproduction project

[Thread Tools Test.zip](https://github.com/godotengine/godot/files/12237193/Thread.Tools.Test.zip)

Note: I use a mono build of Godot, but this does not contain any C#.

Contains an autoloaded tool script that is a slightly modified version of the [Using Multiple Threads#Semaphores](https://docs.godotengine.org/en/stable/tutorials/performance/using_multiple_threads.html#semaphores) guide.

Also contains a test scene with a tool script where changing a number in-editor will post the semaphore to see that the thread is running correctly in the background.
EditorFileDialog::set_default_display_mode((EditorFileDialog::DisplayMode)EDITOR_GET("filesystem/file_dialog/display_mode").operator int());
			bool theme_changed =
					EditorSettings::get_singleton()->check_changed_settings_in_group("interface/theme") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/font") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/main_font") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/code_font") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/theme") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/help/help") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("filesystem/file_dialog/thumbnail_size") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("run/output/font_size") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("interface/touchscreen/increase_scrollbar_touch_area") ||
					EditorSettings::get_singleton()->check_changed_settings_in_group("interface/touchscreen/scale_gizmo_handles");
			if (theme_changed) {
				theme = create_custom_theme(theme_base->get_theme());
				DisplayServer::set_early_window_clear_color_override(true, theme->get_color(SNAME("background"), SNAME("Editor")));
				theme_base->set_theme(theme);
				gui_base->set_theme(theme);
				get_window()->set_theme(theme);
				gui_base->add_theme_style_override("panel", gui_base->get_theme_stylebox(SNAME("Background"), SNAME("EditorStyles")));
				main_vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, gui_base->get_theme_constant(SNAME("window_border_margin"), SNAME("Editor")));
				main_vbox->add_theme_constant_override("separation", gui_base->get_theme_constant(SNAME("top_bar_separation"), SNAME("Editor")));
				scene_root_parent->add_theme_style_override("panel", gui_base->get_theme_stylebox(SNAME("Content"), SNAME("EditorStyles")));
				bottom_panel->add_theme_style_override("panel", gui_base->get_theme_stylebox(SNAME("BottomPanel"), SNAME("EditorStyles")));
				tabbar_panel->add_theme_style_override("panel", gui_base->get_theme_stylebox(SNAME("tabbar_background"), SNAME("TabContainer")));
				scene_tabs->add_theme_constant_override("icon_max_width", gui_base->get_theme_constant(SNAME("class_icon_size"), SNAME("Editor")));
				scene_tab_add_ph->set_custom_minimum_size(scene_tab_add->get_minimum_size());
				main_menu->add_theme_style_override("hover", gui_base->get_theme_stylebox(SNAME("MenuHover"), SNAME("EditorStyles")));
			}
			scene_tabs->set_max_tab_width(int(EDITOR_GET("interface/scene_tabs/maximum_width")) * EDSCALE);
			_update_scene_tabs();