Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic in sqlx_core::pool::connection::Floating<DB,sqlx_core::pool::connection::Idle<DB>>::metadata #1912

Closed
lovasoa opened this issue Jun 17, 2022 · 7 comments · Fixed by #1915

Comments

@lovasoa
Copy link
Contributor

lovasoa commented Jun 17, 2022

When using PoolOptions::before_acquire, I got a panic crashing my entire server with the following error:

thread 'main' panicked at 'supplied instant is later than self', library/std/src/time.rs:313:48
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:116:14
   2: core::panicking::panic_display
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:72:5
   3: core::panicking::panic_str
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:56:5
   4: core::option::expect_failed
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/option.rs:1817:5
   5: core::option::Option<T>::expect
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/option.rs:692:21
   6: std::time::Instant::duration_since
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/time.rs:313:9
   7: sqlx_core::pool::connection::Floating<DB,sqlx_core::pool::connection::Idle<DB>>::metadata
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.6.0/src/pool/connection.rs:328:18
   8: sqlx_core::pool::inner::check_idle_conn::{{closure}}
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.6.0/src/pool/inner.rs:374:20
   9: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  10: sqlx_core::pool::inner::PoolInner<DB>::acquire::{{closure}}::{{closure}}
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.6.0/src/pool/inner.rs:197:79
  11: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  12: <tokio::time::timeout::Timeout<T> as core::future::future::Future>::poll
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/time/timeout.rs:176:33
  13: sqlx_core::pool::inner::PoolInner<DB>::acquire::{{closure}}
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.6.0/src/pool/inner.rs:219:10
  14: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  15: sqlx_core::pool::Pool<DB>::acquire::{{closure}}
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.6.0/src/pool/mod.rs:343:38
  16: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  17: sqlx_core::pool::executor::<impl sqlx_core::executor::Executor for &sqlx_core::pool::Pool<DB>>::prepare_with::{{closure}}
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.6.0/src/pool/executor.rs:58:45
  18: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  19: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/future.rs:123:9
  20: opcua_server::database::Database::init::{{closure}}
             at ./src/database.rs:76:18
  21: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  22: opcua_server::Main::create::{{closure}}
             at ./src/main.rs:38:91
  23: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  24: opcua_server::main::{{closure}}
             at ./src/main.rs:88:66
  25: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/future/mod.rs:84:19
  26: tokio::park::thread::CachedParkThread::block_on::{{closure}}
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/park/thread.rs:263:54
  27: tokio::coop::with_budget::{{closure}}
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/coop.rs:102:9
  28: std::thread::local::LocalKey<T>::try_with
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/thread/local.rs:412:16
  29: std::thread::local::LocalKey<T>::with
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/thread/local.rs:388:9
  30: tokio::coop::with_budget
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/coop.rs:95:5
  31: tokio::coop::budget
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/coop.rs:72:5
  32: tokio::park::thread::CachedParkThread::block_on
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/park/thread.rs:263:31
  33: tokio::runtime::enter::Enter::block_on
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/runtime/enter.rs:151:13
  34: tokio::runtime::thread_pool::ThreadPool::block_on
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/runtime/thread_pool/mod.rs:90:9
  35: tokio::runtime::Runtime::block_on
             at /home/olojkine/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.19.2/src/runtime/mod.rs:482:43
  36: opcua_server::main
             at ./src/main.rs:101:5
@lovasoa
Copy link
Contributor Author

lovasoa commented Jun 17, 2022

sqlx should probably use saturating_duration_since instead of duration_since in Floating::metadata.

See: https://doc.rust-lang.org/std/time/struct.Instant.html#monotonicity

@abonander
Copy link
Collaborator

No, I'm dumb. These need to be reversed: https://github.com/launchbadge/sqlx/blob/main/sqlx-core/src/pool/connection.rs#L328-L329

So

        PoolConnectionMetadata {
            age: now.duration_since(self.created_at),
            idle_for: now.duration_since(self.idle_since),
        }

The receiver of the method is supposed to be the later Instant and the argument is the earlier one. Kinda easy to confuse tbh, I probably should have just used the - operator as getting that wrong would be easier to spot.

@lovasoa
Copy link
Contributor Author

lovasoa commented Jun 17, 2022

Well, actually, looking at the code: it looks like the subtraction is done in the wrong order !

@lovasoa
Copy link
Contributor Author

lovasoa commented Jun 17, 2022

You beat me to it :)

@lovasoa
Copy link
Contributor Author

lovasoa commented Jun 17, 2022

The receiver of the method is supposed to be the later Instant and the argument is the earlier one. Kinda easy to confuse tbh, I probably should have just used the - operator as getting that wrong would be easier to spot.

I think the main takeaway is rather: there should have been a test catching that 😉

@lovasoa
Copy link
Contributor Author

lovasoa commented Jun 17, 2022

This code was obviously not executed a single time before being published on crates.io...

@abonander
Copy link
Collaborator

Yeah, I meant to add a test but forgot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants