lib / pq

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

Context Cancellation May Collateralize Future Queries #1001

Closed kahuang closed 3 years ago

kahuang commented 3 years ago

PR that fixes this issue here: https://github.com/lib/pq/pull/1000

The way query cancellation is implemented now, there is a race between cancelling the query in postgres, and potentially re-using that same connection for a future query. What can happen is:

  1. A query [query1] is executed
  2. The context for [query1] is cancelled
  3. The watchCancel goroutine is triggered, which begins the query cancellation in postgres
  4. Before postgres can finish cancelling [query1], it returns its result, doesn't see that it's being cancelled, and returns its result and is returned to the connection pool
  5. This same connection is reused for another query, [query2]
  6. The query cancellation from [query1] in postgres goes through
  7. [query2] gets erroneously cancelled

At a few hundred transactions/s, we'd only see a few of these per day. Since applying the above patch, we haven't seen this issue for the past 3 days.

kahuang commented 3 years ago

Fixed in https://github.com/lib/pq/pull/1000