Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robustify multi-threading primitives #72249

Merged
merged 1 commit into from
May 15, 2023

Conversation

RandomShaper
Copy link
Member

@RandomShaper RandomShaper commented Jan 28, 2023

This PR does the following:

  • Fixes the issue that Thread objects were leaked to the OS if not joined (i.e., in those cases there would be no possibility for the warning about calling wait_to_finish() to be printed; therefore, pretty much hiding the issue to the user).
  • In debug builds, detects wrong cleanup of Semaphore (long explanation in the code comments; TL;DR is Don't exit with semaphores being awaited) and avoids the undefined behavior. I have tried to add similar protection to Mutex, but there are no reasonable ways to detect misuse. In any case, now we can finally get the warning for non-joined threads, most (all?) cases of misuse would cause some kind of admonishment.
  • Documents the details about proper usage of the related classes, to prevent users from running into undefined behavior, including those we can detect and warn about or countermeasure and those that don't.

Fixes #71216.

UPDATE: Back-ported to 3.x in #72251.

@@ -113,7 +113,9 @@ Thread::Thread() {
Thread::~Thread() {
if (id != _thread_id_hash(std::thread::id())) {
#ifdef DEBUG_ENABLED
WARN_PRINT("A Thread object has been destroyed without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread.");
WARN_PRINT(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just reworded this one to be consistent with the warning newly added to Semaphore.

@YuriSizov
Copy link
Contributor

YuriSizov commented Feb 17, 2023

Given 3.x is also affected (and has a fix ready), this should be safe to merge for 4.0.x when reduz has time to review it.

@RandomShaper
Copy link
Member Author

Pushed just fixing a couple of mistakes in the docs.

@RandomShaper
Copy link
Member Author

Rebased on top of 4.1.

@akien-mga akien-mga merged commit 1d83a4c into godotengine:master May 15, 2023
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants