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.

Author sbt
Recipients nadeem.vawda, neologix, pitrou, sbt
Date 2012-04-12.22:30:10
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1334269810.74.0.569809857732.issue14548@psf.upfronthosting.co.za>
In-reply-to
Content
> 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.)
History
Date User Action Args
2012-04-12 22:30:10sbtsetrecipients: + sbt, pitrou, nadeem.vawda, neologix
2012-04-12 22:30:10sbtsetmessageid: <1334269810.74.0.569809857732.issue14548@psf.upfronthosting.co.za>
2012-04-12 22:30:10sbtlinkissue14548 messages
2012-04-12 22:30:10sbtcreate