Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-go): classify proxy failures before request send
JasonBuildAI3e7ea7474a9b
diff --git a/sdks/sandbox/go/http.go b/sdks/sandbox/go/http.goindex dbf179924..c716e62a0 100644--- a/sdks/sandbox/go/http.go+++ b/sdks/sandbox/go/http.go@@ -365,8 +365,19 @@ func (c *Client) doRequestOnce(ctx context.Context, method, path string, body an}var sent atomic.Bool+ var traceStarted atomic.Bool+ var gotConn atomic.Boolvar tlsHandshakeFailed atomic.Booltrace := &httptrace.ClientTrace{+ GetConn: func(string) {+ traceStarted.Store(true)+ },+ GotConn: func(info httptrace.GotConnInfo) {+ gotConn.Store(true)+ if reused != nil {+ *reused = info.Reused+ }+ },WroteHeaders: func() {sent.Store(true)},@@ -379,11 +390,6 @@ func (c *Client) doRequestOnce(ctx context.Context, method, path string, body an}},}- if reused != nil {- trace.GotConn = func(info httptrace.GotConnInfo) {- *reused = info.Reused- }- }ctx = httptrace.WithClientTrace(ctx, trace)req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader)@@ -407,7 +413,7 @@ func (c *Client) doRequestOnce(ctx context.Context, method, path string, body anif err != nil {if sent.Load() {err = &requestSentError{err: err}- } else if tlsHandshakeFailed.Load() {+ } else if (traceStarted.Load() && !gotConn.Load()) || tlsHandshakeFailed.Load() {err = &preSendError{err: err}}return fmt.Errorf("opensandbox: do request: %w", err)diff --git a/sdks/sandbox/go/retry_non_idempotent_test.go b/sdks/sandbox/go/retry_non_idempotent_test.goindex 3be8533f1..7679b3c7c 100644--- a/sdks/sandbox/go/retry_non_idempotent_test.go+++ b/sdks/sandbox/go/retry_non_idempotent_test.go@@ -15,12 +15,14 @@package opensandboximport (+ "bufio""context""fmt""io""net""net/http""net/http/httptest"+ "net/url""strings""sync/atomic""testing"@@ -170,6 +172,35 @@ func TestRetry_NonIdempotentTLSHandshakeFailureRetried(t *testing.T) {require.Equal(t, int32(2), accepts.Load(), "a failed TLS handshake is a pre-send failure")}+func TestRetry_NonIdempotentProxyConnectFailureRetried(t *testing.T) {+ ln, err := net.Listen("tcp", "127.0.0.1:0")+ require.NoError(t, err)+ defer ln.Close()++ var accepts atomic.Int32+ go func() {+ for {+ conn, err := ln.Accept()+ if err != nil {+ return+ }+ accepts.Add(1)+ _, _ = http.ReadRequest(bufio.NewReader(conn))+ _, _ = conn.Write([]byte("HTTP/1.1 502 Bad Gateway\r\nContent-Length: 0\r\n\r\n"))+ _ = conn.Close()+ }+ }()++ proxyURL := &url.URL{Scheme: "http", Host: ln.Addr().String()}+ client := NewClient("https://sandbox.invalid", "token", "OPEN-SANDBOX-API-KEY",+ WithHTTPClient(&http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}),+ WithRetry(RetryConfig{MaxRetries: 1, InitialBackoff: time.Millisecond}),+ )+ err = client.doRequest(context.Background(), http.MethodPost, "/", map[string]string{"a": "b"}, nil)+ require.Error(t, err)+ require.Equal(t, int32(2), accepts.Load(), "a failed proxy CONNECT is a pre-send failure")+}+type retryRoundTripFunc func(*http.Request) (*http.Response, error)func (f retryRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {diff --git a/sdks/sandbox/go/retry_test.go b/sdks/sandbox/go/retry_test.goindex 050c800e5..ce0f790cf 100644--- a/sdks/sandbox/go/retry_test.go+++ b/sdks/sandbox/go/retry_test.go@@ -274,7 +274,7 @@ func TestRetry_RateLimit429(t *testing.T) {}}-func TestRetry_StreamingConnectionNotRetried(t *testing.T) {+func TestRetry_StreamingWriteStatusOptInNotRetried(t *testing.T) {var attempts atomic.Int32srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {