lib/rest: fix multipart uploads stopping on context cancel

Before this change when the context was cancelled (due to
--max-duration for example) this could deadlock when uploading
multipart uploads.

This change fixes the problem by introducing another go routine to
monitor the context and close the pipe with an error when the context
errors.
This commit is contained in:
Nick Craig-Wood 2021-03-25 15:35:08 +00:00
parent 8c5c91e68f
commit fc57648b75
5 changed files with 21 additions and 7 deletions

View file

@ -450,6 +450,7 @@ func TestRcFsInfo(t *testing.T) {
func TestUploadFile(t *testing.T) {
r, call := rcNewRun(t, "operations/uploadfile")
defer r.Finalise()
ctx := context.Background()
testFileName := "test.txt"
testFileContent := "Hello World"
@ -460,7 +461,7 @@ func TestUploadFile(t *testing.T) {
currentFile, err := os.Open(path.Join(r.LocalName, testFileName))
require.NoError(t, err)
formReader, contentType, _, err := rest.MultipartUpload(currentFile, url.Values{}, "file", testFileName)
formReader, contentType, _, err := rest.MultipartUpload(ctx, currentFile, url.Values{}, "file", testFileName)
require.NoError(t, err)
httpReq := httptest.NewRequest("POST", "/", formReader)
@ -482,7 +483,7 @@ func TestUploadFile(t *testing.T) {
currentFile, err = os.Open(path.Join(r.LocalName, testFileName))
require.NoError(t, err)
formReader, contentType, _, err = rest.MultipartUpload(currentFile, url.Values{}, "file", testFileName)
formReader, contentType, _, err = rest.MultipartUpload(ctx, currentFile, url.Values{}, "file", testFileName)
require.NoError(t, err)
httpReq = httptest.NewRequest("POST", "/", formReader)