|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package proxy |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "crypto/tls" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "net/url" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | + "golang.org/x/net/http2" |
| 21 | + |
| 22 | + "github.com/e2b-dev/infra/packages/shared/pkg/connlimit" |
| 23 | + "github.com/e2b-dev/infra/packages/shared/pkg/logger" |
| 24 | + reverseproxy "github.com/e2b-dev/infra/packages/shared/pkg/proxy" |
| 25 | + "github.com/e2b-dev/infra/packages/shared/pkg/proxy/pool" |
| 26 | +) |
| 27 | + |
| 28 | +func TestSandboxProxyCloseInterruptsStalledH2CUpload(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + |
| 31 | + for _, forced := range []bool{false, true} { |
| 32 | + t.Run(fmt.Sprintf("forced=%t", forced), func(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + |
| 35 | + backendStarted := make(chan error, 1) |
| 36 | + backend := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 37 | + _, err := io.ReadFull(r.Body, make([]byte, 1)) |
| 38 | + backendStarted <- err |
| 39 | + _, _ = io.Copy(io.Discard, r.Body) |
| 40 | + })) |
| 41 | + t.Cleanup(backend.Close) |
| 42 | + backendURL, err := url.Parse(backend.URL) |
| 43 | + require.NoError(t, err) |
| 44 | + |
| 45 | + handlerDone := make(chan struct{}) |
| 46 | + proxy := &SandboxProxy{proxy: reverseproxy.New(0, reverseproxy.SandboxProxyRetries, time.Minute, |
| 47 | + func(*http.Request) (*pool.Destination, error) { |
| 48 | + return &pool.Destination{ |
| 49 | + Url: backendURL, |
| 50 | + ConnectionKey: "upload-lifecycle", |
| 51 | + RequestLogger: logger.NewNopLogger(), |
| 52 | + }, nil |
| 53 | + }, &reverseproxy.ConnectionLimitConfig{ |
| 54 | + Limiter: connlimit.NewConnectionLimiter(), |
| 55 | + GetMaxLimit: func(context.Context) int { return 10 }, |
| 56 | + OnConnectionReleased: func(context.Context, int64) { close(handlerDone) }, |
| 57 | + }, true)} |
| 58 | + var listenConfig net.ListenConfig |
| 59 | + listener, err := listenConfig.Listen(t.Context(), "tcp", "127.0.0.1:0") |
| 60 | + require.NoError(t, err) |
| 61 | + serveDone := make(chan error, 1) |
| 62 | + go func() { serveDone <- proxy.proxy.Serve(listener) }() |
| 63 | + t.Cleanup(func() { |
| 64 | + assert.NoError(t, proxy.proxy.Close()) |
| 65 | + assert.ErrorIs(t, <-serveDone, http.ErrServerClosed) |
| 66 | + }) |
| 67 | + |
| 68 | + client := &http.Client{Transport: &http2.Transport{ |
| 69 | + AllowHTTP: true, |
| 70 | + DialTLSContext: func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) { |
| 71 | + return (&net.Dialer{}).DialContext(ctx, network, addr) |
| 72 | + }, |
| 73 | + }} |
| 74 | + t.Cleanup(client.CloseIdleConnections) |
| 75 | + reader, writer := io.Pipe() |
| 76 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "http://"+listener.Addr().String()+"/upload", reader) |
| 77 | + require.NoError(t, err) |
| 78 | + req.ContentLength = 8193 |
| 79 | + clientDone := make(chan struct{}) |
| 80 | + go func() { |
| 81 | + defer close(clientDone) |
| 82 | + resp, err := client.Do(req) |
| 83 | + if err == nil { |
| 84 | + _ = resp.Body.Close() |
| 85 | + } |
| 86 | + }() |
| 87 | + t.Cleanup(func() { |
| 88 | + _ = writer.Close() |
| 89 | + _ = reader.Close() |
| 90 | + select { |
| 91 | + case <-clientDone: |
| 92 | + case <-time.After(5 * time.Second): |
| 93 | + t.Error("upload client did not exit") |
| 94 | + } |
| 95 | + }) |
| 96 | + _, err = io.WriteString(writer, strings.Repeat("x", 8192)) |
| 97 | + require.NoError(t, err) |
| 98 | + select { |
| 99 | + case err := <-backendStarted: |
| 100 | + require.NoError(t, err) |
| 101 | + case <-time.After(5 * time.Second): |
| 102 | + t.Fatal("backend did not receive the upload") |
| 103 | + } |
| 104 | + backend.CloseClientConnections() |
| 105 | + |
| 106 | + ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond) |
| 107 | + defer cancel() |
| 108 | + if forced { |
| 109 | + cancel() |
| 110 | + } |
| 111 | + require.NoError(t, proxy.Close(ctx)) |
| 112 | + select { |
| 113 | + case <-handlerDone: |
| 114 | + case <-time.After(5 * time.Second): |
| 115 | + t.Fatal("shutdown left the upload handler blocked") |
| 116 | + } |
| 117 | + assert.Zero(t, proxy.proxy.CurrentServerConnections()) |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments