See https://discourse.phabricator-community.org/t/files-created-from-repository-contents-slightly-over-one-chunk-in-size-are-truncated-to-exactly-one-chunk-in-size/988/1. Three issues here:
- When we finish reading git cat-file ... or whatever, we can end up with more than one chunk worth of bytes left in the internal buffer if the read is fast. Use while instead of if to make sure we write the whole buffer.
- Limiting output with setStdoutSizeLimit() isn't really a reliable way to limit the size if we're also reading from the buffer. It's also pretty indirect and confusing. Instead, just let the FileUploadSource explicitly implement a byte limit in a straightforward way.
- We weren't setting the time limit correctly on the main path.
Overall, this could cause >4MB files to "write" as 4MB files, with the rest of the file left in the UploadSource buffer. Since these files were technically under the limit, they could return as valid. This was intermittent.