From 78af793338535af557bf104ba44ac2e2e746709e Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Mon, 3 Aug 2015 15:57:59 -0700 Subject: [PATCH] client: return context canceled error correctly If the body is closed to stop watching, it will ignore the error from reading body and return context error. Before this PR, the cancel when watching always returns error `read tcp 127.0.0.1:57824: use of closed network connection`. After this PR, it will return expected context canceled error. --- client/client.go | 6 ++---- client/client_test.go | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/client/client.go b/client/client.go index 116a1a4b2..48aa0b1dd 100644 --- a/client/client.go +++ b/client/client.go @@ -349,11 +349,9 @@ func (c *simpleHTTPClient) Do(ctx context.Context, act httpAction) (*http.Respon select { case <-ctx.Done(): - err = resp.Body.Close() + resp.Body.Close() <-done - if err == nil { - err = ctx.Err() - } + return nil, nil, ctx.Err() case <-done: } diff --git a/client/client_test.go b/client/client_test.go index ecd6f56d0..c291aa971 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -257,8 +257,8 @@ func TestSimpleHTTPClientDoCancelContextResponseBodyClosedWithBlockingBody(t *te }() _, _, err := c.Do(ctx, &fakeAction{}) - if err == nil { - t.Fatalf("expected non-nil error, got nil") + if err != context.Canceled { + t.Fatalf("expected %+v, got %+v", context.Canceled, err) } if !body.closed {