Skip to content

Commit

Permalink
To fix memory access exception when iteration breaks in the middle of…
Browse files Browse the repository at this point in the history
… the list before reaching end.
  • Loading branch information
JakeJP authored and lostmsu committed Nov 7, 2023
1 parent 8e8c3f3 commit eef67db
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/runtime/CollectionWrappers/IterableWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ public IEnumerator<T> GetEnumerator()
{
iterObject = PyIter.GetIter(pyObject);
}

using var _ = iterObject;
while (true)
try
{
using var GIL = Py.GIL();

if (!iterObject.MoveNext())
while (true)
{
iterObject.Dispose();
break;
using var _ = Py.GIL();
if (!iterObject.MoveNext())
{
break;
}
yield return iterObject.Current.As<T>()!;
}
yield return iterObject.Current.As<T>()!;
}
finally
{
using var _ = Py.GIL();
iterObject.Dispose();
}
}
}
Expand Down

0 comments on commit eef67db

Please sign in to comment.