Skip to content

fix: client prompt return on context cancellation #1729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"context"
"errors"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -132,36 +131,26 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
req = req.WithContext(ctx)
}
resp, err := c.client.Do(req)
defer func() {
if resp != nil {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}
}()

if err != nil {
return nil, nil, err
}

var body []byte
done := make(chan struct{})
done := make(chan error, 1)
go func() {
var buf bytes.Buffer
// TODO(bwplotka): Add LimitReader for too long err messages (e.g. limit by 1KB)
_, err = buf.ReadFrom(resp.Body)
_, err := buf.ReadFrom(resp.Body)
body = buf.Bytes()
close(done)
done <- err
}()

select {
case <-ctx.Done():
resp.Body.Close()
<-done
err = resp.Body.Close()
if err == nil {
err = ctx.Err()
}
case <-done:
return resp, nil, ctx.Err()
case err = <-done:
resp.Body.Close()
return resp, body, err
}

return resp, body, err
}
48 changes: 48 additions & 0 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package api
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)

func TestConfig(t *testing.T) {
Expand Down Expand Up @@ -116,6 +118,52 @@ func TestClientURL(t *testing.T) {
}
}

func TestDoContextCancellation(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("partial"))
if f, ok := w.(http.Flusher); ok {
f.Flush()
}

<-r.Context().Done()
}))

defer ts.Close()

client, err := NewClient(Config{
Address: ts.URL,
})
if err != nil {
t.Fatalf("failed to create client: %v", err)
}

req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()

start := time.Now()
resp, body, err := client.Do(ctx, req)
elapsed := time.Since(start)

if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("expected error %v, got: %v", context.DeadlineExceeded, err)
}
if body != nil {
t.Errorf("expected no body due to cancellation, got: %q", string(body))
}
if elapsed > 200*time.Millisecond {
t.Errorf("Do did not return promptly on cancellation: took %v", elapsed)
}

if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}

// Serve any http request with a response of N KB of spaces.
type serveSpaces struct {
sizeKB int
Expand Down