comparison Python/pythonrun.c @ 87070:c2a13acd5e2b

Close #19466: Clear the frames of daemon threads earlier during the Python shutdown to call objects destructors. So "unclosed file" resource warnings are now corretly emitted for daemon threads.
author Victor Stinner <victor.stinner@gmail.com>
date Tue, 12 Nov 2013 16:37:55 +0100
parents 01c4a0af73cf
children 10a8e676b87b
comparison
equal deleted inserted replaced
87069:cb05beabb656 87070:c2a13acd5e2b
574 /* Remaining threads (e.g. daemon threads) will automatically exit 574 /* Remaining threads (e.g. daemon threads) will automatically exit
575 after taking the GIL (in PyEval_RestoreThread()). */ 575 after taking the GIL (in PyEval_RestoreThread()). */
576 _Py_Finalizing = tstate; 576 _Py_Finalizing = tstate;
577 initialized = 0; 577 initialized = 0;
578 578
579 /* Flush stdout+stderr */ 579 /* Destroy the state of all threads except of the current thread: in
580 flush_std_files(); 580 practice, only daemon threads should still be alive. Clear frames of
581 581 other threads to call objects destructor. Destructors will be called in
582 /* Disable signal handling */ 582 the current Python thread. Since _Py_Finalizing has been set, no other
583 PyOS_FiniInterrupts(); 583 Python threads can lock the GIL at this point (if they try, they will
584 exit immediatly). */
585 _PyThreadState_DeleteExcept(tstate);
584 586
585 /* Collect garbage. This may call finalizers; it's nice to call these 587 /* Collect garbage. This may call finalizers; it's nice to call these
586 * before all modules are destroyed. 588 * before all modules are destroyed.
587 * XXX If a __del__ or weakref callback is triggered here, and tries to 589 * XXX If a __del__ or weakref callback is triggered here, and tries to
588 * XXX import a module, bad things can happen, because Python no 590 * XXX import a module, bad things can happen, because Python no
593 * XXX in <function callback at 0x008F5718> ignored 595 * XXX in <function callback at 0x008F5718> ignored
594 * XXX but I'm unclear on exactly how that one happens. In any case, 596 * XXX but I'm unclear on exactly how that one happens. In any case,
595 * XXX I haven't seen a real-life report of either of these. 597 * XXX I haven't seen a real-life report of either of these.
596 */ 598 */
597 PyGC_Collect(); 599 PyGC_Collect();
600
598 #ifdef COUNT_ALLOCS 601 #ifdef COUNT_ALLOCS
599 /* With COUNT_ALLOCS, it helps to run GC multiple times: 602 /* With COUNT_ALLOCS, it helps to run GC multiple times:
600 each collection might release some types from the type 603 each collection might release some types from the type
601 list, so they become garbage. */ 604 list, so they become garbage. */
602 while (PyGC_Collect() > 0) 605 while (PyGC_Collect() > 0)
603 /* nothing */; 606 /* nothing */;
604 #endif 607 #endif
608
609 /* Flush stdout+stderr */
610 flush_std_files();
611
612 /* Disable signal handling */
613 PyOS_FiniInterrupts();
614
605 /* Destroy all modules */ 615 /* Destroy all modules */
606 PyImport_Cleanup(); 616 PyImport_Cleanup();
607 617
608 /* Flush stdout+stderr (again, in case more was printed) */ 618 /* Flush stdout+stderr (again, in case more was printed) */
609 flush_std_files(); 619 flush_std_files();