System.AccessViolationException VS Code Rhino Inside


After using the following line of Python code using Rhino.Inside Python in VS Code, I get an error after the script finishes. Could someone suggest how to fix this exception? I am not even sure if this line specifically causes the problem, but I noticed the script works fine when I skip this one.

centroids = [mesh.Faces.GetFaceCenter(i) for i in range(mesh.Faces.Count)]


Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Python.Runtime.Runtime.PyObject_TYPE(BorrowedReference op)
   at Python.Runtime.Runtime.NullGCHandles(IEnumerable`1 objects)
   at Python.Runtime.Runtime.TryCollectingGarbage(Int32 runs, Boolean forceBreakLoops)
   at Python.Runtime.Runtime.Shutdown()
   at Python.Runtime.PythonEngine.Shutdown()
   at Python.Runtime.Loader.Shutdown(IntPtr data, Int32 size)

Thank you for your time!!

Based on further inspection I do not expect this exception to be caused by the GetFaceCenter specifically. It seems to happen when I use other functions in a loop with a lot of iterations as well.