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

Skip to content

Commit 51fa458

Browse files
committed
Issue #18203: Fix Py_Finalize(): destroy the GIL after the last call to
PyMem_Malloc() or PyObject_Malloc(). For example, PyCFunction_Fini() calls PyObject_GC_Del() which calls PyObject_FREE().
1 parent b385529 commit 51fa458

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Python/pythonrun.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,6 @@ Py_Finalize(void)
606606

607607
_PyExc_Fini();
608608

609-
/* Cleanup auto-thread-state */
610-
#ifdef WITH_THREAD
611-
_PyGILState_Fini();
612-
#endif /* WITH_THREAD */
613-
614609
/* Sundry finalizers */
615610
PyMethod_Fini();
616611
PyFrame_Fini();
@@ -629,10 +624,6 @@ Py_Finalize(void)
629624
/* Cleanup Unicode implementation */
630625
_PyUnicode_Fini();
631626

632-
/* Delete current thread. After this, many C API calls become crashy. */
633-
PyThreadState_Swap(NULL);
634-
PyInterpreterState_Delete(interp);
635-
636627
/* reset file system default encoding */
637628
if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
638629
free((char*)Py_FileSystemDefaultEncoding);
@@ -647,6 +638,15 @@ Py_Finalize(void)
647638

648639
PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
649640

641+
/* Cleanup auto-thread-state */
642+
#ifdef WITH_THREAD
643+
_PyGILState_Fini();
644+
#endif /* WITH_THREAD */
645+
646+
/* Delete current thread. After this, many C API calls become crashy. */
647+
PyThreadState_Swap(NULL);
648+
PyInterpreterState_Delete(interp);
649+
650650
#ifdef Py_TRACE_REFS
651651
/* Display addresses (& refcnts) of all objects still alive.
652652
* An address can be used to find the repr of the object, printed

0 commit comments

Comments
 (0)