[Python-Dev] Track ResourceWarning warnings with tracemalloc

Victor Stinner victor.stinner at gmail.com
Fri Nov 29 13:12:26 CET 2013


I'm trying to write an example of usage of the new
tracemalloc.get_object_traceback() function. Last month I proposed to
use it to give the traceback where a file/socket was allocated on


I found a working solution using monkey patching of the socket, _pyio
and builtins modules:


This hack uses tracemalloc to retrieve the traceback where the
file/socket was allocated, but you can do the same using
traceback.extract_stack() for older Python version. So it's just an
example to show how tracemalloc can be used.

Example with test_poplib (without res_warn.py):

[1/1] test_poplib
ResourceWarning: unclosed <ssl.SSLSocket fd=5,
family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('',
45747), raddr=('', 48933)>

Ok, you now that the socket was destroyed be the garbage collector...
But which test created this socket???

Example of res_warn.py with test_poplib:

[1/1] test_poplib
ResourceWarning: unclosed <ssl.SSLSocket fd=5,
family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('',
49715), raddr=('', 43904)>
Allocation traceback (most recent first):
  File '/home/haypo/prog/python/default/Lib/ssl.py', line 340
  File '/home/haypo/prog/python/default/Lib/poplib.py', line 390
    self.sock = context.wrap_socket(self.sock)
  File '/home/haypo/prog/python/default/Lib/test/test_poplib.py', line 407
  File '/home/haypo/prog/python/default/Lib/unittest/case.py', line 567
  File '/home/haypo/prog/python/default/Lib/unittest/case.py', line 610
    return self.run(*args, **kwds)

You can see that the socket was created by test_poplib at line 407
TestPOP3_TLSClass.setUp). It's more useful than the previous warning

I found two issues in _pyio and tracemalloc modules. These issues
should be fixed to use res_warn.py on text or buffered files and use
it during Python shutdown:



More information about the Python-Dev mailing list