Close a rhino document when using rhinoinside from python


I am running a python workflow where I open a rhino file and then use the geometry to generate a mesh for running a numerical simulation. However, occasionally the process fails with a .NET error. See attached snapshot from console.

I think this might be related to that I do not close Rhino properly in the script. I use the doc.Dispose() to close the document, but it does not solve the issue.

Have you experienced similar challenges or any suggestions to what I might try?

If you have a repeatable sample where this happens, I would love to see it. These types of bugs are pretty hard to track down without a repeatable case.