Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

send_file uses 2x file's size in memory with conditional = True #361

Closed
qxcv opened this Issue · 3 comments

3 participants

@qxcv

https://github.com/mitsuhiko/flask/blob/master/flask/helpers.py#L423

Conditional responses (Response.make_conditional()) cause the iterator to be converted to a list (which stores the contents of the file in memory) and then ''.join() is used on the list (!!) which stores the contents of the file in memory AGAIN so Flask can use len() to determine content length. So for a short time it uses twice the item size in memory until the list conversion function returns the string from ''.join() and the original list is destroyed.

This isn't mentioned anywhere in the Flask docs, and can be avoided by calculating the file size and setting the Content-Length header using os.path.getsize() or fp.seek() and fp.tell() before rv.make_conditional is called.

@ThomasWaldmann

is this still true?

@grampajoe

If I'm reading it right, the changes made in mitsuhiko/werkzeug@e7f0ef4 mostly alleviate this by using a generator to add up the length of the file's lines instead of reading the whole file into a string at once.

@qxcv, do you think that addresses your issue?

@qxcv

Yes, that should fix the issue. I just tested with 0.10.1 and send_file no longer seems to cause an issue. Here is a quick test script I wrote to verify.

@qxcv qxcv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.