@@ -329,15 +329,40 @@ Py_Finalize(void)
329329 warnings_module = NULL ;
330330
331331 /* Collect garbage. This may call finalizers; it's nice to call these
332- before all modules are destroyed. */
332+ * before all modules are destroyed.
333+ * XXX If a __del__ or weakref callback is triggered here, and tries to
334+ * XXX import a module, bad things can happen, because Python no
335+ * XXX longer believes it's initialized.
336+ * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
337+ * XXX is easy to provoke that way. I've also seen, e.g.,
338+ * XXX Exception exceptions.ImportError: 'No module named sha'
339+ * XXX in <function callback at 0x008F5718> ignored
340+ * XXX but I'm unclear on exactly how that one happens. In any case,
341+ * XXX I haven't seen a real-life report of either of these.
342+ */
333343 PyGC_Collect ();
334344
335345 /* Destroy all modules */
336346 PyImport_Cleanup ();
337347
338348 /* Collect final garbage. This disposes of cycles created by
339- new-style class definitions, for example. */
349+ * new-style class definitions, for example.
350+ * XXX This is disabled because it caused too many problems. If
351+ * XXX a __del__ or weakref callback triggers here, Python code has
352+ * XXX a hard time running, because even the sys module has been
353+ * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
354+ * XXX One symptom is a sequence of information-free messages
355+ * XXX coming from threads (if a __del__ or callback is invoked,
356+ * XXX other threads can execute too, and any exception they encounter
357+ * XXX triggers a comedy of errors as subsystem after subsystem
358+ * XXX fails to find what it *expects* to find in sys to help report
359+ * XXX the exception and consequent unexpected failures). I've also
360+ * XXX seen segfaults then, after adding print statements to the
361+ * XXX Python code getting called.
362+ */
363+ #if 0
340364 PyGC_Collect ();
365+ #endif
341366
342367 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
343368 _PyImport_Fini ();
0 commit comments