Skip to content

Commit

Permalink
Query cancellation fix (#148)
Browse files Browse the repository at this point in the history
* cancellation fix

* cancellation fix

* cancellation fix

* wait for bg routine to finish
  • Loading branch information
k-anshul authored Feb 1, 2024
1 parent badd00c commit 2f01e5b
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,28 @@ func (s *stmt) execute(ctx context.Context, args []driver.NamedValue) (*C.duckdb
}
defer C.duckdb_destroy_pending(&pendingRes)

done := make(chan bool)
defer close(done)

mainDoneCh := make(chan struct{})
bgDoneCh := make(chan struct{})
go func() {
select {
case <-ctx.Done():
// also need to interrupt to cancel the query
C.duckdb_interrupt(*s.c.con)
close(bgDoneCh)
return
case <-done:
case <-mainDoneCh:
close(bgDoneCh)
return
}
}()

var res C.duckdb_result
if state := C.duckdb_execute_pending(pendingRes, &res); state == C.DuckDBError {
state := C.duckdb_execute_pending(pendingRes, &res)
close(mainDoneCh)
// also wait for background goroutine to finish
// sometimes the bg goroutine is not scheduled immediately and by that time if another query is running on this connection
// it can cancel that query so need to wait for it to finish as well
<-bgDoneCh
if state == C.DuckDBError {
if ctx.Err() != nil {
return nil, ctx.Err()
}
Expand Down

0 comments on commit 2f01e5b

Please sign in to comment.