From b68935894aab420db8860e4530ab472a5614fba3 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Mon, 23 Nov 2026 23:51:36 +0000 Subject: [PATCH 1/3] fix: bound Thrift CloudFetch downloads with ClientTimeout Bound each Thrift CloudFetch transfer attempt with the existing 900s ClientTimeout so stalled downloads cannot wait indefinitely. Preserve default-client settings and use the final WithTransport option for downloads. Omit the link's presigned query string from download errors. Addresses #475 (part 2) Signed-off-by: Vu Anh Phung Co-authored-by: Isaac --- CHANGELOG.md | 6 + cloudfetch_benchmark_test.go | 56 ++++++ connector.go | 9 +- connector_cloudfetch_timeout_test.go | 152 ++++++++++++++ connector_test.go | 2 +- internal/rows/arrowbased/batchloader.go | 46 ++++- internal/rows/arrowbased/batchloader_test.go | 5 +- .../arrowbased/cloudfetch_timeout_test.go | 190 ++++++++++++++++++ 8 files changed, 451 insertions(+), 15 deletions(-) create mode 100644 cloudfetch_benchmark_test.go create mode 100644 connector_cloudfetch_timeout_test.go create mode 100644 internal/rows/arrowbased/cloudfetch_timeout_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index c2e0757c..2643eb0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release History +## Unreleased + +### Driver changes + +- **Behavior change:** Bound each whole-transfer Thrift CloudFetch download attempt, including `WithTransport`, by the existing 900s `ClientTimeout`, and omit the link's presigned query string from download errors (databricks/databricks-sql-go#475). + ## v2.0.0 (2026-11-23) ### Breaking changes diff --git a/cloudfetch_benchmark_test.go b/cloudfetch_benchmark_test.go new file mode 100644 index 00000000..94f7d9ed --- /dev/null +++ b/cloudfetch_benchmark_test.go @@ -0,0 +1,56 @@ +package dbsql + +import ( + "bytes" + "context" + "io" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/databricks/databricks-sql-go/v2/internal/cli_service" + "github.com/databricks/databricks-sql-go/v2/internal/rows/arrowbased" +) + +func BenchmarkCloudFetchDownload(b *testing.B) { + payload := bytes.Repeat([]byte("cloudfetch"), 512*1024) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write(payload) + })) + defer server.Close() + links := []*cli_service.TSparkArrowResultLink{{FileLink: server.URL, ExpiryTime: time.Now().Add(time.Hour).Unix(), RowCount: 1}} + for _, transport := range []string{"default", "WithTransport"} { + b.Run(transport, func(b *testing.B) { + var options []ConnOption + if transport == "WithTransport" { + options = append(options, WithTransport(http.DefaultTransport)) + } + con, err := NewConnector(options...) + if err != nil { + b.Fatal(err) + } + cfg := con.(*connector).cfg + cfg.UseLz4Compression = false + b.ReportAllocs() + b.SetBytes(int64(len(payload))) + b.ResetTimer() + for range b.N { + iterator, err := arrowbased.NewCloudIPCStreamIterator(context.Background(), links, 0, cfg, nil) + if err != nil { + b.Fatal(err) + } + stream, nextErr := iterator.Next() + if nextErr != nil { + iterator.Close() + b.Fatal(nextErr) + } + _, readErr := io.Copy(io.Discard, stream) + iterator.Close() + if readErr != nil { + b.Fatal(readErr) + } + } + }) + } +} diff --git a/connector.go b/connector.go index 9bd30c69..abc1bd9d 100644 --- a/connector.go +++ b/connector.go @@ -171,6 +171,9 @@ func NewConnector(options ...ConnOption) (driver.Connector, error) { for _, opt := range options { opt(cfg) } + if cfg.HTTPClient == nil && cfg.Transport != nil { + cfg.HTTPClient = &http.Client{Transport: cfg.Transport, Timeout: cfg.ClientTimeout} + } // Covers the DSN path too, which applies ParseDSN's UserConfig as an option. if cfg.MaxDownloadThreads <= 0 { logger.Warn().Msgf("databricks: maxDownloadThreads (WithMaxDownloadThreads) value %d is not positive; using the default %d", @@ -531,12 +534,6 @@ func WithAuthenticator(authr auth.Authenticator) ConnOption { func WithTransport(t http.RoundTripper) ConnOption { return func(c *config.Config) { c.Transport = t - - if c.HTTPClient == nil { - c.HTTPClient = &http.Client{ - Transport: t, - } - } } } diff --git a/connector_cloudfetch_timeout_test.go b/connector_cloudfetch_timeout_test.go new file mode 100644 index 00000000..a7c78ac9 --- /dev/null +++ b/connector_cloudfetch_timeout_test.go @@ -0,0 +1,152 @@ +package dbsql + +import ( + "context" + "database/sql/driver" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + dbsqlerr "github.com/databricks/databricks-sql-go/v2/errors" + "github.com/databricks/databricks-sql-go/v2/internal/cli_service" + "github.com/databricks/databricks-sql-go/v2/internal/client" + "github.com/databricks/databricks-sql-go/v2/internal/config" + dbsqlerrint "github.com/databricks/databricks-sql-go/v2/internal/errors" + driverrows "github.com/databricks/databricks-sql-go/v2/internal/rows" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type cloudFetchStallingTransport struct { + cancelled chan<- struct{} + release <-chan struct{} +} + +func (transport cloudFetchStallingTransport) RoundTrip(request *http.Request) (*http.Response, error) { + select { + case <-request.Context().Done(): + transport.cancelled <- struct{}{} + return nil, request.Context().Err() + case <-transport.release: + return nil, context.Canceled + } +} + +func TestCloudFetchClientTimeout(t *testing.T) { + for _, testCase := range []struct { + name string + stallBody bool + withTransport bool + }{ + {name: "default/headers"}, + {name: "default/body", stallBody: true}, + {name: "WithTransport/headers", withTransport: true}, + } { + t.Run(testCase.name, func(t *testing.T) { + release := make(chan struct{}) + cancelled := make(chan struct{}, 2) + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + if testCase.withTransport { + response.WriteHeader(http.StatusNoContent) + return + } + if testCase.stallBody { + response.Header().Set("Content-Length", "100") + response.WriteHeader(http.StatusOK) + response.(http.Flusher).Flush() + } + select { + case <-request.Context().Done(): + cancelled <- struct{}{} + case <-release: + } + })) + defer server.Close() + defer close(release) + + options := []ConnOption{func(cfg *config.Config) { + cfg.ClientTimeout = 100 * time.Millisecond + cfg.RetryMax = 0 + cfg.UseLz4Compression = false + cfg.MaxDownloadThreads = 2 + }} + if testCase.withTransport { + options = append(options, WithTransport(cloudFetchStallingTransport{cancelled: cancelled, release: release})) + } + con, err := NewConnector(options...) + require.NoError(t, err) + cfg := con.(*connector).cfg + fixture, err := os.ReadFile("internal/rows/testdata/directResultsMultipleFetch/ExecuteStatement.json") + require.NoError(t, err) + var execute cli_service.TExecuteStatementResp + require.NoError(t, json.Unmarshal(fixture, &execute)) + direct := execute.DirectResults + direct.ResultSet.Results.ArrowBatches = nil + expiry := time.Now().Add(time.Hour).Unix() + direct.ResultSet.Results.ResultLinks = []*cli_service.TSparkArrowResultLink{ + {FileLink: server.URL + "/file?sig=SECRET_SIGNATURE", ExpiryTime: expiry, RowCount: 1}, + {FileLink: server.URL + "/file?sig=SECRET_SIGNATURE", ExpiryTime: expiry, RowCount: 1, StartRowOffset: 1}, + } + format := cli_service.TSparkRowSetType_URL_BASED_SET + direct.ResultSetMetadata.ResultFormat = &format + testClient := &client.TestClient{ + FnCloseOperation: func(context.Context, *cli_service.TCloseOperationReq) (*cli_service.TCloseOperationResp, error) { + return &cli_service.TCloseOperationResp{}, nil + }, + } + rowSet, err := driverrows.NewRows(context.Background(), nil, testClient, cfg, direct, nil) + require.NoError(t, err) + defer rowSet.Close() //nolint:errcheck + + result := make(chan error, 1) + start := time.Now() + go func() { result <- rowSet.Next(make([]driver.Value, len(rowSet.Columns()))) }() + select { + case nextErr := <-result: + require.Error(t, nextErr) + assert.ErrorIs(t, nextErr, dbsqlerr.DriverError) + assert.ErrorIs(t, nextErr, context.DeadlineExceeded) + assert.Equal(t, dbsqlerrint.CategoryChunkDownload, dbsqlerrint.CategoryFromError(nextErr)) + assert.NotContains(t, nextErr.Error(), "SECRET_SIGNATURE") + assert.GreaterOrEqual(t, time.Since(start), cfg.ClientTimeout) + case <-time.After(cfg.ClientTimeout + 2*time.Second): + server.CloseClientConnections() + select { + case nextErr := <-result: + assert.Error(t, nextErr) + case <-time.After(2 * time.Second): + t.Fatal("CloudFetch read did not exit after connections closed") + } + t.Fatal("CloudFetch read did not honor ClientTimeout") + } + for range 2 { + select { + case <-cancelled: + case <-time.After(2 * time.Second): + t.Fatal("in-flight CloudFetch request was not cancelled") + } + } + }) + } +} + +func TestCloudFetchWithTransportClientTimeoutOptionOrder(t *testing.T) { + for _, timeout := range []time.Duration{0, 100 * time.Millisecond, 5 * time.Second} { + for _, order := range []string{"before", "after"} { + t.Run(timeout.String()+"/"+order, func(t *testing.T) { + options := []ConnOption{func(cfg *config.Config) { cfg.ClientTimeout = timeout }, WithTransport(http.DefaultTransport)} + if order == "after" { + options[0], options[1] = options[1], options[0] + } + con, err := NewConnector(options...) + require.NoError(t, err) + require.NotNil(t, con.(*connector).cfg.HTTPClient) + assert.Equal(t, timeout, con.(*connector).cfg.HTTPClient.Timeout) + assert.Same(t, http.DefaultTransport, con.(*connector).cfg.HTTPClient.Transport) + }) + } + } +} diff --git a/connector_test.go b/connector_test.go index c8e1847d..526d27e2 100644 --- a/connector_test.go +++ b/connector_test.go @@ -167,7 +167,7 @@ func TestNewConnector(t *testing.T) { MaxFilesInMemory: 10, MinTimeToExpiry: 0 * time.Second, CloudFetchSpeedThresholdMbps: 0.1, - HTTPClient: &http.Client{Transport: roundTripper}, + HTTPClient: &http.Client{Transport: roundTripper, Timeout: 900 * time.Second}, } expectedUserConfig := config.UserConfig{ Host: host, diff --git a/internal/rows/arrowbased/batchloader.go b/internal/rows/arrowbased/batchloader.go index 4c121a94..d8e7456b 100644 --- a/internal/rows/arrowbased/batchloader.go +++ b/internal/rows/arrowbased/batchloader.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "net/url" "strings" "time" @@ -59,9 +60,13 @@ func NewCloudIPCStreamIterator( cfg *config.Config, onFileDownloaded func(downloadMs int64), ) (IPCStreamIterator, dbsqlerr.DBError) { - httpClient := http.DefaultClient - if cfg.HTTPClient != nil { - httpClient = cfg.HTTPClient + httpClient := cfg.HTTPClient + if httpClient == nil { + client := *http.DefaultClient + if cfg.ClientTimeout > 0 && (client.Timeout <= 0 || client.Timeout > cfg.ClientTimeout) { + client.Timeout = cfg.ClientTimeout + } + httpClient = &client } bi := &cloudIPCStreamIterator{ @@ -399,6 +404,33 @@ func logCloudFetchSpeed(fullURL string, contentLength int64, duration time.Durat } } +func redactCloudFetchError(err error) error { + urlErr, ok := err.(*url.Error) + if !ok { + return err + } + redacted := *urlErr + redacted.URL, _, _ = strings.Cut(urlErr.URL, "?") + redacted.Err = redactCloudFetchError(urlErr.Err) + return &redacted +} + +type redactedCloudFetchError struct { + cause error + rawQuery string +} + +func (downloadErr redactedCloudFetchError) Error() string { + if downloadErr.rawQuery == "" { + return downloadErr.cause.Error() + } + return strings.ReplaceAll(downloadErr.cause.Error(), downloadErr.rawQuery, "[REDACTED]") +} + +func (downloadErr redactedCloudFetchError) Unwrap() error { + return downloadErr.cause +} + // fetchBatchBytes downloads a single CloudFetch result link and returns the // raw response body, still compressed if the server used LZ4. Connection-time // failures, retryable HTTP statuses, and mid-stream body read failures are @@ -421,6 +453,8 @@ func fetchBatchBytes( if retryMax < 0 { retryMax = 0 } + _, rawQuery, _ := strings.Cut(link.FileLink, "?") + rawQuery, _, _ = strings.Cut(rawQuery, "#") var ( lastErr error @@ -455,7 +489,7 @@ func fetchBatchBytes( req, err := http.NewRequestWithContext(ctx, "GET", link.FileLink, nil) if err != nil { - return nil, err + return nil, redactedCloudFetchError{cause: redactCloudFetchError(err), rawQuery: rawQuery} } if link.HttpHeaders != nil { for key, value := range link.HttpHeaders { @@ -471,7 +505,7 @@ func fetchBatchBytes( if ctx.Err() != nil { return nil, ctx.Err() } - lastErr = err + lastErr = redactedCloudFetchError{cause: redactCloudFetchError(err), rawQuery: rawQuery} lastStatus = 0 lastRetryAfter = "" continue @@ -486,7 +520,7 @@ func fetchBatchBytes( if ctx.Err() != nil { return nil, ctx.Err() } - lastErr = readErr + lastErr = redactedCloudFetchError{cause: redactCloudFetchError(readErr), rawQuery: rawQuery} lastStatus = 0 lastRetryAfter = "" continue diff --git a/internal/rows/arrowbased/batchloader_test.go b/internal/rows/arrowbased/batchloader_test.go index 4d35dd89..9ccdd6f5 100644 --- a/internal/rows/arrowbased/batchloader_test.go +++ b/internal/rows/arrowbased/batchloader_test.go @@ -328,7 +328,7 @@ func TestCloudFetchIterator(t *testing.T) { assert.NotNil(t, sab) }) - t.Run("should fallback to http.DefaultClient when HTTPClient is nil", func(t *testing.T) { + t.Run("should use a bounded default client when HTTPClient is nil", func(t *testing.T) { handler = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write(generateMockArrowBytes(generateArrowRecord())) @@ -360,7 +360,8 @@ func TestCloudFetchIterator(t *testing.T) { assert.Nil(t, err) cbi := bi.(*batchIterator).ipcIterator.(*cloudIPCStreamIterator) - assert.Equal(t, http.DefaultClient, cbi.httpClient) + assert.NotSame(t, http.DefaultClient, cbi.httpClient) + assert.Equal(t, cfg.ClientTimeout, cbi.httpClient.Timeout) // Verify fetch works with default client sab, nextErr := bi.Next() diff --git a/internal/rows/arrowbased/cloudfetch_timeout_test.go b/internal/rows/arrowbased/cloudfetch_timeout_test.go new file mode 100644 index 00000000..92bc7ef0 --- /dev/null +++ b/internal/rows/arrowbased/cloudfetch_timeout_test.go @@ -0,0 +1,190 @@ +package arrowbased + +import ( + "context" + "net/http" + "net/http/cookiejar" + "net/http/httptest" + "net/url" + "sync/atomic" + "testing" + "time" + + "github.com/databricks/databricks-sql-go/v2/internal/cli_service" + "github.com/databricks/databricks-sql-go/v2/internal/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCloudFetchDefaultClientTransportHonored(t *testing.T) { + payload := generateMockArrowBytes(generateArrowRecord()) + server := httptest.NewTLSServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + if request.URL.Path == "/redirect" { + http.Redirect(response, request, "/file", http.StatusFound) + return + } + cookie, err := request.Cookie("cloudfetch-test") + if err != nil || cookie.Value != "preserved" || request.Header.Get("X-Redirect-Policy") != "preserved" { + response.WriteHeader(http.StatusUnauthorized) + return + } + _, _ = response.Write(payload) + })) + defer server.Close() + customClient := server.Client() + jar, err := cookiejar.New(nil) + require.NoError(t, err) + serverURL, err := url.Parse(server.URL) + require.NoError(t, err) + jar.SetCookies(serverURL, []*http.Cookie{{Name: "cloudfetch-test", Value: "preserved", Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode}}) + customClient.Jar = jar + customClient.CheckRedirect = func(request *http.Request, previous []*http.Request) error { + request.Header.Set("X-Redirect-Policy", "preserved") + return nil + } + originalClient := http.DefaultClient + http.DefaultClient = customClient + defer func() { http.DefaultClient = originalClient }() + cfg := config.WithDefaults() + cfg.UseLz4Compression = false + cfg.RetryMax = 0 + iterator, driverErr := NewCloudBatchIterator(context.Background(), []*cli_service.TSparkArrowResultLink{{ + FileLink: server.URL + "/redirect", ExpiryTime: time.Now().Add(time.Hour).Unix(), RowCount: 6, + }}, 0, nil, cfg, nil) + require.NoError(t, driverErr) + defer iterator.Close() + records, nextErr := iterator.Next() + require.NoError(t, nextErr) + records.Close() + assert.Zero(t, customClient.Timeout) +} + +func TestCloudFetchDefaultClientTimeoutCap(t *testing.T) { + for _, timeout := range []time.Duration{0, 100 * time.Millisecond, 5 * time.Second} { + t.Run(timeout.String(), func(t *testing.T) { + customClient := *http.DefaultClient + customClient.Timeout = timeout + originalClient := http.DefaultClient + http.DefaultClient = &customClient + defer func() { http.DefaultClient = originalClient }() + cfg := config.WithDefaults() + cfg.ClientTimeout = time.Second + iterator, err := NewCloudIPCStreamIterator(context.Background(), nil, 0, cfg, nil) + require.NoError(t, err) + defer iterator.Close() + boundedClient := iterator.(*cloudIPCStreamIterator).httpClient + assert.NotSame(t, http.DefaultClient, boundedClient) + expected := cfg.ClientTimeout + if timeout > 0 && timeout < expected { + expected = timeout + } + assert.Equal(t, expected, boundedClient.Timeout) + assert.Equal(t, timeout, customClient.Timeout) + }) + } +} + +func TestCloudFetchDownloadErrorsRedactQuery(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + <-request.Context().Done() + })) + defer server.Close() + cfg := config.WithDefaults() + cfg.ClientTimeout = 50 * time.Millisecond + cfg.RetryMax = 1 + cfg.RetryWaitMin = time.Millisecond + cfg.RetryWaitMax = time.Millisecond + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + iterator, err := NewCloudIPCStreamIterator(ctx, []*cli_service.TSparkArrowResultLink{{ + FileLink: server.URL + "/file?sig=SECRET_SIGNATURE", ExpiryTime: time.Now().Add(time.Hour).Unix(), RowCount: 1, + }}, 0, cfg, nil) + require.NoError(t, err) + defer iterator.Close() + _, nextErr := iterator.Next() + require.Error(t, nextErr) + assert.ErrorIs(t, nextErr, context.DeadlineExceeded) + assert.ErrorContains(t, nextErr, server.URL+"/file") + assert.NotContains(t, nextErr.Error(), "SECRET_SIGNATURE") +} + +func TestCloudFetchErrorRedactionPreservesCause(t *testing.T) { + cause := &url.Error{Op: "Get", URL: "https://storage.example/file?sig=SECRET_SIGNATURE", Err: context.DeadlineExceeded} + original := &url.Error{Op: "Get", URL: "https://storage.example/redirect?sig=SECRET_SIGNATURE", Err: cause} + redacted := redactCloudFetchError(original) + assert.NotSame(t, original, redacted) + assert.ErrorIs(t, redacted, context.DeadlineExceeded) + assert.NotContains(t, redacted.Error(), "SECRET_SIGNATURE") + assert.ErrorContains(t, redacted, "https://storage.example/file") + assert.ErrorContains(t, redacted, "https://storage.example/redirect") + assert.Contains(t, original.URL, "SECRET_SIGNATURE") + assert.Contains(t, cause.URL, "SECRET_SIGNATURE") +} + +func TestCloudFetchDownloadErrorsRedactLocation(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + response.Header().Set("Location", "http://127.0.0.1:badport/file?"+request.URL.RawQuery) + response.WriteHeader(http.StatusFound) + })) + defer server.Close() + cfg := config.WithDefaults() + cfg.RetryMax = 0 + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + iterator, err := NewCloudIPCStreamIterator(ctx, []*cli_service.TSparkArrowResultLink{{ + FileLink: server.URL + "/file?sig=SECRET_SIGNATURE", ExpiryTime: time.Now().Add(time.Hour).Unix(), RowCount: 1, + }}, 0, cfg, nil) + require.NoError(t, err) + defer iterator.Close() + _, nextErr := iterator.Next() + require.Error(t, nextErr) + assert.ErrorContains(t, nextErr, "failed to parse Location header") + assert.ErrorContains(t, nextErr, "/file") + assert.NotContains(t, nextErr.Error(), "SECRET_SIGNATURE") +} + +func TestCloudFetchClientTimeoutRetries(t *testing.T) { + for _, stall := range []string{"headers", "body"} { + t.Run(stall, func(t *testing.T) { + payload := generateMockArrowBytes(generateArrowRecord()) + var attempts atomic.Int32 + cancelled := make(chan struct{}) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if attempts.Add(1) == 1 { + if stall == "body" { + w.Header().Set("Content-Length", "100") + w.WriteHeader(http.StatusOK) + w.(http.Flusher).Flush() + } + <-r.Context().Done() + close(cancelled) + return + } + _, _ = w.Write(payload) + })) + defer server.Close() + cfg := config.WithDefaults() + cfg.ClientTimeout = 100 * time.Millisecond + cfg.UseLz4Compression = false + cfg.RetryMax = 1 + cfg.RetryWaitMin = time.Millisecond + cfg.RetryWaitMax = time.Millisecond + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + iterator, err := NewCloudBatchIterator(ctx, []*cli_service.TSparkArrowResultLink{{ + FileLink: server.URL, ExpiryTime: time.Now().Add(time.Hour).Unix(), RowCount: 6, + }}, 0, nil, cfg, nil) + require.NoError(t, err) + defer iterator.Close() + records, nextErr := iterator.Next() + require.NoError(t, nextErr) + records.Close() + assert.Equal(t, int32(2), attempts.Load()) + select { + case <-cancelled: + case <-ctx.Done(): + t.Fatal("timed-out request was not cancelled") + } + }) + } +} From 4a33e8091e809812856000b9626267c177f8b787 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Wed, 25 Nov 2026 17:19:26 +0000 Subject: [PATCH 2/3] fix: preserve CloudFetch error cause compatibility Signed-off-by: Vu Anh Phung --- internal/rows/arrowbased/batchloader.go | 4 ++++ .../arrowbased/cloudfetch_timeout_test.go | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/internal/rows/arrowbased/batchloader.go b/internal/rows/arrowbased/batchloader.go index d8e7456b..c364bcb2 100644 --- a/internal/rows/arrowbased/batchloader.go +++ b/internal/rows/arrowbased/batchloader.go @@ -431,6 +431,10 @@ func (downloadErr redactedCloudFetchError) Unwrap() error { return downloadErr.cause } +func (downloadErr redactedCloudFetchError) Cause() error { + return downloadErr.cause +} + // fetchBatchBytes downloads a single CloudFetch result link and returns the // raw response body, still compressed if the server used LZ4. Connection-time // failures, retryable HTTP statuses, and mid-stream body read failures are diff --git a/internal/rows/arrowbased/cloudfetch_timeout_test.go b/internal/rows/arrowbased/cloudfetch_timeout_test.go index 92bc7ef0..e6f12b2c 100644 --- a/internal/rows/arrowbased/cloudfetch_timeout_test.go +++ b/internal/rows/arrowbased/cloudfetch_timeout_test.go @@ -2,6 +2,7 @@ package arrowbased import ( "context" + "io" "net/http" "net/http/cookiejar" "net/http/httptest" @@ -12,6 +13,7 @@ import ( "github.com/databricks/databricks-sql-go/v2/internal/cli_service" "github.com/databricks/databricks-sql-go/v2/internal/config" + pkgerrors "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -108,6 +110,28 @@ func TestCloudFetchDownloadErrorsRedactQuery(t *testing.T) { assert.NotContains(t, nextErr.Error(), "SECRET_SIGNATURE") } +func TestCloudFetchDownloadErrorsPreserveCause(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + response.Header().Set("Content-Length", "100") + _, _ = io.WriteString(response, "short") + })) + defer server.Close() + cfg := config.WithDefaults() + cfg.RetryMax = 0 + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + iterator, err := NewCloudIPCStreamIterator(ctx, []*cli_service.TSparkArrowResultLink{{ + FileLink: server.URL + "/file?sig=SECRET_SIGNATURE", ExpiryTime: time.Now().Add(time.Hour).Unix(), RowCount: 1, + }}, 0, cfg, nil) + require.NoError(t, err) + defer iterator.Close() + _, nextErr := iterator.Next() + require.Error(t, nextErr) + assert.ErrorIs(t, nextErr, io.ErrUnexpectedEOF) + assert.Equal(t, io.ErrUnexpectedEOF, pkgerrors.Cause(nextErr)) + assert.NotContains(t, nextErr.Error(), "SECRET_SIGNATURE") +} + func TestCloudFetchErrorRedactionPreservesCause(t *testing.T) { cause := &url.Error{Op: "Get", URL: "https://storage.example/file?sig=SECRET_SIGNATURE", Err: context.DeadlineExceeded} original := &url.Error{Op: "Get", URL: "https://storage.example/redirect?sig=SECRET_SIGNATURE", Err: cause} From a7a0416b31bd5fda2ccdb71837e6ffadeb49e43d Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Wed, 25 Nov 2026 17:24:38 +0000 Subject: [PATCH 3/3] docs: clarify CloudFetch transport timeout behavior Signed-off-by: Vu Anh Phung --- connector.go | 1 + 1 file changed, 1 insertion(+) diff --git a/connector.go b/connector.go index abc1bd9d..31105ec5 100644 --- a/connector.go +++ b/connector.go @@ -531,6 +531,7 @@ func WithAuthenticator(authr auth.Authenticator) ConnOption { } // WithTransport sets up the transport configuration to be used by the httpclient. +// Thrift CloudFetch downloads also use this transport, with ClientTimeout bounding each attempt. func WithTransport(t http.RoundTripper) ConnOption { return func(c *config.Config) { c.Transport = t