Skip to content

Commit 99fcc61

Browse files
authored
Revert "bpo-36356: Destroy the GIL at exit (GH-12453)" (GH613006)
This reverts commit b36e5d6.
1 parent b36e5d6 commit 99fcc61

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

Include/ceval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
192192

193193
PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
194194
PyAPI_FUNC(void) PyEval_InitThreads(void);
195+
#ifndef Py_LIMITED_API
196+
PyAPI_FUNC(void) _PyEval_FiniThreads(void);
197+
#endif /* !Py_LIMITED_API */
195198
PyAPI_FUNC(void) PyEval_AcquireLock(void) Py_DEPRECATED(3.2);
196199
PyAPI_FUNC(void) PyEval_ReleaseLock(void) /* Py_DEPRECATED(3.2) */;
197200
PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);

Include/internal/pycore_ceval.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ struct _ceval_runtime_state {
5454

5555
PyAPI_FUNC(void) _PyEval_Initialize(struct _ceval_runtime_state *);
5656

57-
PyAPI_FUNC(void) _PyEval_FiniThreads(void);
58-
PyAPI_FUNC(void) _PyEval_FiniThreads2(void);
59-
6057
#ifdef __cplusplus
6158
}
6259
#endif

Modules/main.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* Python interpreter main program */
22

33
#include "Python.h"
4-
#include "pycore_ceval.h" /* _PyEval_FiniThreads2() */
54
#include "pycore_coreconfig.h"
65
#include "pycore_pylifecycle.h"
76
#include "pycore_pymem.h"
@@ -526,15 +525,15 @@ pymain_run_python(int *exitcode)
526525

527526
/* --- pymain_main() ---------------------------------------------- */
528527

529-
/* Free global variables which cannot be freed in Py_Finalize():
530-
configuration options set before Py_Initialize() which should
531-
remain valid after Py_Finalize(), since
532-
Py_Initialize()-Py_Finalize() can be called multiple times. */
533528
static void
534529
pymain_free(void)
535530
{
536531
_PyImport_Fini2();
537-
_PyEval_FiniThreads2();
532+
533+
/* Free global variables which cannot be freed in Py_Finalize():
534+
configuration options set before Py_Initialize() which should
535+
remain valid after Py_Finalize(), since
536+
Py_Initialize()-Py_Finalize() can be called multiple times. */
538537
_PyPathConfig_ClearGlobal();
539538
_Py_ClearStandardStreamEncoding();
540539
_Py_ClearArgcArgv();

Python/ceval.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,20 @@ PyEval_InitThreads(void)
188188
}
189189
}
190190

191-
192191
void
193192
_PyEval_FiniThreads(void)
194-
{
195-
if (_PyRuntime.ceval.pending.lock != NULL) {
196-
PyThread_free_lock(_PyRuntime.ceval.pending.lock);
197-
_PyRuntime.ceval.pending.lock = NULL;
198-
}
199-
}
200-
201-
202-
void
203-
_PyEval_FiniThreads2(void)
204193
{
205194
if (!gil_created()) {
206195
return;
207196
}
208197

209198
destroy_gil();
210199
assert(!gil_created());
200+
201+
if (_PyRuntime.ceval.pending.lock != NULL) {
202+
PyThread_free_lock(_PyRuntime.ceval.pending.lock);
203+
_PyRuntime.ceval.pending.lock = NULL;
204+
}
211205
}
212206

213207
static inline void

Python/pylifecycle.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
#include "Python-ast.h"
66
#undef Yield /* undefine macro conflicting with <winbase.h> */
7-
#include "pycore_ceval.h" /* _PyEval_FiniThreads() */
8-
#include "pycore_context.h"
97
#include "pycore_coreconfig.h"
8+
#include "pycore_context.h"
109
#include "pycore_fileutils.h"
1110
#include "pycore_hamt.h"
1211
#include "pycore_pathconfig.h"
@@ -556,11 +555,12 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
556555
return _Py_INIT_ERR("can't make first thread");
557556
(void) PyThreadState_Swap(tstate);
558557

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

565565
/* Auto-thread-state API */
566566
_PyGILState_Init(runtime, interp, tstate);
@@ -1357,7 +1357,6 @@ Py_FinalizeEx(void)
13571357

13581358
call_ll_exitfuncs(runtime);
13591359

1360-
_PyEval_FiniThreads();
13611360
_PyRuntime_Finalize();
13621361
return status;
13631362
}

0 commit comments

Comments
 (0)