Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#8175 closed (fixed)

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

Reported by: magneto Owned by: nobody
Component: File uploads/storage Version: master
Severity: Keywords: filefield fs-rf
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 (3)

close_open_trap.diff (580 bytes) - added by magneto 6 years ago.
try/except around the 'open' step in close()
8175.charmless.diff (1.1 KB) - added by charmless 6 years ago.
Don't close unopened files in delete.
8175-charmless.diff (1.1 KB) - added by charmless 6 years ago.
Don't close unopened files in delete. (2nd attempt to upload)

Download all attachments as: .zip

Change History (10)

Changed 6 years ago by magneto

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

comment:1 Changed 6 years ago by jacob

  • Component changed from Core framework to File uploads/storage
  • Has patch set
  • milestone set to 1.0
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago 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'

Changed 6 years ago by charmless

Don't close unopened files in delete.

Changed 6 years ago by charmless

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

comment:3 Changed 6 years ago 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.

comment:4 Changed 6 years ago by charmless

  • Needs tests unset

comment:5 Changed 6 years ago 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).

comment:6 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

Fixed in [8637].

comment:7 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.