crawshaw / sqlite

Go SQLite3 driver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sqlitex.Pool Close and Put are racy

AdamSLevy opened this issue · comments

Currently the Pool has a race condition between Pool.Close and Pool.Put, which may easily be called from different threads.

When Close is called, it closes all Conns, including those that are currently in use by other goroutines. Put also executes some commands on Conn which is racy with the same Conn's use in Close.

For the Pool to not have race conditions between Close and other goroutines that may call Put or may be using Conns from the Pool, it should not touch Conns that have not yet been returned with Put. Additionally, Put must be guarded against returning a Conn to a close channel.

I propose that Close only close Conns from the free channel, and block until all Conns have been Put and closed. This means that not returning a Conn to the Pool before an application exits will be considered incorrectly programmed. users will need to ensure that Conns are returned to Put in order of Close to return. Proper use of context cancellation should make this fairly idiomatic.

Additionally the Pool.GetSnapshot will need to be modified as the set aside Conn is only returned in a Finalizer which is not guaranteed to be run.