Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added file object to '_closable_objects' to close at end of request #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chugcup
Copy link

@chugcup chugcup commented Aug 20, 2019

This commit fixes ResourceWarnings in Python 3 where file objects do not appear to be closed at the end of the request, and the Django server will emit ResourceWarnings about unclosed files.

The _closable_objects list is managed by the base FileResponse class in Django and will call .close() on objects at the end of the request.

Issue number

N/A

Expected behaviour

File object would be closed at the end of the request.

Actual behaviour

File object remains open.

Description of fix

The FileResponse object maintains a list of objects to be closed at the end of the request. Since this class uses a special reader class the file object is not appended to. This will add the file object when the instance is created. Checks to ensure file object has a close() method, and that the FileResponse class has the _closable_object property (for compatibility).

Other info

Was causing intermittent warnings/crashes in the Django runserver when --autoreload used.

This commit fixes ResourceWarnings in Python 3 where file objects
do not appear to be closed at the end of the request, and the
Django server will emit ResourceWarnings about unclosed files.
The `_closable_objects` list is managed by the base FileResponse
class in Django and will call `.close()` on objects at the end
of the request.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant