when process is exiting, there's no need to save live .NET objects as… · pythonnet/pythonnet@8d61215

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -296,6 +296,7 @@ static void OnDomainUnload(object _, EventArgs __)

296296
297297

static void OnProcessExit(object _, EventArgs __)

298298

{

299+

Runtime.ProcessIsTerminating = true;

299300

Shutdown();

300301

}

301302
Original file line numberDiff line numberDiff line change

@@ -92,6 +92,7 @@ internal static int GetRun()

9292

}

9393
9494

internal static bool HostedInPython;

95+

internal static bool ProcessIsTerminating;

9596
9697

/// Initialize the runtime...

9798

/// </summary>

@@ -254,7 +255,7 @@ internal static void Shutdown()

254255
255256

var state = PyGILState_Ensure();

256257
257-

if (!HostedInPython)

258+

if (!HostedInPython && !ProcessIsTerminating)

258259

{

259260

// avoid saving dead objects

260261

TryCollectingGarbage(runs: 3);