sqlx
sqlx copied to clipboard
SelectContext does not cancel when context is timed out
I'm using the SelectContext method, and trying to timeout the context to cancel indefinitely running queries. It appears that the timeout is not respected, and the goroutine hangs forever. It seems that SelectContext should respect context.WithTimeout.
Code:
ctx, cancel := context.WithTimeout(ctx, _defaultTimeout)
defer cancel()
return g.DB.SelectContext(ctx, dest, query, args...)
SIGQUIT trace:
goroutine 158 [sync.RWMutex.RLock, 52 minutes]:
--
runtime.gopark(0x4?, 0xd0?, 0x20?, 0xc1?, 0x1?)
GOROOT/src/runtime/proc.go:398 +0xce fp=0xc00147d100 sp=0xc00147d0e0 pc=0x1c4f1ee
runtime.goparkunlock(...)
GOROOT/src/runtime/proc.go:404
runtime.semacquire1(0xc0093ca1f4, 0x0?, 0x3, 0x0, 0x39?)
GOROOT/src/runtime/sema.go:160 +0x218 fp=0xc00147d168 sp=0xc00147d100 pc=0x1c60738
sync.runtime_SemacquireRWMutexR(0x4e4f495449545241?, 0x20?, 0x4e49462027313230?)
GOROOT/src/runtime/sema.go:82 +0x25 fp=0xc00147d1a0 sp=0xc00147d168 pc=0x1c7eb05
sync.(*RWMutex).RLock(...)
GOROOT/src/sync/rwmutex.go:71
database/sql.(*Rows).Columns(0xc0093ca1b0)
GOROOT/src/database/sql/sql.go:3111 +0x65 fp=0xc00147d248 sp=0xc00147d1a0 pc=0x265bd25
github.com/jmoiron/sqlx.(*Rows).Columns(0xb62638?)
<autogenerated>:1 +0x1f fp=0xc00147d260 sp=0xc00147d248 pc=0x2c5685f
github.com/jmoiron/sqlx.scanAll({0xb43a20?, 0xc0011a4050?}, {0x328860, 0xc004b7af90}, 0x0)
external/com_github_jmoiron_sqlx/sqlx.go:924 +0x253 fp=0xc00147d3f8 sp=0xc00147d260 pc=0x2c52253
github.com/jmoiron/sqlx.SelectContext({0xb40368?, 0xc0008c2190?}, {0xb33c60?, 0xc001609bf0?}, {0x328860, 0xc004b7af90}, {0x85b081?, 0x18?}, {0x0, 0x0, ...})
external/com_github_jmoiron_sqlx/sqlx_context.go:61 +0xf0 fp=0xc00147d478 sp=0xc00147d3f8 pc=0x2c52fb0
github.com/jmoiron/sqlx.(*DB).SelectContext(...)
external/com_github_jmoiron_sqlx/sqlx_context.go:140
<...>
I am facing the same problem, did you find any solution?
hmm, ran into the same thing, not sure what and where isn't canceling the context. In the mean time I opted for:
// Select performs a db.Select with a Timeout context of 5 seconds.
func Select(db *sqlx.DB, dest interface{}, query string, args ...interface{}) error {
// It seems db.SelectContext does _not_ do context cancellation, which sucks.
ch := make(chan error)
go func() {
err := db.Select(dest, query, args...)
ch <- err
}()
select {
case <-time.After(5 * time.Second):
return dberror.Connection
case err := <-ch:
return err
}
}
Running into this as well. Would be great if we could get it fixed to avoid channel wrapping.
any news on this?