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.

classification
Title: Python keeps file references after calling close methode
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eternia, georg.brandl, isandler, jepler
Priority: normal Keywords:

Created on 2005-04-10 15:20 by eternia, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg24993 - (view) Author: Eelco (eternia) Date: 2005-04-10 15:20
I found this bug using a python script that:

- first mounts a partition  (os.system("mount") etc)
- change a few files on this partition (f = open ();
f.write; f.close)
- umounts the partition (os.system("umount") etc)

Strangely, the umount didn't work because of a
filesystem busy error. Using fuser and lsof i traced
this being busy back to the script itself. This is
strange behavior because after changing the files on
the mounted partition the close method was called which
should close all references to the file on the partition. 

Finally the solution was to do f = 0. So if python has
closed a file on a mount a open reference to that file
will keep to exist until the script has ended or until
the file object is nullified.



msg24994 - (view) Author: Jeff Epler (jepler) Date: 2005-04-14 19:50
Logged In: YES 
user_id=2772

I tried but can't reproduce this problem.

After mounting a filesystem on /mnt/tmp, I ran the following
in an interactive session:
>>> f = open("/mnt/tmp/x", "w")
>>> f.write("testing")
>>> f.close()
>>> import os; os.system("umount /mnt/tmp")
0

Did you write 'f.close()' or 'f.close'?  The former closes
the file, but the latter is a statement which has no effect.
 When you later executed 'f=0', the reference count of
the-old-value-of-f dropped to 0 and the file was close()d
impliclty.
msg24995 - (view) Author: Ilya Sandler (isandler) Date: 2005-06-26 20:01
Logged In: YES 
user_id=971153

I also failed to reproduce the problem with a recent Python
build on Debian (python session at the bottom)
 
Suggest closing the bug.

 bagira:~/python> dist/src/python
 Python 2.5a0 (#18, Jun  2 2005, 17:12:48) 
 [GCC 2.95.4 20011002 (Debian prerelease)] on linux2
 Type "help", "copyright", "credits" or "license" for more
information.
 >>> import os
 >>> os.system("mount /floppy")
 0
 >>> f=open("/floppy/test","w")
 >>> f.write('abcd')
 >>> f.close()
 >>> os.system('umount /floppy')
 0
msg24996 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-06-26 20:39
Logged In: YES 
user_id=1188172

I think so too and close this as Invalid.

That the resources were freed when the file object was
cleaned up shows that the submitter must have made a mistake
since the destructor executes exactly the same code as close().
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41839
2005-04-10 15:20:04eterniacreate