From 7b58dcb28cda0fa275b6b86b811cecf642974fce Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 4 May 2020 14:33:49 -0700 Subject: [PATCH] fix: return context error from context reader (#9507) --- cmd/bucket-handlers.go | 2 +- cmd/object-api-errors.go | 3 +++ cmd/object-api-utils.go | 14 ++++++++------ cmd/object-handlers.go | 4 ++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cmd/bucket-handlers.go b/cmd/bucket-handlers.go index 2a3c12189..499820e59 100644 --- a/cmd/bucket-handlers.go +++ b/cmd/bucket-handlers.go @@ -628,7 +628,7 @@ func (api objectAPIHandlers) PostPolicyBucketHandler(w http.ResponseWriter, r *h bucket := mux.Vars(r)["bucket"] // To detect if the client has disconnected. - r.Body = &detectDisconnect{r.Body, r.Context().Done()} + r.Body = &contextReader{r.Body, r.Context()} // Require Content-Length to be set in the request size := r.ContentLength diff --git a/cmd/object-api-errors.go b/cmd/object-api-errors.go index f8f9aed34..7487cb929 100644 --- a/cmd/object-api-errors.go +++ b/cmd/object-api-errors.go @@ -17,6 +17,7 @@ package cmd import ( + "context" "errors" "fmt" "io" @@ -106,6 +107,8 @@ func toObjectErr(err error, params ...string) error { err = InsufficientWriteQuorum{} case io.ErrUnexpectedEOF, io.ErrShortWrite: err = IncompleteBody{} + case context.Canceled, context.DeadlineExceeded: + err = IncompleteBody{} } return err } diff --git a/cmd/object-api-utils.go b/cmd/object-api-utils.go index ebfaf33f2..db3b845cd 100644 --- a/cmd/object-api-utils.go +++ b/cmd/object-api-utils.go @@ -18,6 +18,7 @@ package cmd import ( "bytes" + "context" "encoding/hex" "fmt" "io" @@ -842,16 +843,17 @@ func newS2CompressReader(r io.Reader) io.ReadCloser { return pr } -// Returns error if the cancelCh has been closed (indicating that S3 client has disconnected) -type detectDisconnect struct { +// Returns error if the context is canceled, indicating +// either client has disconnected +type contextReader struct { io.ReadCloser - cancelCh <-chan struct{} + ctx context.Context } -func (d *detectDisconnect) Read(p []byte) (int, error) { +func (d *contextReader) Read(p []byte) (int, error) { select { - case <-d.cancelCh: - return 0, io.ErrUnexpectedEOF + case <-d.ctx.Done(): + return 0, d.ctx.Err() default: return d.ReadCloser.Read(p) } diff --git a/cmd/object-handlers.go b/cmd/object-handlers.go index 2fad07d50..cb2cb6c4f 100644 --- a/cmd/object-handlers.go +++ b/cmd/object-handlers.go @@ -1209,7 +1209,7 @@ func (api objectAPIHandlers) PutObjectHandler(w http.ResponseWriter, r *http.Req } // To detect if the client has disconnected. - r.Body = &detectDisconnect{r.Body, r.Context().Done()} + r.Body = &contextReader{r.Body, r.Context()} // X-Amz-Copy-Source shouldn't be set for this call. if _, ok := r.Header[xhttp.AmzCopySource]; ok { @@ -1958,7 +1958,7 @@ func (api objectAPIHandlers) PutObjectPartHandler(w http.ResponseWriter, r *http } // To detect if the client has disconnected. - r.Body = &detectDisconnect{r.Body, r.Context().Done()} + r.Body = &contextReader{r.Body, r.Context()} // X-Amz-Copy-Source shouldn't be set for this call. if _, ok := r.Header[xhttp.AmzCopySource]; ok {