Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixed crash in finalizer of CLR types defined in Python, that survive engine shutdown #1260

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

Merged
merged 1 commit into from
Dec 18, 2020

Conversation

lostmsu
Copy link
Member

@lostmsu lostmsu commented Oct 13, 2020

What does this implement/fix? Explain your changes.

During engine shutdown all links from Python to .NET instances are severed. If an instance of CLR class defined in Python survives the shutdown (for example, a reference is stored in static field) and later gets finalized, it will attempt to severe link again, which is an invalid operation.

The fix is to check if the link has already been severed and skip that step during finalization.

Additionally in this change: refactored MoveClrInstancesOnwershipToPython

Does this close any currently open issues?

#1256

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@lostmsu lostmsu force-pushed the bugs/1256 branch 4 times, most recently from 9498c07 to 927f980 Compare October 13, 2020 17:26
@lostmsu
Copy link
Member Author

lostmsu commented Oct 13, 2020

Interesting. It appears, that the fix is legitimate, but the test for it causes segfault in soft shutdown mode with xplat. Which probably means it is caused by a separate issue :/

@codecov-io
Copy link

codecov-io commented Oct 13, 2020

Codecov Report

Merging #1260 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1260   +/-   ##
=======================================
  Coverage   86.25%   86.25%           
=======================================
  Files           1        1           
  Lines         291      291           
=======================================
  Hits          251      251           
  Misses         40       40           
Flag Coverage Δ
#setup_linux 64.94% <ø> (ø)
#setup_windows 72.50% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f506d65...927f980. Read the comment docs.

@lostmsu
Copy link
Member Author

lostmsu commented Nov 11, 2020

@amos402 I could see it being freed by one of tp_clear or tp_dealloc and then again by finalizer.

@amos402
Copy link
Member

amos402 commented Nov 13, 2020

GCHandle gc = GCHandle.Alloc(self, GCHandleType.Weak);
Marshal.WriteIntPtr(self.pyHandle, ObjectOffset.magic(self.tpHandle), (IntPtr)gc);
self.gcHandle.Free();
self.gcHandle = gc;

It set by a weak GCHandle instead after it freed.

@tminka
Copy link
Contributor

tminka commented Dec 17, 2020

I have successfully avoided the problem mentioned by @amos402 above via the following commit.

… engine shutdown

pythonnet#1256
pythonnet#1256

During engine shutdown all links from Python to .NET instances are severed. If an instance of CLR class defined in Python survives the shutdown (for example, a reference is stored in static field) and later gets finalized, it will attempt to severe link again, which is an invalid operation.

The fix is to check if the link has already been severed and skip that step during finalization.
@filmor filmor merged commit e2ab3ae into pythonnet:master Dec 18, 2020
BadSingleton pushed a commit to Unity-Technologies/pythonnet that referenced this pull request Dec 18, 2020
… engine shutdown (pythonnet#1260)

pythonnet#1256
pythonnet#1256

During engine shutdown all links from Python to .NET instances are severed. If an instance of CLR class defined in Python survives the shutdown (for example, a reference is stored in static field) and later gets finalized, it will attempt to severe link again, which is an invalid operation.

The fix is to check if the link has already been severed and skip that step during finalization.
BadSingleton pushed a commit to Unity-Technologies/pythonnet that referenced this pull request Dec 18, 2020
… engine shutdown (pythonnet#1260)

pythonnet#1256
pythonnet#1256

During engine shutdown all links from Python to .NET instances are severed. If an instance of CLR class defined in Python survives the shutdown (for example, a reference is stored in static field) and later gets finalized, it will attempt to severe link again, which is an invalid operation.

The fix is to check if the link has already been severed and skip that step during finalization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants