### 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.
if (!global_menu || !OS::get_singleton()->has_feature("macos")) {
		
		file_menu->add_separator();
		file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/file_quit", TTR("Quit"), KeyModifierMask::CMD_OR_CTRL + Key::Q), FILE_QUIT, true);
	}
	project_menu = memnew(PopupMenu);
	project_menu->set_name(TTR("Project"));
	main_menu->add_child(project_menu);
	project_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/project_settings", TTR("Project Settings..."), Key::NONE, TTR("Project Settings")), RUN_SETTINGS);
	project_menu->connect("id_pressed", callable_mp(this, &EditorNode::_menu_option));
	project_menu->add_separator();
	project_menu->add_item(TTR("Version Control"), VCS_MENU);
	project_menu->add_separator();
	project_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/export", TTR("Export..."), Key::NONE, TTR("Export")), FILE_EXPORT_PROJECT);
#ifndef ANDROID_ENABLED
	project_menu->add_item(TTR("Install Android Build Template..."), FILE_INSTALL_ANDROID_SOURCE);
	project_menu->add_item(TTR("Open User Data Folder"), RUN_USER_DATA_FOLDER);
#endif
	project_menu->add_separator();
	project_menu->add_item(TTR("Customize Engine Build Configuration..."), TOOLS_BUILD_PROFILE_MANAGER);
	project_menu->add_separator();
	plugin_config_dialog = memnew(PluginConfigDialog);
	plugin_config_dialog->connect("plugin_ready", callable_mp(this, &EditorNode::_on_plugin_ready));
	gui_base->add_child(plugin_config_dialog);
	tool_menu = memnew(PopupMenu);
	tool_menu->set_name("Tools");
	tool_menu->connect("index_pressed", callable_mp(this, &EditorNode::_tool_menu_option));
	project_menu->add_child(tool_menu);
	project_menu->add_submenu_item(TTR("Tools"), "Tools");
	tool_menu->add_item(TTR("Orphan Resource Explorer..."), TOOLS_ORPHAN_RESOURCES);