Skip to content
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

interrupt query on ctx cancel/timeout #143

Merged
merged 3 commits into from
Jan 12, 2024
Merged
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
16 changes: 16 additions & 0 deletions duckdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,22 @@ func TestParquetExtension(t *testing.T) {
require.NoError(t, err)
}

func TestQueryTimeout(t *testing.T) {
db := openDB(t)
defer db.Close()

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

now := time.Now()
_, err := db.ExecContext(ctx, "CREATE TABLE test AS SELECT * FROM range(10000000) t1, range(1000000) t2;")
require.ErrorIs(t, err, context.DeadlineExceeded)

// a very defensive time check, but should be good enough
// the query takes much longer than 10 seconds
require.Less(t, time.Since(now), 10*time.Second)
}

func openDB(t *testing.T) *sql.DB {
db, err := sql.Open("duckdb", "")
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func (s *stmt) execute(ctx context.Context, args []driver.NamedValue) (*C.duckdb
select {
// if context is cancelled or deadline exceeded, don't execute further
case <-ctx.Done():
// also need to interrupt to cancel the query
C.duckdb_interrupt(*s.c.con)
return nil, ctx.Err()
default:
// continue
Expand Down
Loading