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

Avoid deadlocks in multi-threaded management of the C# script map #99539

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

RandomShaper
Copy link
Member

@RandomShaper RandomShaper commented Nov 22, 2024

The idea here is that using a read-write locking instead of all-or-nothing reduces the chances of a deadlock.

I'm not sure if this covers every possibility of such, because the interaction between, say, multiple resource loading threads instantiating C# scripts and the .NET module can be complex. All I can say is that this at least stops me from being able to reproduce the issue with the MRP provided.

Note for reviewers: Disable whitespace diff as one of the methods has a change of indentation level that makes the patch look more complex than it actually is.

Fixes #99128.
Fixes #99839 (I haven't been able to reproduce the issue with the MRP, even after modifying it to effectively use multiple loader threads; however, it's extremely likely that the issue the reporter found is fixed by this PR).

@RandomShaper RandomShaper added bug topic:dotnet cherrypick:4.3 Considered for cherry-picking into a future 4.3.x release labels Nov 22, 2024
@RandomShaper RandomShaper added this to the 4.4 milestone Nov 22, 2024
@RandomShaper RandomShaper requested a review from a team as a code owner November 22, 2024 13:07
@RandomShaper RandomShaper force-pushed the fix_dotnet_rl_deadlock branch from 5c94f0b to f8f5505 Compare December 3, 2024 06:57
@akien-mga akien-mga requested a review from raulsntos December 17, 2024 21:21
Copy link
Member

@raulsntos raulsntos left a comment

Choose a reason for hiding this comment

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

Sorry for the delay. As discussed in RC, I believe this PR to be safer than #99798 and according to the author of the other PR it fixes their issue as well.

This PR changes the locking mechanism from lock(obj) to using a ReaderWriterLockSlim instance, reducing the chances of a deadlock. The other PR moves NativeFuncs.godotsharp_internal_reload_registered_script(scriptPtr) outside the lock, and while this also seems to avoid the deadlock it can cause regressions due to underlocking. So this PR seems safer.

I was unable to reproduce the linked issue, but the code looks good to me.

@akien-mga akien-mga merged commit 7d30972 into godotengine:master Dec 18, 2024
20 checks passed
@akien-mga
Copy link
Member

Thanks!

@conde2
Copy link

conde2 commented Jan 6, 2025

This didn't fix the issue in my end, any possibility to reopen the issue? Tested with the same MRP and still getting stuck when loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.3 Considered for cherry-picking into a future 4.3.x release topic:dotnet
Projects
None yet
4 participants