Skip to content

Skip garbage collection on process shutdown #2245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix refactored tests
  • Loading branch information
filmor committed Sep 23, 2023
commit 4fe3757636be91e9fe52c3a7029c089f79146e12
2 changes: 1 addition & 1 deletion src/embed_tests/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void BaseSetup()
[OneTimeTearDown]
public void BaseTearDown()
{
PyObjectConversions.Reset();
PythonEngine.Shutdown(allowReload: true);
PyObjectConversions.Reset();
}
}
6 changes: 6 additions & 0 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace Python.EmbeddingTest;

public class Codecs : BaseFixture
{
[SetUp]
public void SetUp()
{
PyObjectConversions.Reset();
}

[Test]
public void TupleConversionsGeneric()
{
Expand Down
5 changes: 2 additions & 3 deletions src/embed_tests/TestDomainReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,13 @@ public static void InitPython(string dllName)
public static void ShutdownPython()
{
PythonEngine.EndAllowThreads(_state);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

public static void ShutdownPythonCompletely()
{
PythonEngine.EndAllowThreads(_state);

PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

static void OnDomainUnload(object sender, EventArgs e)
Expand Down
6 changes: 3 additions & 3 deletions src/embed_tests/TestFinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void SetUp()
public void TearDown()
{
Finalizer.Instance.Threshold = _oldThreshold;
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

private static void FullGCCollect()
Expand Down Expand Up @@ -89,7 +89,7 @@ public void CollectBasicObject()
}

[Test]
[Obsolete("GC tests are not guaranteed")]
// [Obsolete("GC tests are not guaranteed")]
public void CollectOnShutdown()
{
IntPtr op = MakeAGarbage(out var shortWeak, out var longWeak);
Expand All @@ -100,7 +100,7 @@ public void CollectOnShutdown()
Assert.IsTrue(garbage.Contains(op),
"Garbage should contains the collected object");

PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
garbage = Finalizer.Instance.GetCollectedObjects();

if (garbage.Count > 0)
Expand Down
26 changes: 13 additions & 13 deletions src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void GetPythonPathDefault()
string s = PythonEngine.PythonPath;

StringAssert.Contains("python", s.ToLower());
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

[Test]
Expand All @@ -89,7 +89,7 @@ public static void GetProgramNameDefault()
string s = PythonEngine.ProgramName;

Assert.NotNull(s);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

/// <summary>
Expand All @@ -105,15 +105,15 @@ public static void GetPythonHomeDefault()
string enginePythonHome = PythonEngine.PythonHome;

Assert.AreEqual(envPythonHome, enginePythonHome);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

[Test]
public void SetPythonHome()
{
PythonEngine.Initialize();
var pythonHomeBackup = PythonEngine.PythonHome;
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

if (pythonHomeBackup == "")
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
Expand All @@ -124,7 +124,7 @@ public void SetPythonHome()
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

// Restoring valid pythonhome.
PythonEngine.PythonHome = pythonHomeBackup;
Expand All @@ -135,7 +135,7 @@ public void SetPythonHomeTwice()
{
PythonEngine.Initialize();
var pythonHomeBackup = PythonEngine.PythonHome;
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

if (pythonHomeBackup == "")
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
Expand All @@ -147,7 +147,7 @@ public void SetPythonHomeTwice()
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

PythonEngine.PythonHome = pythonHomeBackup;
}
Expand All @@ -161,23 +161,23 @@ public void SetPythonHomeEmptyString()
var backup = PythonEngine.PythonHome;
if (backup == "")
{
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
}
PythonEngine.PythonHome = "";

Assert.AreEqual("", PythonEngine.PythonHome);

PythonEngine.PythonHome = backup;
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

[Test]
public void SetProgramName()
{
if (PythonEngine.IsInitialized)
{
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

var programNameBackup = PythonEngine.ProgramName;
Expand All @@ -188,7 +188,7 @@ public void SetProgramName()
PythonEngine.Initialize();

Assert.AreEqual(programName, PythonEngine.ProgramName);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

PythonEngine.ProgramName = programNameBackup;
}
Expand Down Expand Up @@ -220,14 +220,14 @@ public void SetPythonPath()
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.

PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

PythonEngine.PythonPath = path;
PythonEngine.Initialize();

Assert.AreEqual(path, PythonEngine.PythonPath);
if (importShouldSucceed) Py.Import(moduleName);

PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}
}
41 changes: 19 additions & 22 deletions src/embed_tests/pyinitialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,36 @@ public class PyInitializeTest
public static void StartAndStopTwice()
{
PythonEngine.Initialize();
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

PythonEngine.Initialize();
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

[Test]
public static void LoadDefaultArgs()
{
using (new PythonEngine())
PythonEngine.Initialize();
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
{
using(var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
{
Assert.AreNotEqual(0, argv.Length());
}
Assert.AreNotEqual(0, argv.Length());
}
PythonEngine.Shutdown(allowReload: true);
}

[Test]
public static void LoadSpecificArgs()
{
var args = new[] { "test1", "test2" };
using (new PythonEngine(args))
PythonEngine.Initialize(args);
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
{
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
{
using var v0 = argv[0];
using var v1 = argv[1];
Assert.AreEqual(args[0], v0.ToString());
Assert.AreEqual(args[1], v1.ToString());
}
using var v0 = argv[0];
using var v1 = argv[1];
Assert.AreEqual(args[0], v0.ToString());
Assert.AreEqual(args[1], v1.ToString());
}
PythonEngine.Shutdown(allowReload: true);
}

// regression test for https://github.com/pythonnet/pythonnet/issues/1561
Expand All @@ -63,7 +61,7 @@ public void ImportClassShutdownRefcount()

Assert.Less(Runtime.Runtime.Refcount32(clsRef), 256);

PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
Assert.Greater(Runtime.Runtime.Refcount32(clsRef), 0);
}

Expand All @@ -73,7 +71,6 @@ public void ImportClassShutdownRefcount()
/// More complex version of StartAndStopTwice test
/// </summary>
[Test]
[Ignore("GH#376: System.OverflowException : Arithmetic operation resulted in an overflow")]
//[Ignore("System.ArgumentException : Cannot pass a GCHandle across AppDomains")]
public void ReInitialize()
{
Expand All @@ -84,7 +81,7 @@ public void ReInitialize()
// Import any class or struct from .NET
PythonEngine.RunSimpleString(code);
}
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);

PythonEngine.Initialize();
using (Py.GIL())
Expand All @@ -97,7 +94,7 @@ public void ReInitialize()
// If replacing int with Int64, OverflowException will be replaced with AppDomain exception.
PythonEngine.RunSimpleString("Int32(1)");
}
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
}

/// <summary>
Expand All @@ -123,15 +120,15 @@ public void ShutdownHandlers()
shutdown_count = 0;
PythonEngine.Initialize();
PythonEngine.AddShutdownHandler(OnShutdownIncrement);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
Assert.That(shutdown_count, Is.EqualTo(1));

// Test we can run multiple shutdown handlers in the right order.
shutdown_count = 4;
PythonEngine.Initialize();
PythonEngine.AddShutdownHandler(OnShutdownIncrement);
PythonEngine.AddShutdownHandler(OnShutdownDouble);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
// Correct: 4 * 2 + 1 = 9
// Wrong: (4 + 1) * 2 = 10
Assert.That(shutdown_count, Is.EqualTo(9));
Expand All @@ -145,7 +142,7 @@ public void ShutdownHandlers()
PythonEngine.AddShutdownHandler(OnShutdownIncrement);
PythonEngine.AddShutdownHandler(OnShutdownDouble);
PythonEngine.RemoveShutdownHandler(OnShutdownDouble);
PythonEngine.Shutdown();
PythonEngine.Shutdown(allowReload: true);
// Correct: (4 + 1) * 2 + 1 + 1 = 12
// Wrong: (4 * 2) + 1 + 1 + 1 = 11
Assert.That(shutdown_count, Is.EqualTo(12));
Expand Down