Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
net/http: multipart ReadForm fails to remove temporary files on io.Copy error #16296
I upload a rather large file to a http.Server as a value of a form in a http POST request. The server is a basic http.Server, set with its Server.ReadTimeout and Server.WriteTimeout at one second, i.e. way too short to handle the request. I am trying to get the file by calling
The function fails with a timeout error, which is perfectly expected, but it leaves in the temporary folder multipart-xxxxxx files. I think I have traced it back to mime\multipart\formdata.go, l 74:
We notice that in this case, if io.Copy fails (which is what happens with my short timeouts) we are going to call os.Remove BEFORE calling file.Close, leading to a os.Remove failure. I confirmed this by adding some logs :
and this does confirm the error :
First line is the expected timeout error
Fix is basic, just manually close the file before os.Remove and move the defer file.Close() (or just call it manually a second time) below this first error management but I'd love to see this fixed for go 1.7. Given the time frame you may fix it faster on your side than if it gets processed as a first-time submission, but if you'd rather have me submit a patch I'll look into the process early next week.
changed the title
multipart ReadForm fails to remove temporary files on io.Copy error
Jul 8, 2016
It's been like this for the past 7 releases, since Go 1.0 (https://github.com/golang/go/blob/release-branch.go1/src/pkg/mime/multipart/formdata.go#L78) so I think there's no rush doing this in Go 1.7.
It also only affects Windows, and leaving crap in $TMP should be fine in general: the operating system should be cleaning TMP regularly or as needed.
But we can fix it for Go 1.8.