Skip to content

Commit f345ddf

Browse files
fix: close rows after query timeout to prevent hang
1 parent 2138fa7 commit f345ddf

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

cmd/modern/e2e_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"sync"
1414
"testing"
15+
"time"
1516

1617
"github.com/stretchr/testify/assert"
1718
"github.com/stretchr/testify/require"
@@ -312,6 +313,23 @@ func TestE2E_PipedInput_WithBytesBuffer_NoPanic(t *testing.T) {
312313
assert.NotContains(t, outputStr, "nil pointer", "should not have nil pointer error")
313314
}
314315

316+
func TestE2E_QueryTimeout_NoHang(t *testing.T) {
317+
skipIfNoLiveConnection(t)
318+
binary := buildBinary(t)
319+
320+
args := append([]string{"-C", "-t", "1", "-Q", "WAITFOR DELAY '00:00:10'"}, getAuthArgs(t)...)
321+
cmd := exec.Command(binary, args...)
322+
cmd.Env = os.Environ()
323+
324+
start := time.Now()
325+
output, err := cmd.CombinedOutput()
326+
elapsed := time.Since(start)
327+
328+
assert.Error(t, err)
329+
assert.Contains(t, string(output), "Timeout expired")
330+
assert.Less(t, elapsed, 30*time.Second, "command hung instead of timing out")
331+
}
332+
315333
// cleanupBinary removes the temporary build directory containing the test binary.
316334
// TestMain calls this to ensure deterministic cleanup instead of relying on
317335
// eventual OS temp directory maintenance.

pkg/sqlcmd/sqlcmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ func (s *Sqlcmd) runQuery(query string) (int, error) {
431431
}
432432
retmsg := &sqlexp.ReturnMessage{}
433433
rows, qe := s.db.QueryContext(ctx, query, retmsg)
434+
if rows != nil {
435+
defer func() { _ = rows.Close() }()
436+
}
434437
if qe != nil {
435438
s.Format.AddError(qe)
436439
}

0 commit comments

Comments
 (0)