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

Fix C# ScriptManagerBridge duplicate key Type #83204

Closed
wants to merge 2 commits into from

Conversation

ricaun
Copy link

@ricaun ricaun commented Oct 12, 2023

Issue

This PR is related to fix #79519.

Summary

When the same key is added in ScriptManagerBridge, throws an Exception and breaks Unload.

Here is a minimal project example with the issue: https://github.com/taylorhadden/godot-csharp-generic-assembly-reload-error

This PR only adds the key if not exist, fixing the problem but sometimes the Missing class qualified name for reloading script happens.

GD.PushError("Missing class qualified name for reloading script");

I didn't figure out how to prevent that message Error.

This PR is related to fix godotengine#79519.

## Summary

When the same key is added in `ScriptManagerBridge`, throws an Exception and breaks Unload.

Here is a minimal project example with the issue: https://github.com/taylorhadden/godot-csharp-generic-assembly-reload-error

This PR only adds the key if not exist, fixing the problem but sometimes the `Missing class qualified name for reloading script` happens.

https://github.com/godotengine/godot/blob/b1371806ad3907c009458ea939bd4b810f9deb21/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs#L558

I didn't figure out how to prevent that message Error.
@ricaun ricaun requested a review from a team as a code owner October 12, 2023 18:02
@AThousandShips AThousandShips changed the title [Fix] C# ScriptManagerBridge duplicate key Type Fix C# ScriptManagerBridge duplicate key Type Oct 12, 2023
@AThousandShips AThousandShips added this to the 4.2 milestone Oct 12, 2023
Comment on lines +24 to +26
// Due to generic classes, more than one class can point to the same type, so
// there could be duplicate keys in this case. We only add a type as key once.
_typeScriptMap.TryAdd(scriptType, scriptPtr);
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't sound right, generic classes can't be scripts so they shouldn't be added to the map. If a generic type reaches this point, then we have a bug somewhere else where we should be filtering/ignoring generic classes.

Copy link
Author

@ricaun ricaun Oct 26, 2023

Choose a reason for hiding this comment

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

You are right, filtering the types would be the best approach. I didn't find a way to do that. 😐

I you have any idea when/where the types are created, that would help.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to update the other entry as well (removing the old script pointer). Based on my test the change I propose can reduce the issue into an Editor Error Message instead of Crashing the Mono module (Fail to unload assembly on second build). However, I agree that finding the root cause is necessary.

public void Add(IntPtr scriptPtr, Type scriptType)
{
    // TODO: What if this is called while unloading a load context, but after we already did cleanup in preparation for unloading?

    if (_typeScriptMap.TryGetValue(scriptType, out var oldPtr))
    {
        _scriptTypeMap.Remove(oldPtr);
        _typeScriptMap[scriptType] = scriptPtr;
    }
    else
    {
        _typeScriptMap.Add(scriptType, scriptPtr);
    }
    _scriptTypeMap.Add(scriptPtr, scriptType);

@raulsntos
Copy link
Member

Superseded by #87550

@raulsntos raulsntos closed this Jan 26, 2024
@raulsntos raulsntos removed this from the 4.x milestone Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C# Script Registration that use Generics can error out in the ScriptManagerBridge
5 participants