Skip to content

Commit 66ab719

Browse files
committed
Multi times for running cross dispose
1 parent bcfdcc7 commit 66ab719

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

src/embed_tests/TestDomainReload.cs

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Reflection;
45
using System.Runtime.InteropServices;
@@ -256,37 +257,81 @@ public void TestClassReference()
256257
[Test]
257258
public void CrossReleaseBuiltinType()
258259
{
260+
void ExecTest()
261+
{
262+
try
263+
{
264+
var numRef = CreateNumReference();
265+
GC.Collect();
266+
GC.WaitForPendingFinalizers(); // <- this will put former `num` into Finalizer queue
267+
Finalizer.Instance.Collect(forceDispose: true);
268+
// ^- this will call PyObject.Dispose, which will call XDecref on `num.Handle`,
269+
// but Python interpreter from "run" 1 is long gone, so it will corrupt memory instead.
270+
Assert.False(numRef.IsAlive);
271+
}
272+
finally
273+
{
274+
PythonEngine.Shutdown();
275+
}
276+
}
277+
278+
var errorArgs = new List<Finalizer.ErrorArgs>();
279+
void ErrorHandler(object sender, Finalizer.ErrorArgs e)
280+
{
281+
errorArgs.Add(e);
282+
}
283+
Finalizer.Instance.ErrorHandler += ErrorHandler;
259284
try
260285
{
261-
var numRef = CreateNumReference();
262-
GC.Collect();
263-
GC.WaitForPendingFinalizers(); // <- this will put former `num` into Finalizer queue
264-
Finalizer.Instance.Collect(forceDispose: true);
265-
// ^- this will call PyObject.Dispose, which will call XDecref on `num.Handle`,
266-
// but Python interpreter from "run" 1 is long gone, so it will corrupt memory instead.
267-
Assert.False(numRef.IsAlive);
286+
for (int i = 0; i < 10; i++)
287+
{
288+
ExecTest();
289+
}
268290
}
269291
finally
270292
{
271-
PythonEngine.Shutdown();
293+
Finalizer.Instance.ErrorHandler -= ErrorHandler;
272294
}
295+
Assert.AreEqual(errorArgs.Count, 0);
273296
}
274297

275298
[Test]
276299
public void CrossReleaseCustomType()
277300
{
301+
void ExecTest()
302+
{
303+
try
304+
{
305+
var objRef = CreateConcreateObject();
306+
GC.Collect();
307+
GC.WaitForPendingFinalizers();
308+
Finalizer.Instance.Collect(forceDispose: true);
309+
Assert.False(objRef.IsAlive);
310+
}
311+
finally
312+
{
313+
PythonEngine.Shutdown();
314+
}
315+
}
316+
317+
var errorArgs = new List<Finalizer.ErrorArgs>();
318+
void ErrorHandler(object sender, Finalizer.ErrorArgs e)
319+
{
320+
errorArgs.Add(e);
321+
}
322+
Finalizer.Instance.ErrorHandler += ErrorHandler;
278323
try
279324
{
280-
var objRef = CreateConcreateObject();
281-
GC.Collect();
282-
GC.WaitForPendingFinalizers();
283-
Finalizer.Instance.Collect(forceDispose: true);
284-
Assert.False(objRef.IsAlive);
325+
for (int i = 0; i < 10; i++)
326+
{
327+
ExecTest();
328+
}
285329
}
286330
finally
287331
{
288-
PythonEngine.Shutdown();
332+
Finalizer.Instance.ErrorHandler -= ErrorHandler;
289333
}
334+
Assert.AreEqual(errorArgs.Count, 0);
290335
}
291336

292337
private static WeakReference CreateNumReference()

0 commit comments

Comments
 (0)