← Winnitude! Search  ·  Reader Mode (1293 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 78 LINKS

Fixed crash in finalizer of CLR types defined in Python, that survive engine shutdown by lostmsu · Pull Request #1260 · pythonnet/pythonnet

Repository navigation

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


Merged
filmor merged 1 commit into pythonnet:master from losttech:bugs/1256 Dec 18, 2020
Merged

Fixed crash in finalizer of CLR types defined in Python, that survive engine shutdown #1260 filmor merged 1 commit into pythonnet:master from losttech:bugs/1256

Conversation

lostmsu commented Oct 13, 2020



Copy link Copy Markdown
Member

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 force-pushed the bugs/1256 branch 4 times, most recently from 9498c07 to 927f980
Compare
October 13, 2020 17:26

lostmsu commented Oct 13, 2020 · edited Loading



Copy link Copy Markdown
Member

Author

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 commented Oct 13, 2020 · edited Loading



Copy link Copy Markdown

Codecov Report

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

@@           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 commented Nov 11, 2020



Copy link Copy Markdown
Member

Author

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

amos402 commented Nov 13, 2020



Copy link Copy Markdown
Member

pythonnet/src/runtime/classderived.cs

Lines 77 to 80 in 927f980

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.
lostmsu mentioned this pull request Nov 20, 2020 Exception in finalizer of custom encoder #1256 Open

tminka commented Dec 17, 2020



Copy link Copy Markdown
Contributor

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

tminka mentioned this pull request Dec 17, 2020 Incorrectly using a non-generic type with type parameters now produces a helpful Python error instead of throwing NullReferenceException #1326 Merged 3 of 4 tasks fixed crash in finalizer of CLR types defined in Python, that survive... 1fd6dc2
... 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.
lostmsu force-pushed the bugs/1256 branch from 927f980 to 1fd6dc2
Compare
December 17, 2020 19:49 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 fixed crash in finalizer of CLR types defined in Python, that survive... 94f29a7
... 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 fixed crash in finalizer of CLR types defined in Python, that survive... 3069285
... 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

5 participants