bpo-41162: Clear audit hooks later during finalization (GH-21222)

Co-authored-by: Konge <zkonge@outlook.com>
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index eb065eb..e218808 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1303,6 +1303,13 @@
 
     finalize_interp_types(tstate, is_main_interp);
 
+    /* Clear all loghooks */
+    /* Both _PySys_Audit function and users still need PyObject, such as tuple.
+       Call _PySys_ClearAuditHooks when PyObject available. */
+    if (is_main_interp) {
+        _PySys_ClearAuditHooks(tstate);
+    }
+
     if (is_main_interp) {
         /* XXX Still allocated:
            - various static ad-hoc pointers to interned strings
@@ -1419,9 +1426,6 @@
      */
     _PyGC_CollectIfEnabled();
 
-    /* Clear all loghooks */
-    _PySys_ClearAuditHooks(tstate);
-
     /* Destroy all modules */
     _PyImport_Cleanup(tstate);