Skip to content

Commit

Permalink
Merge pull request #4 from k-anshul/cancellation_fix
Browse files Browse the repository at this point in the history
Cancellation fix
  • Loading branch information
k-anshul authored Jan 17, 2024
2 parents aeedfc7 + be560b9 commit ec79707
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,19 @@ func (s *stmt) execute(ctx context.Context, args []driver.NamedValue) (*C.duckdb
}
defer C.duckdb_destroy_pending(&pendingRes)

done := make(chan bool)
done := make(chan struct{})
defer close(done)
queryComplete := false

go func() {
select {
case <-ctx.Done():
// also need to interrupt to cancel the query
// sometimes this goroutine is not scheduled immediately and by that time if another query is scheduled on this connection
// this can cancel that query so need to handle cases when original query was complete.
if queryComplete {
return
}
// need to interrupt to cancel the query
C.duckdb_interrupt(*s.c.con)
return
case <-done:
Expand All @@ -218,7 +224,9 @@ func (s *stmt) execute(ctx context.Context, args []driver.NamedValue) (*C.duckdb
}()

var res C.duckdb_result
if state := C.duckdb_execute_pending(pendingRes, &res); state == C.DuckDBError {
state := C.duckdb_execute_pending(pendingRes, &res)
queryComplete = true
if state == C.DuckDBError {
if ctx.Err() != nil {
return nil, ctx.Err()
}
Expand Down

0 comments on commit ec79707

Please sign in to comment.