This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ResourceWarning sometimes doesn't display
Type: resource usage Stage:
Components: Interpreter Core Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, msmhrt, ned.deily, pitrou, python-dev, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2014-07-06 02:49 by msmhrt, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unload_main_first.patch vstinner, 2014-07-07 21:10 review
warnings_shutdown.patch vstinner, 2016-03-24 15:10 review
Messages (14)
msg222390 - (view) Author: Masami HIRATA (msmhrt) Date: 2014-07-06 02:49
It seems that ResouceWarning about unclosed file handles with '-W all' option sometimes doesn't display.
Is this behaviour normal?

$ uname -a
Linux ashrose 3.2.0-65-generic #99-Ubuntu SMP Fri Jul 4 21:03:29 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ python3.4 --version
Python 3.4.1
$ touch spam.txt
$ echo 'a = open("spam.txt")' >test_warning.py
$
$ python3.4 -W all test_warning.py
$ python3.4 -W all test_warning.py
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='spam.txt' mode='r' encoding='UTF-8'>
$ python3.4 -W all test_warning.py
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='spam.txt' mode='r' encoding='UTF-8'>
$ python3.4 -W all test_warning.py
$ python3.4 -W all test_warning.py
$ python3.4 -W all test_warning.py
$ python3.4 -W all test_warning.py
$ python3.4 -W all test_warning.py
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='spam.txt' mode='r' encoding='UTF-8'>
$
msg222392 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2014-07-06 05:30
I believe this is an artifact of hash randomization which affects the order of how objects are destroyed during shutdown.  If you run your test using different values of the PYTHONHASHSEED environment variable, you'll probably see predictable results.  For example, with a particular build of Python 3.4.1, if I set PYTHONHASHSEED set to 0, thereby disabling hash randomization, I never see the warning:

PYTHONHASHSEED=0 python3.4 -W all test_warning.py

With it set to 1, I always see the warning.  With 2, no warning. With no PYTHONHASHSEED, I see random behavior similar to your results.

I don't think there is anything to be done here as Python makes no promises about when and in what order objects are collected.

https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED
msg222517 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-07-07 21:04
The problem is tricky.

The modules are deleted in a random order at exit. The problem is that showing the warning requires to import the linecache module. But during Python finalization, we may or may not be able to import modules.

Py_Finalize() calls PyImport_Cleanup() which starts by setting sys.path and sys.meta_path to None. The _find_spec() function of importlib._bootstrap fails because sys.meta_path is None. In fact, find_spec() starts by emiting a warning which calls formatwarning() which tries to import linecache again... and then importlib emits again a warning... "import linecache" is tried twice and then everything fails.

A workaround would be to unload the __main__ module first, before clearing sys attributes.

Another workaround would be to not try to import modules in warnings.formatwarning() if Python is exiting.

The tricky exit code was already discussed in the issue #19421 ("FileIO destructor imports indirectly the io module at exit"). See also my more general issue #21788 ("Rework Python finalization").
msg222518 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-07-07 21:07
New changeset 047da19efdab by Victor Stinner in branch '3.4':
Issue #21925: PyImport_Cleanup(): Remove unused parameter in
http://hg.python.org/cpython/rev/047da19efdab

New changeset b255ecb175c4 by Victor Stinner in branch 'default':
(Merge 3.4) Issue #21925: PyImport_Cleanup(): Remove unused parameter in
http://hg.python.org/cpython/rev/b255ecb175c4
msg222519 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-07-07 21:10
I don't know if it's the best trick, but here is a patch to unload the __main__ module first in PyImport_Cleanup().
msg222595 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-07-09 01:49
Well, it's unclear to me why we would want to remove the __main__ module first, rather than last.
msg222937 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-07-13 15:14
May be save the timestamp for module importing and then clean up modules them in reversed order?
msg262353 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-24 15:10
It looks like the warnings is logged when the C implemention is used. When the Python implementation is used, "import linecache" or "linecache.getline()" fail, and so the warnings is skipped.

Attached patch makes the Python implementation safer when Python is shutting down. Add "try/except Exception" to ignore exceptions on import, linecache and tracemalloc.
msg262354 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-24 15:11
See also the issue #26637: "importlib: better error message when import fail during Python shutdown".
msg262384 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-24 23:15
It looks like logging a warning at Python exit always works on Python 2.7:
---
import _warnings

class Bla:
    def __del__(self, w=_warnings):
        w.warn_explicit('message', DeprecationWarning, 'x.py', 5)

bla = Bla()
---

It looks like it uses the Python implementation and linecache.getline() always works. Maybe the warning is emitted earlier that the Python 3 code emitting ResourceWarning. I see that warnings.py of Python 2 imports linecache at top level, whereas the Python 3 code imports the module at the first use in showwarning(). I recall that imports at top level are avoided to get faster startup time.
msg262385 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-24 23:34
New changeset 1de90a7065ba by Victor Stinner in branch '3.5':
warnings.formatwarning(): catch exceptions
https://hg.python.org/cpython/rev/1de90a7065ba

New changeset 36be356f6253 by Victor Stinner in branch 'default':
Merge 3.5
https://hg.python.org/cpython/rev/36be356f6253
msg262386 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-24 23:37
I fixed warnings.formatwarning(). I don't expect the code to be perfect (Python shutdown process is complex and fragile), but the fix is quite simple and it's enough to fix the bug described in the initial message. I even added an unit test for it.

I didn't try to force when the __main__ module is unloaded, I didn't see any consensus around that.

Thanks for the bug report, it's now fixed ;-) Sorry for the delay. We had to fix a million of other subtle issues to make the Python shutdown process and the stdlib stronger.
msg262408 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-25 08:51
New changeset 307ba4afd0a6 by Victor Stinner in branch 'default':
Issue #21925: Fix test_warnings for release mode
https://hg.python.org/cpython/rev/307ba4afd0a6
msg262410 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-03-25 09:11
New changeset f474898ef6de by Victor Stinner in branch '3.5':
Issue #21925: Fix test_warnings for release mode
https://hg.python.org/cpython/rev/f474898ef6de
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66124
2016-03-25 09:11:25python-devsetmessages: + msg262410
2016-03-25 08:51:56python-devsetmessages: + msg262408
2016-03-24 23:37:36vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg262386

versions: + Python 3.6, - Python 3.4
2016-03-24 23:34:14python-devsetmessages: + msg262385
2016-03-24 23:15:09vstinnersetmessages: + msg262384
2016-03-24 15:11:07vstinnersetmessages: + msg262354
2016-03-24 15:10:48vstinnersetfiles: + warnings_shutdown.patch

messages: + msg262353
2014-07-13 15:14:11serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg222937
2014-07-13 08:59:40Arfreversetnosy: + Arfrever
title: ResouceWarning sometimes doesn't display -> ResourceWarning sometimes doesn't display

versions: + Python 3.5
2014-07-09 01:49:23pitrousetnosy: + pitrou
messages: + msg222595
2014-07-07 21:10:17vstinnersetfiles: + unload_main_first.patch
keywords: + patch
messages: + msg222519
2014-07-07 21:07:53python-devsetnosy: + python-dev
messages: + msg222518
2014-07-07 21:04:44vstinnersetnosy: + vstinner
messages: + msg222517
2014-07-06 05:30:47ned.deilysetnosy: + ned.deily
messages: + msg222392
2014-07-06 02:49:33msmhrtcreate