rnd-team-dev / plotoptix

Data visualisation and ray tracing in Python based on OptiX 7.7 framework.
https://rnd.team/plotoptix
Other
498 stars 25 forks source link

Failing to initialize Pythonnet #50

Closed tonyf closed 1 year ago

tonyf commented 1 year ago

In version 0.14.4, while everything seems to run fine, I'm getting the following RuntimeError

Failed to initialize pythonnet: System.NullReferenceException: Object reference not set to an instance of an object
  at Python.Runtime.ImportHook.UpdateCLRModuleDict () [0x00014] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.ImportHook.GetCLRModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.InitExt () [0x00060] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Initialize (System.IntPtr data, System.Int32 size) [0x0002f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.ImportHook.UpdateCLRModuleDict () [0x00014] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.ImportHook.GetCLRModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.InitExt () [0x00060] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Initialize (System.IntPtr data, System.Int32 size) [0x0002f] in <02a0d26b83f24e67b6a0456fd958e703>:0 Failed to shutdown pythonnet: System.InvalidOperationException: ValueFactory attempted to access the Value property of this instance.
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyModule.Import (System.String name) [0x00015] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime+<>c__DisplayClass42_0.<GetModuleLazy>b__0 () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyObject.ToString () [0x00012] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.InternString.Shutdown () [0x0002b] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime.Shutdown () [0x000c3] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.Shutdown () [0x00064] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Shutdown (System.IntPtr data, System.Int32 size) [0x00026] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyModule.Import (System.String name) [0x00015] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime+<>c__DisplayClass42_0.<GetModuleLazy>b__0 () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyObject.ToString () [0x00012] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.InternString.Shutdown () [0x0002b] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime.Shutdown () [0x000c3] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.Shutdown () [0x00064] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Shutdown (System.IntPtr data, System.Int32 size) [0x00026] in <02a0d26b83f24e67b6a0456fd958e703>:0 Exception ignored in atexit callback: <function unload at 0x7f5a74ee2170>
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pythonnet/__init__.py", line 158, in unload
    raise RuntimeError("Failed to call Python.NET shutdown")

Using the following versions

pycparser==2.21
pythonnet==3.0.1

I'm guessing there might just be a version mismatch here? Are there known versions that work with 0.14.14?

robertsulej commented 1 year ago

I'll try to setup this release and reproduce the error. Was it repeating each time or random? Would be great if you can share a piece of code that was causing problems.

tonyf commented 1 year ago

This was repeated but was caused by the service managing our containers. I moved rt.close() before the SIGTERM is sent sent to the process and that error disappeared. However, the following exception is now being thrown by mono

=================================================================
        Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
        Native stacktrace:
=================================================================
        0x7fea0595b609 - /usr/lib/libmonosgen-2.0.so.1 : 
        0x7fea0595b989 - /usr/lib/libmonosgen-2.0.so.1 : 
        0x7fea058e5f2f - /usr/lib/libmonosgen-2.0.so.1 : 
        0x7fea0585f549 - /usr/lib/libmonosgen-2.0.so.1 : 
        0x7fea17811d60 - /lib/x86_64-linux-gnu/libc.so.6 : 
        0x7fea1793933a - /lib/x86_64-linux-gnu/libc.so.6 : 
        0x7fea0fbddadb - /usr/local/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so : 
        0x7fea0fc8021d - /usr/local/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so : 
        0x7fea17b282e4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b251b0 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyObject_MakeTpCall
        0x7fea0fc8115c - /usr/local/lib/python3.10/site-packages/numpy/core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so : 
        0x7fea17b282e4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b251b0 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyObject_MakeTpCall
        0x7fea1695335e - /usr/local/lib/python3.10/lib-dynload/_pickle.cpython-310-x86_64-linux-gnu.so : 
        0x7fea1695571f - /usr/local/lib/python3.10/lib-dynload/_pickle.cpython-310-x86_64-linux-gnu.so : 
        0x7fea16954bb4 - /usr/local/lib/python3.10/lib-dynload/_pickle.cpython-310-x86_64-linux-gnu.so : 
        0x7fea1695689a - /usr/local/lib/python3.10/lib-dynload/_pickle.cpython-310-x86_64-linux-gnu.so : 
        0x7fea169557fb - /usr/local/lib/python3.10/lib-dynload/_pickle.cpython-310-x86_64-linux-gnu.so : 
        0x7fea169558f3 - /usr/local/lib/python3.10/lib-dynload/_pickle.cpython-310-x86_64-linux-gnu.so : 
        0x7fea169559b2 - /usr/local/lib/python3.10/lib-dynload/_pickle.cpython-310-x86_64-linux-gnu.so : 
        0x7fea17b2e076 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b21ca1 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ed02 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b3242e - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17a20682 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b3242e - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17a20682 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b3242e - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea16333476 - /usr/local/lib/python3.10/lib-dynload/_asyncio.cpython-310-x86_64-linux-gnu.so : 
        0x7fea17b251b0 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyObject_MakeTpCall
        0x7fea17aae80c - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b24473 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b22d0a - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1dd51 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b21ca1 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1dd51 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b20501 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b1ebe6 - /usr/local/bin/../lib/libpython3.10.so.1.0 : _PyEval_EvalFrameDefault
        0x7fea17b1d9a4 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b26e2f - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17b2ea1a - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17bb3556 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea17bb3504 - /usr/local/bin/../lib/libpython3.10.so.1.0 : 
        0x7fea177beea7 - /lib/x86_64-linux-gnu/libpthread.so.0 : 
        0x7fea178d4a2f - /lib/x86_64-linux-gnu/libc.so.6 : clone

=================================================================
        Telemetry Dumper:
=================================================================
Thread 0x7fea149c3700 may have been prematurely finalized* Assertion at mono-threads.c:651, condition `info' not met, function:mono_thread_info_current,

Is there a way to wait until all the resources have been cleaned up?

The following code block executes fine. The exception is getting thrown afterwards.

rt.close()
while not rt.is_closed():
        await asyncio.sleep(0.1)
print("Closed plotoptix")
tonyf commented 1 year ago

Sorry, upon further investigation it seems like that issue was caused by the fact I was accessing the numpy arrays after calling rt.close(). Calling .copy() on them fixed the mono issue.

We're back to the same pythonnet issue. This is on python3.10 and plotoptix 0.16.0

Failed to initialize pythonnet: System.NullReferenceException: Object reference not set to an instance of an object
  at Python.Runtime.ImportHook.UpdateCLRModuleDict () [0x00014] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.ImportHook.GetCLRModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.InitExt () [0x00060] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Initialize (System.IntPtr data, System.Int32 size) [0x0002f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.ImportHook.UpdateCLRModuleDict () [0x00014] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.ImportHook.GetCLRModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.InitExt () [0x00060] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Initialize (System.IntPtr data, System.Int32 size) [0x0002f] in <02a0d26b83f24e67b6a0456fd958e703>:0 Failed to shutdown pythonnet: System.InvalidOperationException: ValueFactory attempted to access the Value property of this instance.
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyModule.Import (System.String name) [0x00015] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime+<>c__DisplayClass42_0.<GetModuleLazy>b__0 () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyObject.ToString () [0x00012] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.InternString.Shutdown () [0x0002b] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime.Shutdown () [0x000c3] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.Shutdown () [0x00064] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Shutdown (System.IntPtr data, System.Int32 size) [0x00026] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyModule.Import (System.String name) [0x00015] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime+<>c__DisplayClass42_0.<GetModuleLazy>b__0 () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) [0x00043] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].CreateValue () [0x00047] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Lazy`1[T].get_Value () [0x0000a] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at Python.Runtime.Runtime.get_InteropModule () [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.TryDecodePyErr (Python.Runtime.BorrowedReference typeRef, Python.Runtime.BorrowedReference valRef, Python.Runtime.BorrowedReference tbRef) [0x00000] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.FetchCurrentOrNull (System.Runtime.ExceptionServices.ExceptionDispatchInfo& dispatchInfo) [0x0007e] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonException.ThrowLastAsClrException () [0x00005] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.NewReferenceExtensions.BorrowOrThrow (Python.Runtime.NewReference& reference) [0x0000f] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PyObject.ToString () [0x00012] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.InternString.Shutdown () [0x0002b] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Runtime.Shutdown () [0x000c3] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.PythonEngine.Shutdown () [0x00064] in <02a0d26b83f24e67b6a0456fd958e703>:0 
  at Python.Runtime.Loader.Shutdown (System.IntPtr data, System.Int32 size) [0x00026] in <02a0d26b83f24e67b6a0456fd958e703>:0 Exception ignored in atexit callback: <function unload at 0x7f2102c2c430>
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pythonnet/__init__.py", line 158, in unload
    raise RuntimeError("Failed to call Python.NET shutdown")
RuntimeError: Failed to call Python.NET shutdown
tonyf commented 1 year ago

Resolved by moving the NpOptix out of __aenter__. It looks like it had to due with the container & pythonnet lifecycle events as described here: https://github.com/pythonnet/pythonnet/issues/1701#issuecomment-1054526765