Django

Code

Ticket #8175 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

FileStorage-rf: Why does 'close' call 'open'?

Reported by: magneto Assigned to: nobody
Milestone: 1.0 Component: File uploads/storage
Version: SVN Keywords: filefield fs-rf
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Just got the new FileStorage? re-factor, ran a battery of little tests on a site of mine and got this very strange backtrace when trying to delete a file

and YES, there is a signal that does some fancy things with the original file location (likes moves is somewhere rather then really deleting it)

so the file is really 'not' in the original location anymore, but it does seems strange to me that the Field would bounce all the way down to OPEN a file it wants CLOSED

File " assets/views.py" in simple_asset_delete
  597. 	asset.delete()
File " assets/models.py" in delete
  538. 		super(Asset, self).delete()
File " django/db/models/base.py" in delete
  429.         delete_objects(seen_objs)
File " django/db/models/query.py" in delete_objects
  846.             signals.post_delete.send(sender=cls, instance=instance)
File " django/dispatch/dispatcher.py" in send
  133.             response = receiver(signal=self, sender=sender, **named)
File " django/db/models/fields/files.py" in delete_file
  208.                 file.delete(save=False)
File " django/db/models/fields/files.py" in delete
  291.         super(ImageFieldFile, self).delete(save)
File " django/db/models/fields/files.py" in delete
  83.         self.close()
File " django/core/files/base.py" in close
  147.         self.file.close()
File " django/db/models/fields/files.py" in _get_file
  45.             self._file = self.storage.open(self.name, 'rb')
File " django/core/files/storage.py" in open
  27.         file = self._open(name, mode)
File " django/core/files/storage.py" in _open
  140.         return File(open(self.path(name), mode))

Exception Type: IOError at /delete/asset/86263/
Exception Value: [Errno 2] No such file or directory: u'86/862/86263/mfrohne_2941966_01'

Attachments

close_open_trap.diff (0.6 kB) - added by magneto on 08/08/08 17:24:14.
try/except around the 'open' step in close()
8175.charmless.diff (1.1 kB) - added by charmless on 08/14/08 15:26:02.
Don't close unopened files in delete.
8175-charmless.diff (1.1 kB) - added by charmless on 08/14/08 15:26:31.
Don't close unopened files in delete. (2nd attempt to upload)

Change History

08/08/08 17:24:14 changed by magneto

  • attachment close_open_trap.diff added.

try/except around the 'open' step in close()

08/12/08 12:17:48 changed by jacob

  • needs_better_patch changed.
  • component changed from Core framework to File uploads/storage.
  • needs_tests set to 1.
  • milestone set to 1.0.
  • needs_docs changed.
  • has_patch set to 1.
  • stage changed from Unreviewed to Accepted.

08/14/08 15:07:03 changed by charmless

I'm having a similar issue when deleting files: I've cleared out my uploads directory and now want to remove all the database entries that refer to them*. When I try to do that using the admin, however, it yields a 500 debug page complaining that the file is not open (traceback below).

It doesn't make much sense for delete to complain that the file it's deleting no longer exists. And, as the original reporter notes, 'close' should not call 'open'.

* Yes, this is backwards; I'm cleaning up the upload directories after testing several different ways of generating filenames in an upload_to callback, and just wanted to wipe them all out.

Traceback:
File "/Users/typing/software/python-packages/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/Users/typing/software/python-packages/django/contrib/admin/sites.py" in root
  172.                 return self.model_page(request, *url.split('/', 2))
File "/Users/typing/software/python-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/Users/typing/software/python-packages/django/contrib/admin/sites.py" in model_page
  189.         return admin_obj(request, rest_of_url)
File "/Users/typing/software/python-packages/django/contrib/admin/options.py" in __call__
  273.             return self.delete_view(request, unquote(url[:-7]))
File "/Users/typing/software/python-packages/django/contrib/admin/options.py" in delete_view
  750.             obj.delete()
File "/Users/typing/software/python-packages/django/db/models/base.py" in delete
  420.         delete_objects(seen_objs)
File "/Users/typing/software/python-packages/django/db/models/query.py" in delete_objects
  850.             signals.post_delete.send(sender=cls, instance=instance)
File "/Users/typing/software/python-packages/django/dispatch/dispatcher.py" in send
  132.             response = receiver(signal=self, sender=sender, **named)
File "/Users/typing/software/python-packages/django/db/models/fields/files.py" in delete_file
  204.                 file.delete(save=False)
File "/Users/typing/software/python-packages/django/db/models/fields/files.py" in delete
  276.         super(ImageFieldFile, self).delete(save)
File "/Users/typing/software/python-packages/django/db/models/fields/files.py" in delete
  83.         self.close()
File "/Users/typing/software/python-packages/django/core/files/base.py" in close
  147.         self.file.close()
File "/Users/typing/software/python-packages/django/db/models/fields/files.py" in _get_file
  45.             self._file = self.storage.open(self.name, 'rb')
File "/Users/typing/software/python-packages/django/core/files/storage.py" in open
  28.         file = self._open(name, mode)
File "/Users/typing/software/python-packages/django/core/files/storage.py" in _open
  128.         return File(open(self.path(name), mode))

Exception Type: IOError at /admin/images/image/5/delete/
Exception Value: [Errno 2] No such file or directory: u'/Users/typing/Desktop/spre/media-data/user/jim/images/Property Street Image-Banner.jpg'

08/14/08 15:26:02 changed by charmless

  • attachment 8175.charmless.diff added.

Don't close unopened files in delete.

08/14/08 15:26:31 changed by charmless

  • attachment 8175-charmless.diff added.

Don't close unopened files in delete. (2nd attempt to upload)

08/14/08 15:27:38 changed by charmless

I've uploaded a patch with a testcase, but I can't see it in the trac diff view. It's there in the "Original Format" link.

08/14/08 15:27:46 changed by charmless

  • needs_tests deleted.

08/17/08 07:08:34 changed by idangazit

We ran into this issue during the Django sprint in Tel Aviv. +1 for the 2nd charmless patch (the one with the testcase).

08/27/08 16:19:27 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

Fixed in [8637].


Add/Change #8175 (FileStorage-rf: Why does 'close' call 'open'?)




Change Properties
Action