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

C#: Check if .NET runtime has been finalized in the instance binding reference callback #77362

Conversation

RedworkDE
Copy link
Member

Fixes #77305

Turns out you can't detach an instance binding from an object, so duplicate a check from _instance_binding_free_callback.

@RedworkDE RedworkDE requested a review from a team as a code owner May 22, 2023 18:02
@akien-mga akien-mga added this to the 4.1 milestone May 22, 2023
@neikeq neikeq self-requested a review May 23, 2023 07:26
@akien-mga
Copy link
Member

CC @raulsntos

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.

I'm not very familiar with the reference count system so take this review with a grain of salt. In general it makes sense to me that if the .NET runtime is finalized, the reference_callback implementation would return true and let the object be freed.

Copy link
Contributor

@neikeq neikeq left a comment

Choose a reason for hiding this comment

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

I'm preemptively blocking this from being merged before I review it tomorrow. But in advance I can say there's probably something wrong elsewhere as instance bindings should have been cleaned up before/after finalizing.

@RedworkDE
Copy link
Member Author

RedworkDE commented Jun 23, 2023

But in advance I can say there's probably something wrong elsewhere as instance bindings should have been cleaned up before/after finalizing.

Yes, arguably the instance binding should have been detached from objects before

script_bindings.clear();
but there is no way of doing that currently and thus I went for the simple crash fix that doesn't require core changes.

Edit: alternatively #78157 also fixes the crash by moving the OnGodotShuttingDown to before the bindings cleanup, tho if there are cases where bound objects survive that this (or a similar) patch may still be needed.

@akien-mga akien-mga modified the milestones: 4.1, 4.2 Jun 23, 2023
@akien-mga akien-mga added cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release and removed cherrypick:4.0 labels Jun 23, 2023
@neikeq
Copy link
Contributor

neikeq commented Jun 28, 2023

The problem is indeed somewhere else. We cannot remove the script bindings, but that's what the script_binding.inited. This method checks that, so for some reason it's not cleaned up. Probably a script binding is being created again after all the bindings were finalized, which would explain why #78157 fixes the issue.

@neikeq
Copy link
Contributor

neikeq commented Jun 28, 2023

Wait, I just realized we check script_binding.inited after CRASH_COND(!rc_owner);. Does moving it to check that first work?

@RedworkDE
Copy link
Member Author

Does moving it to check that first work?

When these issues occur p_binding is a dangling pointer to freed memory and we can no longer safely check script_binding.inited, so no that doesn't help.

I would use #78157 to fix the issue (It gracefully deletes the managed objects before dropping the instance bindings map) and then either keep this PR as-is as backup or just close this as superseded.

@raulsntos
Copy link
Member

If #78157 fixes the issue, then I think I'm leaning towards closing this PR as superseded.

If, as you say, some bound objects survive after OnGodotShuttingDown, then it may be preferable to let Godot crash instead of potentially hiding an issue somewhere else.

As I understand it from the previous conversation, the bound objects should have been cleared before reaching _instance_binding_free_callback so if any bound objects survive it's likely a bug in the .NET module that should be fixed.

@akien-mga

This comment was marked as duplicate.

@RedworkDE
Copy link
Member Author

Superseded by #78157

@RedworkDE RedworkDE closed this Nov 1, 2023
@RedworkDE RedworkDE deleted the net-instance-binding-reference-finalized branch November 1, 2023 07:49
@akien-mga akien-mga added archived and removed cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release labels Nov 1, 2023
@AThousandShips AThousandShips removed this from the 4.2 milestone Nov 1, 2023
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.

ERROR: FATAL: Condition "!rc_owner" is true. After adding C# Script
5 participants