Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25a4064

Browse files
committed
Add validation for downgrading the shutdown mode
1 parent 19d1379 commit 25a4064

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/runtime/runtime.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,34 @@ private static IntPtr Get_PyObject_NextNotImplemented()
312312
return iternext;
313313
}
314314

315+
/// <summary>
316+
/// Tries to downgrade the shutdown mode, if possible.
317+
/// The only possibles downgrades are:
318+
/// Soft -> Normal
319+
/// Reload -> Soft
320+
/// Reload -> Normal
321+
/// </summary>
322+
/// <param name="mode">The desired shutdown mode</param>
323+
/// <returns>The `mode` parameter if the downgrade is supported, the ShutdownMode
324+
/// set at initialization otherwise.</returns>
325+
static ShutdownMode TryDowngradeShutdown(ShutdownMode mode)
326+
{
327+
if (
328+
mode == Runtime.ShutdownMode
329+
|| mode == ShutdownMode.Normal
330+
#if !NETSTANDARD
331+
|| (mode == ShutdownMode.Soft && Runtime.ShutdownMode == ShutdownMode.Reload)
332+
#endif
333+
)
334+
{
335+
return mode;
336+
}
337+
else // we can't downgrade
338+
{
339+
return Runtime.ShutdownMode;
340+
}
341+
}
342+
315343
internal static void Shutdown(ShutdownMode mode)
316344
{
317345
if (Py_IsInitialized() == 0 || !_isInitialized)
@@ -320,6 +348,11 @@ internal static void Shutdown(ShutdownMode mode)
320348
}
321349
_isInitialized = false;
322350

351+
// If the shutdown mode specified is not the the same as the one specified
352+
// during Initialization, we need to validate it; we can only downgrade,
353+
// not upgrade the shutdown mode.
354+
mode = TryDowngradeShutdown(mode);
355+
323356
var state = PyGILState_Ensure();
324357

325358
if (mode == ShutdownMode.Soft)

0 commit comments

Comments
 (0)