Skip to content

Commit

Permalink
cancellation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anshul committed Jan 18, 2024
1 parent 04231da commit d940582
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,20 @@ 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 {
// if context is cancelled or deadline exceeded, don't execute further
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 @@ -219,16 +225,16 @@ 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()
}

dbErr := C.GoString(C.duckdb_result_error(&res))
C.duckdb_destroy_result(&res)
return nil, errors.New(dbErr)
}

return &res, nil
}

Expand Down

0 comments on commit d940582

Please sign in to comment.