mitsuhiko/flask — Issue #361: send_file uses 2x file's size in memory with conditional = True
Author: qxcv
Created: 2011-12-13 00:49:45+00:00
State: open

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.