<p dir="ltr"><br>
On 19 Mar 2014 06:09, "Tarek Ziadé" <<a href="mailto:tarek@ziade.org">tarek@ziade.org</a>> wrote:<br>
><br>
> Hey<br>
><br>
> ResourceWarning instances that are created in some classes' __del__<br>
> function, like FileIO<br>
> are a great tool to track down a program bad behavior.<br>
><br>
> In network programming, that's even more important to avoid crashes, or<br>
> huge leaks.<br>
><br>
> But some of them are very hard to fix because we don't get much context,<br>
> we just<br>
> get warnings at the end of the program execution, when gc.collect is called.<br>
><br>
> sys:1: ResourceWarning: unclosed file <_io.FileIO name=18 mode='wb'><br>
> sys:1: ResourceWarning: unclosed file <_io.FileIO name=17 mode='rb'><br>
><br>
> Here I just know that somewhere, 2 file descriptors where not closed.<br>
><br>
> What I'd like to be able to do is to track down the origin of those<br>
> warnings.<br>
><br>
> Since __del__ is called asynchronously, it's impossible to track it<br>
> right now (or I don't know how)<br>
><br>
> What we need is a way to keep track of any resource allocation *when it<br>
> happens*.<br>
><br>
> Here's an idea: let's add three private functions in Python's io:<br>
><br>
> def __allocate_resource(fd) => records the file descriptor that was<br>
> allocated, along with the current traceback.<br>
> def __free_resource(fd) => removes the fd from the list.<br>
> def __is_resource_allocated(fd) => tell if the resource is in the list.<br>
><br>
> These three functions, plugged in somewhere in io's classes, could be<br>
> used in conjunction with ResourceWarning:<br>
> when __del__ is called, if the resource was not freed - we'd be able to<br>
> know where it was created.<br>
><br>
> Of course these functions are just a brain dump - I have no idea how io<br>
> internals work. But unless I missed it,<br>
> something like I've just described is missing in Python.</p>
<p dir="ltr">You should be able to experiment with something based on tracemalloc (although it may require patching the io implementation or else installing a GC callback that looks for particular types).</p>
<p dir="ltr">Cheers,<br>
Nick.</p>
<p dir="ltr">><br>
> Cheers<br>
> Tarek<br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a><br>
</p>