pq: Context cancel hangs if there is no network connectivity
Hi,
Not sure if this is a bug or if it is effectively the same as #584. But it appears to be a significant Gotcha if I’m understanding correctly.
Setup
package main
import (
"context"
"database/sql"
"fmt"
"time"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "postgresql://127.0.0.1:5432/postgres?sslmode=disable&connect_timeout=3")
if err != nil {
panic(err)
}
for {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
rows, err := db.QueryContext(ctx, "SELECT 1")
if err != nil {
fmt.Println("ERROR:", err)
}
rows.Next()
var i int
err = rows.Scan(&i)
rows.Close()
cancel()
if err != nil {
panic(err)
}
fmt.Println("Got", i)
time.Sleep(1 * time.Second)
}
}
Behaviour
- Code above works fine with a local postgres instance up
- If you stop the PG instance the connect will be reset and it will fail loudly as expected
- Do any of the following to simulate failure:
- simulate process busy/hung by finding the PID of the connected postgres process and then
kill -stop <pid> - simulate packet loss by adding
echo "block drop quick on lo0 proto tcp from any to any port = 5432" | sudo pfctl -ef -on OS X or equivalent iptables rule to drop packets to the local database - Expected behavior: that the client’s timeout after 5 seconds with an error and print to screen before retrying
- ** Actual Behaviour**: the client remains hung indefinitiely in either case until the “failure” is resolved
Cause
- It seems to be due to the fact that the code handling Context cancelation (https://github.com/lib/pq/blob/master/conn_go18.go#L67) attempts to send a
cancelop to the database and waits forever for a response - The
cancelmethod even callsdialinternally but this is not even respecting theconnect_timeoutoption, possibly because it’s returning a pooled connection? Even my trivial test example doesn’t timeout though and there should only be a single connection made.
Ramifications
In my case the motivation for wanting to use Contexts in the first place is that I need tight bounds on execution time and can’t afford to have clients hang indefinitely on DB whatever the error condition is in the real-world (both packet loss and hung server processes happen).
I could implement this externally presumably with my own watcher on the context in another Goroutine that tears down the whole connection, but it seems like this mechanism should work for my case without re-inventing it outside.
Is there an alternative option?
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 6
- Comments: 15
Almost 3 years passed and still no support
context.WithTimeout()for query…Any news on this? Sounds like a serious issue and it’s almost a year since it was first reported.
I’ll work on this soon.
Another year has passed, and I’ve stumbled upon this issue as well.
I’m currently experencing this issue as described by @banks. Using pq-timeouts won’t help in my case because I’m using a long lived connection pool and have different timeout values per-query. pq-timeouts is only a solution if all queries can use the same timeout value or you create a new connection per-query.
It seems like a bug that if the database your querying is for whatever reason unable to respond that it will hang forever. The whole point of context is to terminate things that run to long.
In my case of PostgreSQL databases are so loaded that at times they can’t respond to the cancel query request so db.QueryContext() hangs forever.
Even if I wrap this in a switch statement with timeout I’ll be leaking connections that never close.