Recently I upgraded sqlx from 0.5.1 to 0.5.2 in one of my hobby projects. This caused me some problems with the connection pooling. I'm not very familiar with rust and with the sqlx codebase I am not familiar at all. But I still wanted to get to the root of this.

A tool that helped me with things like this in the past is git bisect. I just had to find out how to use git bisect with a cargo dependency. And here is how:

First I got my hands on the dependency I wanted to troubleshoot and started the bisect process:

git clone https://github.com/launchbadge/sqlx.git
git checkout v0.5.2
git bisect start
git bisect bad
git bisect good v0.5.1

Git will now check out the commit between the two specified revisions and asks me to test it. Depending on wether it's working or not I git bisect good or git bisect bad. I can also skip commits that I cannot use for testing with git bisect skip.

Now I just had to point cargo to my local directory to use the checked out version of sqlx. For this I edited my Cargo.toml like this:

diff --git a/Cargo.toml b/Cargo.toml
index dfb71b9..7031e4d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,7 +12,7 @@ hyper = "0.14"
 tokio = { version = "1", features = ["full"] }
 tokio-util = "0.6"
 warp = { version = "0.3", features = [] }
-sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "postgres", "offline", "time", "chrono"] }
+sqlx = { path = "/home/serra/dev/sqlx", features = ["runtime-tokio-rustls", "postgres", "offline", "time", "chrono"] }
 serde = { version = "1", features = [] }
 serde_json = "1"
 anyhow = "1"

Now I could rebuild and test my project. To make sure I am not getting some strange artifacts I cleaned the buildcache before every run:

cargo clean
cargo b --release
# Start the project and see if the problem is fixed

After a sequence of goods and bads we arrive at the first "bad" commit:

5295ff10a55a8a4854f410c45968c5c709aa65a4 is the first bad commit
commit 5295ff10a55a8a4854f410c45968c5c709aa65a4
Author: Austin Bonander <austin@launchbadge.com>
Date:   Thu Apr 1 19:18:01 2021 -0700

   fix: pool internals improvements

   * fix `DecrementSizeGuard::drop()` only waking one `Waiter` regardless of whether that waiter was already woken
   * fix connect-backoff loop giving up the size guard
   * don't cut in line to open a new connection
   * have tasks waiting on `acquire()` wake periodically to check if there's a connection in the queue

   Signed-off-by: Austin Bonander <austin@launchbadge.com>

sqlx-core/src/pool/connection.rs |   2 +-
sqlx-core/src/pool/inner.rs      | 231 ++++++++++++++++++++-------------------
sqlx-core/src/pool/options.rs    |  16 ++-
tests/postgres/postgres.rs       |  31 +++---
4 files changed, 145 insertions(+), 135 deletions(-)

Now I have narrowed down the code that I have to read and understand to progress with my troubleshooting.