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
Shut down by default without allowing reload, throw on attempt
  • Loading branch information
filmor committed Sep 23, 2023
commit 85f74bbf6b645a8922e3de0d79f6bbba420917ad
11 changes: 8 additions & 3 deletions src/runtime/PythonEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,13 @@ static void LoadMixins(BorrowedReference targetModuleDict)

static void OnDomainUnload(object _, EventArgs __)
{
Shutdown();
Shutdown(allowReload: true);
}

static void OnProcessExit(object _, EventArgs __)
{
Runtime.ProcessIsTerminating = true;
Shutdown();
Shutdown(allowReload: false);
}

/// <summary>
Expand Down Expand Up @@ -366,6 +366,11 @@ private static void AllowLeaksDuringShutdown(object sender, Finalizer.ErrorArgs
/// after calling the Shutdown method.
/// </summary>
public static void Shutdown()
{
Shutdown(allowReload: false);
}

public static void Shutdown(bool allowReload)
{
if (!initialized)
{
Expand All @@ -389,7 +394,7 @@ public static void Shutdown()

ExecuteShutdownHandlers();
// Remember to shut down the runtime.
Runtime.Shutdown();
Runtime.Shutdown(allowReload);

initialized = false;

Expand Down
18 changes: 14 additions & 4 deletions src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ internal static int GetRun()
return runNumber;
}

internal static bool HostedInPython;
internal static bool ProcessIsTerminating;
internal static bool HostedInPython = false;
internal static bool ProcessIsTerminating = false;
internal static bool ShutdownWithoutReload = false;

/// <summary>
/// Initialize the runtime...
Expand All @@ -112,6 +113,14 @@ internal static void Initialize(bool initSigs = false)
}
_isInitialized = true;

if (ShutdownWithoutReload)
{
throw new Exception(
"Runtime was shut down without allowReload: true, can " +
"not be restarted in this process"
);
}

bool interpreterAlreadyInitialized = TryUsingDll(
() => Py_IsInitialized() != 0
);
Expand Down Expand Up @@ -253,17 +262,18 @@ private static void InitPyMembers()
return Util.ReadPtr<NativeFunc>(pyType.Borrow(), TypeOffset.tp_iternext);
}

internal static void Shutdown()
internal static void Shutdown(bool allowReload)
{
if (Py_IsInitialized() == 0 || !_isInitialized)
{
return;
}
_isInitialized = false;
ShutdownWithoutReload = !allowReload;

var state = PyGILState_Ensure();

if (!HostedInPython && !ProcessIsTerminating)
if (!HostedInPython && !ProcessIsTerminating && allowReload)
{
// avoid saving dead objects
TryCollectingGarbage(runs: 3);
Expand Down