fix: Releasing GIL before attempts to lock the mutex#2581
Merged
hoffstadt merged 2 commits intohoffstadt:masterfrom Dec 18, 2025
Merged
fix: Releasing GIL before attempts to lock the mutex#2581hoffstadt merged 2 commits intohoffstadt:masterfrom
hoffstadt merged 2 commits intohoffstadt:masterfrom
Conversation
Collaborator
Author
|
Please hold on with this one, I think I've found an issue. Not sure yet if it's related to this PR or not, will get back once I sort it out. |
Collaborator
Author
|
Nevermind, I just forgot to remove |
hoffstadt
approved these changes
Dec 18, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
name: Pull Request
about: Create a pull request to help us improve
title: Releasing GIL before attempts to lock the mutex
assignees: ''
Closes #2053
Closes #2077
Closes #2167
Closes #2366
(yes, all of them; they need to be mentioned separately in order to get closed)
Description:
This PR replaces most
lock_guardcalls (the ones that are under GIL) with a custom GIL-safe lock guard: to safely lockGContext->mutexwithout the risk of a deadlock, one needs to make sure that the code locking the mutex does not own GIL. This way in the mutex-GIL pair the mutex will always be locked first, GIL second - the rule of thumb in avoiding deadlocks is to lock mutexes in the same order everywhere. If we lock GIL first and DPG mutex second, we can't guarantee the order because Python manages GIL on its own and can easily lead to locking them in the opposite order.The PR also fixes some race conditions and overall improves synchronization and stability.
If one needs to get back to old locking mechanism for some reason, DPG provides a build option named
MV_NO_USER_THREADS- it was useful during development but I doubt that anyone will really need it (and it's only more or less safe when none of user threads, i.e.threading.Thread, call DPG functions).Concerning Areas:
None.