nikita-volkov / hasql-pool

A pool of connections for Hasql
http://hackage.haskell.org/package/hasql-pool
MIT License
17 stars 15 forks source link

There can be more open connections than the pool limit #30

Closed robx closed 1 year ago

robx commented 1 year ago

hasql-pool uses this pattern to discard connections:

join . atomically $ do
  -- stuff
  modifyTVar' poolCapacity succ
  return $ Connection.release conn

This means (unless I'm missing something?) that it's quite possible to run into a sequence of actions

  1. thread A: bump pool capacity, return connection release action
  2. thread B: create new connection because pool has capacity
  3. thread A: execute the connection release action

Between 2 and 3 there are more "physical" connections than the pool limit, if we were at the limit before.

This seems like something we'd want to guarantee can't happen, right?

A possible solution should be to change that fragment to

join . atomically $ do
  -- stuff
  return $ do
    Connection.release conn
    atomically $ modifyTVar' poolCapacity succ
nikita-volkov commented 1 year ago

Yep. Great catch of an undiscovered bug :)