Closed CodyDWJones closed 2 years ago
It sounds like a legitimate bug report. I don't have a timeline for a fix.
I have just submitted a proposed fix in #498.
Not an issue; the Connection
and Cursor
docs say they do not support concurrent calls, but those comments are very old and aren't actually true.
Describe the bug The connection pool is only useful when writing concurrent code, but it doesn't actually stop multiple goroutines from accessing the same connection simultaneously.
To Reproduce The PR includes a test suite that reproduces the issue with the old Pool code, so long as it's edited to include the
afterAcquire
andbeforeRelease
test hooks.Expected behavior
Pool.Exec()
,Pool.Ping()
,Pool.Query()
, andPool.Server()
are safe for concurrent use. Two calls can only use the same connection serially.Screenshots n/a
System info n/a
Additional context Problems with the prior pool implementation:
Pool.conn()
uses the connections in round-robin order without checking if they are still in use. It appears queries and cursors can be multiplexed on one connection, so this would work fine in the single goroutine case. However, if multiple goroutines call thePool
there is no guarantee eachConnection
will only be used by one goroutine at a time.Similarly there is a race condition in the lines:
Suppose
pointer == 9
andlen(conns) == 10
. Three goroutines could run in this sequence:Because there is no "in use" check, two of the goroutines end up using the same
Connection
.