launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
12.58k stars 1.19k forks source link

PoolTimeOut error happens frequently #2276

Open option-greek opened 1 year ago

option-greek commented 1 year ago

Bug Description

PoolTimeOut error happens frequently when the server is idle. If I don't give a gap between requests, the timeout doesn't happen. My guess is when there is a timeout, the pool/its implementation doesn't detect it. Note: I know it's not a DB issue because connecting directly works (and also works after the pool has timed out). Worst part is if I make a second request, it still waits till the first one times out.

Steps: 1) Create a pool and make it do a query when accessed through a tokio thread (could be any web server framework. I'm using warp) 2) Make the first request. It succeeds 3) Keep the server idle for some time (I kept for 1 hour - happens below that too) 4) Make the second request: It times out with pool timeout. While its hanging trying to timeout, any number of parallel requests will also hang. 5) Make the 3rd request after the PoolTimeOut error. It will succeed again.

Minimal Reproduction

This is how I'm initializing the pool.


   match PgPoolOptions::new()
            .max_connections(10)
            .min_connections(3)
            .connect(&prefs.pg_conn_string.clone()).await
        {
            Ok(pool) =>
                {
                    println!("Successfully inited the conn pool for pg");
                    return Option::from(pool);
                }

            Err(err) => {
                println!("Not able to create the conn pool for pg {:?}", err);
            }
        }

Info

option-greek commented 1 year ago

If anyone knows how to use deadpool with sqlx, please provide a sample. The previous example provided here no longer compiles.

option-greek commented 1 year ago

Workaround: The problem doesn't happen with max life time of 2 min and idle time out of 1 min. It works reliably with these settings.

sugerdudu commented 11 months ago

I have the same problem. Is that okay?

.idle_timeout(Some(Duration::from_secs(60))) .max_lifetime(Some(Duration::from_secs(120)))

@option-greek

JimitSoni18 commented 7 months ago

I am currently facing this same issue, I sql queries will randomly error out with PoolTimedOut, and only rarely there would be no error.