classification
Title: Problem with tempfile.NamedTemporaryFile
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.2, Python 3.1, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, exarkun, loewis, pitrou, rphilips, skrah
Priority: normal Keywords:

Created on 2009-01-13 09:22 by rphilips, last changed 2010-07-08 17:54 by pitrou.

Messages (10)
msg79727 - (view) Author: Richard Philips (rphilips) Date: 2009-01-13 09:22
On Solaris 10 (Solaris 10 5/08 s10x_u5wos_10 X86),
with python 2.5 (Python 2.5.2 (r252:60911, Sep  8 2008, 16:53:36) [C] on
sunos5),

tempfile.NamedTemporaryFile creates - as advertised - a temporary file.

After closing this file object (e.g. by exiting the python process), the
temporary file is indeed removed.

But if you just kill the python process, the temporary file is not
destroyed.

This behavior is different on Red Hat Linux and MS Vista, on these OSes
the temporary file is removed.

A snapshot to illustrate the behavior:

[moto /]# python
Python 2.5.2 (r252:60911, Sep  8 2008, 16:53:36) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, tempfile
>>> f = tempfile.NamedTemporaryFile()
>>> f.name
'/tmp/tmpfHraUd'
>>> os.getpid()
4403
>>> x = "With root priveleges, I execute: 'kill 4403'"
>>>
>>> Terminated
[moto /]#
msg79728 - (view) Author: Richard Philips (rphilips) Date: 2009-01-13 09:26
[Replaces msg 79727]


On Solaris 10 (Solaris 10 5/08 s10x_u5wos_10 X86),
with python 2.5 (Python 2.5.2 (r252:60911, Sep  8 2008, 16:53:36) [C] on
sunos5),

tempfile.NamedTemporaryFile creates - as advertised - a temporary file.

After closing this file object (e.g. by exiting the python process), the
temporary file is indeed removed.

But if you just kill the python process, the temporary file is not
destroyed.

This behavior is different on Red Hat Linux and MS Vista, on these OSes
the temporary file is removed.

A snapshot to illustrate the behavior:

[moto /]# python
Python 2.5.2 (r252:60911, Sep  8 2008, 16:53:36) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, tempfile
>>> f = tempfile.NamedTemporaryFile()
>>> f.name
'/tmp/tmpfHraUd'
>>> os.getpid()
4403
>>> x = "With root priveleges, I execute: 'kill 4403'"
>>>
>>> Terminated
[moto /]# ls /tmp/tmpfHraUd
/tmp/tmpfHraUd
[moto /]#
msg83552 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-03-14 00:41
Martin, do you have time to confirm this problem?
Failure of (Named)TemporaryFile to remove the file on shutdown should be
considered a serious issue.
msg109477 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2010-07-07 14:15
Shouldn't this get looked at as it's high priority, albeit over a year old?
msg109479 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2010-07-07 14:46
I can reproduce this on Ubuntu with Python 2.7 and 3.2.
msg109491 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-07-07 18:46
Unfortunately, I typically don't have time to consider the priority of issues.

However, in the specific case, I also fail to see the bug. Where does it say that they are supposed to be deleted when the process is killed, and what mechanism specifically is supposed to actually perform the deletion?
msg109492 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2010-07-07 18:52
I can't think of any way that you might be able to implement the behavior being requested here.

Instead, if you don't want to leave files lying around, use TemporaryFile instead of NamedTemporaryFile.

Perhaps the documentation for NamedTemporary file could be updated to explain this limitation, though.
msg109493 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-07-07 18:55
> I can't think of any way that you might be able to implement the behavior being requested here.

Thanks for the confirmation; lowering the priority then.

It would, of course, be possible to improve quality by registering an atexit handler. Which, of course, would be futile in case of a SIGKILL or system crash.
msg109505 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-07-07 21:39
> Where does it say that they are supposed to be deleted when the process > is killed,

Well, it is more of a "natural expectation" I guess.

> and what mechanism specifically is supposed to actually perform the
> deletion?

That's not the kind of questions users ask themselves.
If the process shutdown is greatful, either __del__ or a weakref callback is adequate. We probably can't do anything for brutal killings, though.
msg109559 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-07-08 17:54
Ok, my bad. The implementation already has a __del__ method.
History
Date User Action Args
2010-07-08 17:54:33pitrousetmessages: + msg109559
2010-07-07 21:39:42pitrousetmessages: + msg109505
2010-07-07 18:55:53loewissetpriority: high -> normal

messages: + msg109493
2010-07-07 18:52:00exarkunsetnosy: + exarkun
messages: + msg109492
2010-07-07 18:46:27loewissetmessages: + msg109491
2010-07-07 14:47:26skrahsettitle: Problem with tempfile.NamedTemporaryFile on Solaris 10 -> Problem with tempfile.NamedTemporaryFile
2010-07-07 14:46:48skrahsetnosy: + skrah
messages: + msg109479
2010-07-07 14:15:52BreamoreBoysetnosy: + BreamoreBoy

messages: + msg109477
versions: + Python 3.1, Python 3.2, - Python 2.6
2009-03-14 00:41:47pitrousetnosy: + loewis, pitrou
messages: + msg83552
2009-01-15 13:45:42pitrousetpriority: high
stage: test needed
versions: + Python 2.6, Python 2.7, - Python 2.5
2009-01-13 09:26:34rphilipssetmessages: + msg79728
2009-01-13 09:22:45rphilipscreate