#13750 closed Bug (fixed)
ImageField accessing height or width and then data results in "I/O operation on closed file".
Reported by: | ROsborne | Owned by: | Hasan Ramezani |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Aksel | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
If you have a simple model with an ImageField, the following code will fail with a "I/O operation on closed file":
instance = MyClass.objects.get(...) w = instance.image.width h = instance.image.height original = Image.open(instance.image)
The work around is to reopen the file:
instance = MyClass.objects.get(...) w = instance.image.width h = instance.image.height instance.image.open() original = Image.open(instance.image)
Note this is different than the issue of the 2nd read() returning empty strings for two reasons:
- You can not seek(0), the file is closed.
- Needing to reopen in this case is unexpected.
Attachments (7)
Change History (31)
comment:1 by , 15 years ago
Description: | modified (diff) |
---|
comment:2 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:7 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
by , 10 years ago
Attachment: | regression_13750.diff added |
---|
Test to replicate the behaviour described in the ticket
by , 10 years ago
Attachment: | 13750_simple_approach_solution.diff added |
---|
Simple approach to solve #13750
comment:8 by , 10 years ago
Would it be a problem to simply open image files (specifically ImageFieldFile objects) in the _get_ function of the FileDescriptor object? Please see the "simple approach" attachment with a potential diff.
Currently, it fixes the new regression test but approximately 31 other tests fail because they test for the "open-ness" of a file. However, these failing tests could be adapted if people agree to this solution. (See 13750_tests.txt)
I'm happy to hear people's suggestions or other feedback.
comment:9 by , 10 years ago
Cc: | added |
---|
follow-up: 12 comment:10 by , 10 years ago
How would the tests be adapted? Where is the file closed in the first place?
comment:11 by , 10 years ago
I've updated the patch code and I've added the updated test results.
Some tests are now failing because they expect the imagefile to be closed (e.g., tests/modelfields/test_imagefield.py line 165, after getting the size of an image).
I'm assuming that, given the description in this ticket, we want to retrieval of the image (and not an image property) to leave the file open. Is that a correct assumption? And if so, isn't that rather inconsistent behaviour?
comment:12 by , 10 years ago
Replying to timgraham:
How would the tests be adapted? Where is the file closed in the first place?
Hi Tim, I've updated my proposed solution code and test results as attachments. I also left some new questions. Lastly, the file get's closed during saving.
comment:13 by , 10 years ago
Has patch: | set |
---|
comment:14 by , 10 years ago
Patch needs improvement: | set |
---|
I don't see a compelling reason to automatically open the file. That change would be backwards incompatible for anyone who is accessing other attributes but doesn't care about the image (they would now have to close it). Can we simply document the behavior in docs/topics/files.txt?
comment:15 by , 10 years ago
Yeah, while I understand the issue of the ticket reporter, keeping things as they are makes more sense to me because the behaviour would remain consistent. As a next step, I could adapt the regression test to test for an image to be closed and include an entry in docs/topics/files.txt. Unless, of course, someone else does have good reasons to open the file.
by , 10 years ago
Attachment: | 13750.diff added |
---|
Documentation for behavior and updated regression test
comment:16 by , 10 years ago
I updated the regression test to check the behaviour that accessing an image field of a model returns a closed file-like reference. Furthermore, I updated the documentation in docs/topics/files.txt. Changes have an accompanying PR to master. Looking forward to the feedback.
comment:17 by , 10 years ago
Patch needs improvement: | unset |
---|
I took another look at this issue and found some other interesting stuff. I noticed that, when not accessing the width or height property, the call to Image.open(model.image) does pass successfully. I may have been looking at the wrong thing.
I dug a little deeper and I found that in django/core/files/images.py the function get_dimensions() has a "close" keyword argument. This keyword argument comes from a call to self.closed in the ImageFile class (a few lines above). In the "get_image_dimensions" function, however, the "close" keyword argument determines whether a file should be closed at the end of the function. I think that was not correct, because if self.closed returns True, the keyword argument to the function is "close=True" and there is a call to file.close() at the end of the function get_image_dimensions.
As a solution, I removed the "close" keyword argument in "get_image_dimensions" and added a variable "close" that is either True or False depending on whether in the beginning of the function the file is explicitly opened.
I also re-added the regression test that caused the "I/O operation on closed file" error. With the new patch this test now passes and so do all the other tests. I updated my PR to the repo and removed the documentation bit of my previous patch.
follow-up: 19 comment:18 by , 10 years ago
Patch needs improvement: | set |
---|
Still not sure the patch is ideal as noted on the pull request. By the way, there's no need to attach the patch to the ticket if you send a pull request.
comment:19 by , 10 years ago
Replying to timgraham:
Still not sure the patch is ideal as noted on the pull request. By the way, there's no need to attach the patch to the ticket if you send a pull request.
Hey Tim,
thanks for the feedback, much appreciated. I'm a bit confused though:
"Can you make this work adding self.assertTrue(profile.image.closed) after this line? I wouldn't expect accessing the image dimensions to leave the file opened."
Isn't that contradicting with the original complaint of the ticket? If I close the file after accessing the height or image the regression test will fail because a closed file will throw the IO error when passed to an Image.open().
I'm not sure which direction to take to resolve this issue correctly.
comment:21 by , 5 years ago
Component: | File uploads/storage → Documentation |
---|---|
Owner: | changed from | to
Patch needs improvement: | unset |
Summary: | ImageField accessing height or width and then data results in "I/O operation on closed file" → ImageField accessing height or width and then data results in "I/O operation on closed file". |
Version: | 1.2 → master |
comment:22 by , 5 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Reformatted description. Next time please use preview and consult WikiFormatting.