marcboeker / go-duckdb

go-duckdb provides a database/sql driver for the DuckDB database engine.
MIT License
646 stars 97 forks source link

Query cancellation fix #148

Closed k-anshul closed 7 months ago

k-anshul commented 8 months ago

Hey @marcboeker

Found one tricky issue in the query cancellation that we had discussed previously. It is easy to reproduce when query completes quickly like in case of in-memory DB and checkpoint query. Example below:

func main() {
    db, err := sql.Open("duckdb", "")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    conn, err := db.Conn(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    var wg sync.WaitGroup
    wg.Add(100)
    for i := 0; i < 100; i++ {
        v := i
        go func() {
            ctx, cancel := context.WithCancel(context.Background())
            defer cancel()
            defer wg.Done()
            _, err = conn.ExecContext(ctx, "CHECKPOINT;")
            if err != nil {
                log.Printf("err %v", err)
            }
            fmt.Printf("iteration %v done\n", v)
        }()
    }
    wg.Wait()
}

cc : @begelundmuller

k-anshul commented 7 months ago

Hey @marcboeker

Just nudging in case this missed your radar.

marcboeker commented 7 months ago

@k-anshul Thanks for the ping. Missed the change from draft to ready to merge.