sbt <shibt...@gmail.com> added the comment:

> But what if Finalize is used to cleanup a resource that gets 
> duplicated in children, like a file descriptor?
> See e.g. forking.py, line 137 (in Popen.__init__())
> or heap.py, line 244 (BufferWrapper.__init__()).

This was how Finalize objects already acted (or were supposed to).

In the case of BufferWrapper this is intended.  BufferWrapper objects do not 
have reference counting semantics.  Instead the memory is deallocated when the 
object is garbage collected in the process that created it.  (Garbage 
collection in a child process should *not* invalidate memory owned by the 
parent process.)  You can prevent the parent process from garbage collecting 
the object too early by following the advice below from the documentation:

  Explicitly pass resources to child processes

    On Unix a child process can make use of a shared resource created in a 
    parent process using a global resource. However, it is better to pass 
    the object as an argument to the constructor for the child process.

    Apart from making the code (potentially) compatible with Windows this 
    also ensures that as long as the child process is still alive the object 
    will not be garbage collected in the parent process. This might be 
important 
    if some resource is freed when the object is garbage collected in the 
parent process.

In the case of the sentinel in Popen.__init__(), it is harmless if this end of 
the pipe gets accidentally inherited by another process.  Since Process does 
not have a closefds argument like subprocess.Popen unintended leaking happens 
all the time.  And even without the pid check, I think this finalizer would 
very rarely be triggered in a child process.  (A Process object can only be 
garbage collected after it has been joined, and it can only be joined by it 
parent process.)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue14548>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to