lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.01k stars 910 forks source link

Context cancelation blocks for syntax errors #922

Closed cwood closed 4 years ago

cwood commented 4 years ago
package main

import (
    "context"
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
    "time"
)

func main() {
    const delay_in_secs = 10
    db, err := sql.Open("postgres", "postgres://localhost/postgres?sslmode=disable")
    if err != nil {
        panic(fmt.Sprintf("failed to open DB: %v", err))
    }
    ctx, cancelFunc := context.WithTimeout(context.Background(), (delay_in_secs/2)*time.Second)
    defer cancelFunc()
    start := time.Now()
    defer func() {
        fmt.Printf("Total time: %v\n", time.Since(start))
    }()
    tx, err := db.BeginTx(ctx, nil)
    if err != nil {
        panic(fmt.Sprintf("failed to begin TX: %v", err))
    }
    defer tx.Commit()
    _, err = tx.ExecContext(ctx, fmt.Sprintf("SELECT pg_sleep(%d", delay_in_secs))
    if err != nil {
        panic(fmt.Sprintf("failed to begin TX: %v", err))
    }
}

I noticed if I have a syntax error like this example with a missing paren the query exec will not return with an error but will block because of the context being used. So this hangs forever.

version: go1.13

cwood commented 4 years ago

Closing this is due to use using a super old version