Skip to content

bpo-36356: Destroy the GIL at exit #12453

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

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 0 additions & 3 deletions Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);

PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
PyAPI_FUNC(void) PyEval_InitThreads(void);
#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyEval_FiniThreads(void);
#endif /* !Py_LIMITED_API */
PyAPI_FUNC(void) PyEval_AcquireLock(void) Py_DEPRECATED(3.2);
PyAPI_FUNC(void) PyEval_ReleaseLock(void) /* Py_DEPRECATED(3.2) */;
PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
Expand Down
3 changes: 3 additions & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ struct _ceval_runtime_state {

PyAPI_FUNC(void) _PyEval_Initialize(struct _ceval_runtime_state *);

PyAPI_FUNC(void) _PyEval_FiniThreads(void);
PyAPI_FUNC(void) _PyEval_FiniThreads2(void);

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 6 additions & 5 deletions Modules/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Python interpreter main program */

#include "Python.h"
#include "pycore_ceval.h" /* _PyEval_FiniThreads2() */
#include "pycore_coreconfig.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
Expand Down Expand Up @@ -525,15 +526,15 @@ pymain_run_python(int *exitcode)

/* --- pymain_main() ---------------------------------------------- */

/* Free global variables which cannot be freed in Py_Finalize():
configuration options set before Py_Initialize() which should
remain valid after Py_Finalize(), since
Py_Initialize()-Py_Finalize() can be called multiple times. */
static void
pymain_free(void)
{
_PyImport_Fini2();

/* Free global variables which cannot be freed in Py_Finalize():
configuration options set before Py_Initialize() which should
remain valid after Py_Finalize(), since
Py_Initialize()-Py_Finalize() can be called multiple times. */
_PyEval_FiniThreads2();
_PyPathConfig_ClearGlobal();
_Py_ClearStandardStreamEncoding();
_Py_ClearArgcArgv();
Expand Down
16 changes: 11 additions & 5 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,26 @@ PyEval_InitThreads(void)
}
}


void
_PyEval_FiniThreads(void)
{
if (_PyRuntime.ceval.pending.lock != NULL) {
PyThread_free_lock(_PyRuntime.ceval.pending.lock);
_PyRuntime.ceval.pending.lock = NULL;
}
}


void
_PyEval_FiniThreads2(void)
{
if (!gil_created()) {
return;
}

destroy_gil();
assert(!gil_created());

if (_PyRuntime.ceval.pending.lock != NULL) {
PyThread_free_lock(_PyRuntime.ceval.pending.lock);
_PyRuntime.ceval.pending.lock = NULL;
}
}

static inline void
Expand Down
11 changes: 6 additions & 5 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#include "Python-ast.h"
#undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_coreconfig.h"
#include "pycore_ceval.h" /* _PyEval_FiniThreads() */
#include "pycore_context.h"
#include "pycore_coreconfig.h"
#include "pycore_fileutils.h"
#include "pycore_hamt.h"
#include "pycore_pathconfig.h"
Expand Down Expand Up @@ -555,12 +556,11 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return _Py_INIT_ERR("can't make first thread");
(void) PyThreadState_Swap(tstate);

/* We can't call _PyEval_FiniThreads() in Py_FinalizeEx because
destroying the GIL might fail when it is being referenced from
another running thread (see issue #9901).
/* Destroying the GIL in Py_FinalizeEx might fail when it is being
referenced from another running thread (see bpo-9901).
Instead we destroy the previously created GIL here, which ensures
that we can call Py_Initialize / Py_FinalizeEx multiple times. */
_PyEval_FiniThreads();
_PyEval_FiniThreads2();

/* Auto-thread-state API */
_PyGILState_Init(runtime, interp, tstate);
Expand Down Expand Up @@ -1357,6 +1357,7 @@ Py_FinalizeEx(void)

call_ll_exitfuncs(runtime);

_PyEval_FiniThreads();
_PyRuntime_Finalize();
return status;
}
Expand Down