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 at sqlx-core-0.5.1/src/mysql/io/buf.rs:25 #1078

Closed
PaulGrandperrin opened this issue Mar 1, 2021 · 3 comments
Closed

Panic at sqlx-core-0.5.1/src/mysql/io/buf.rs:25 #1078

PaulGrandperrin opened this issue Mar 1, 2021 · 3 comments

Comments

@PaulGrandperrin
Copy link

The function:

   #[tracing::instrument]
    pub async fn change_user_credentials(&self, user_id: &[u8], mut version: u64, username: &[u8], opaque_password: &[u8], sealed_master_key: &[u8], sealed_private_data: &[u8], recovery: bool) -> api::Result<u64> {
        let mut tx = self.pool.begin().await.map_err(|e| api::Error::ServerSideError(e.into()))?;
        
        let row: MySqlRow = sqlx::query("select `version` from `user` where `user_id` = ?")
            .bind(user_id)
            .fetch_one(&mut tx).await.map_err(|e| api::Error::ServerSideError(e.into()))?;
        let db_version: u64 = row.try_get(0).map_err(|e| api::Error::ServerSideError(e.into()))?;
        
        if db_version != version { 
            return Err(api::Error::VersionConflict(db_version, version));
        };

        version +=1;

        let query = format!("update `user` set `version` = ?, `username{0}` = ?, `opaque_password{0}` = ?, `sealed_master_key` = ?, `sealed_private_data` = ? where `user_id` = ?", if recovery {"_recovery"} else {""});
        sqlx::query(&query)
            .bind(version)
            .bind(username)
            .bind(opaque_password)
            .bind(sealed_master_key)
            .bind(sealed_private_data)
            .bind(user_id)
            .execute(&mut tx).await.map_err(|e| {
                match e {
                    sqlx::Error::Database(e)
                        if e.as_error().downcast_ref::<MySqlDatabaseError>().map(|e| e.number()) == Some(1062)
                        => api::Error::UsernameConflict,
                    _ => api::Error::ServerSideError(e.into()),
                }
            })?;

        tx.commit().await.map_err(|e| api::Error::ServerSideError(e.into()))?;

        Ok(version)
    }

When there is a version conflict, I get this panic, I guess probably when the transaction is being rolled-back.

Mar 01 17:26:44.544  INFO rpc{req.ip=::1 req.port=50372}:UpdateUserCredentials{username=a recovery=false}: server::rpc: VersionConflict(8, 7)
The application panicked (crashed).
Message:  assertion failed: self.remaining() >= dst.len()
Location: /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.0.1/src/buf/buf_impl.rs:250

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   1: color_eyre::config::PanicHook::panic_report::hb50bf77c2c098f55
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/color-eyre-0.5.10/src/config.rs:982
   2: color_eyre::config::PanicHook::into_panic_hook::{{closure}}::hfa7572c533d6b92c
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/color-eyre-0.5.10/src/config.rs:961
   3: std::panicking::rust_panic_with_hook::hbcaa5de2cb5e22d5
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/std/src/panicking.rs:595
   4: std::panicking::begin_panic_handler::{{closure}}::h4ee6cde415c8f62d
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/std/src/panicking.rs:495
   5: std::sys_common::backtrace::__rust_end_short_backtrace::h895319f2d3f611c0
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/std/src/sys_common/backtrace.rs:141
   6: rust_begin_unwind<unknown>
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/std/src/panicking.rs:493
   7: core::panicking::panic_fmt::h0123abb763a6e96f
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/core/src/panicking.rs:92
   8: core::panicking::panic::hdf1b6c818ad7c6ad
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/core/src/panicking.rs:50
   9: bytes::buf::buf_impl::Buf::copy_to_slice::hc1d2fba3ee5f279a
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.0.1/src/buf/buf_impl.rs:250
  10: bytes::buf::buf_impl::Buf::get_u64_le::h79bcded757173d87
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.0.1/src/buf/buf_impl.rs:511
  11: <bytes::bytes::Bytes as sqlx_core::mysql::io::buf::MySqlBufExt>::get_uint_lenenc::h1ec7029b0c023ce3
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.1/src/mysql/io/buf.rs:25
  12: sqlx_core::mysql::connection::stream::MySqlStream::skip_result_metadata::{{closure}}::hc947c9f1fa12eb83
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.1/src/mysql/connection/stream.rs:185
  13: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hce3790fd0fa0d7d0
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:80
  14: sqlx_core::mysql::connection::stream::MySqlStream::wait_until_ready::{{closure}}::h8cde2f65b6adcfc8
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.1/src/mysql/connection/stream.rs:109
  15: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h4a0016fadb262e06
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:80
  16: <core::pin::Pin<P> as core::future::future::Future>::poll::hacde063ad1cc3951
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/future.rs:119
  17: <sqlx_core::pool::connection::PoolConnection<DB> as core::ops::drop::Drop>::drop::{{closure}}::h50e2e079da58fb86
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.1/src/pool/connection.rs:79
  18: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h3358ee72401c9bb0
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/mod.rs:80
  19: tokio::runtime::task::core::CoreStage<T>::poll::{{closure}}::h49ae42693c659574
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/core.rs:235
  20: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut::hedfde37959d39def
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/loom/std/unsafe_cell.rs:14
  21: tokio::runtime::task::core::CoreStage<T>::poll::h0868c9878a2b9429
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/core.rs:225
  22: tokio::runtime::task::harness::poll_future::{{closure}}::h1c8f8e94734cac35
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:422
  23: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hc15ae9d4e28cb8a5
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:344
  24: std::panicking::try::do_call::h6e4b9d3ad728dcb3
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:379
  25: __rust_try<unknown>
      at <unknown source file>:<unknown line>
  26: std::panicking::try::h7ed52f0842d5876a
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:343
  27: std::panic::catch_unwind::ha9bacf725f3c2a03
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:431
  28: tokio::runtime::task::harness::poll_future::h9ea76cf9170bb612
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:409
  29: tokio::runtime::task::harness::Harness<T,S>::poll_inner::h6f7153508aabd386
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:89
  30: tokio::runtime::task::harness::Harness<T,S>::poll::h8dc9779a23643fb1
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:59
  31: tokio::runtime::task::raw::poll::h95f1b70bc0346ac4
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/raw.rs:104
  32: tokio::runtime::task::raw::RawTask::poll::hed609415f0ff56a8
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/raw.rs:66
  33: tokio::runtime::task::Notified<S>::run::hcb469ac055fba128
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/mod.rs:171
  34: tokio::runtime::thread_pool::worker::Context::run_task::{{closure}}::h21931191c3b44d13
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/thread_pool/worker.rs:370
  35: tokio::coop::with_budget::{{closure}}::h326e6667271b3948
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/coop.rs:121
  36: std::thread::local::LocalKey<T>::try_with::ha21eaf595e5f48f9
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:272
  37: std::thread::local::LocalKey<T>::with::h2724f14b654422d2
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:248
  38: tokio::coop::with_budget::h63c30d926e274ee0
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/coop.rs:114
  39: tokio::coop::budget::h30af9f6d74d29b65
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/coop.rs:98
  40: tokio::runtime::thread_pool::worker::Context::run_task::hc9cabe666beb9cac
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/thread_pool/worker.rs:348
  41: tokio::runtime::thread_pool::worker::Context::run::h76c6d3d1563a64b8
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/thread_pool/worker.rs:318
  42: tokio::runtime::thread_pool::worker::run::{{closure}}::hbff70d7117f43e97
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/thread_pool/worker.rs:303
  43: tokio::macros::scoped_tls::ScopedKey<T>::set::hd83e26437aea70b9
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/macros/scoped_tls.rs:61
  44: tokio::runtime::thread_pool::worker::run::hb05ac016c1d45f42
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/thread_pool/worker.rs:300
  45: tokio::runtime::thread_pool::worker::Launch::launch::{{closure}}::h554d160ade47755d
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/thread_pool/worker.rs:279
  46: <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll::h042811d412bdaa0e
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/blocking/task.rs:42
  47: tokio::runtime::task::core::CoreStage<T>::poll::{{closure}}::hcca1059dad99f342
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/core.rs:235
  48: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut::hc49ac73197324970
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/loom/std/unsafe_cell.rs:14
  49: tokio::runtime::task::core::CoreStage<T>::poll::he9ab9a53a94e0ecf
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/core.rs:225
  50: tokio::runtime::task::harness::poll_future::{{closure}}::ha5ab641bd96efa5a
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:422
  51: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h065d88ad4f5dc60c
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:344
  52: std::panicking::try::do_call::h2eb0f4886ca3bdbb
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:379
  53: __rust_try<unknown>
      at <unknown source file>:<unknown line>
  54: std::panicking::try::hbb4cfeec1dc28708
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:343
  55: std::panic::catch_unwind::hdd5271a639881957
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:431
  56: tokio::runtime::task::harness::poll_future::hc563bdf8359f97c2
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:409
  57: tokio::runtime::task::harness::Harness<T,S>::poll_inner::hb37e88a0877c259b
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:89
  58: tokio::runtime::task::harness::Harness<T,S>::poll::h86e42c8cdb86c2dd
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/harness.rs:59
  59: tokio::runtime::task::raw::poll::ha7069ac1b15b53d1
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/raw.rs:104
  60: tokio::runtime::task::raw::RawTask::poll::hed609415f0ff56a8
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/raw.rs:66
  61: tokio::runtime::task::Notified<S>::run::h02cc616ec0a028b1
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/task/mod.rs:171
  62: tokio::runtime::blocking::pool::Inner::run::h7271eff39588245b
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/blocking/pool.rs:278
  63: tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}::hf2e6fe6b5b5dde64
      at /home/paulg/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/blocking/pool.rs:258
  64: std::sys_common::backtrace::__rust_begin_short_backtrace::h688632955e6aab57
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:125
  65: std::thread::Builder::spawn_unchecked::{{closure}}::{{closure}}::h9a1f51fc37ce796f
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:474
  66: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::ha8ae9f82843f73dd
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:344
  67: std::panicking::try::do_call::h166c84dae4ac5ae8
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:379
  68: __rust_try<unknown>
      at <unknown source file>:<unknown line>
  69: std::panicking::try::he0897c5f005d24ee
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:343
  70: std::panic::catch_unwind::h33a1208768cd6c00
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:431
  71: std::thread::Builder::spawn_unchecked::{{closure}}::h7627bfe3adc0f1bc
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:473
  72: core::ops::function::FnOnce::call_once{{vtable.shim}}::h2fadb9f24cb4c7bc
      at /home/paulg/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227
  73: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hdc51fe7e73bc86bf
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/alloc/src/boxed.rs:1546
  74: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he605738a76b56d9d
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/alloc/src/boxed.rs:1546
  75: std::sys::unix::thread::Thread::new::thread_start::he44b12fd83e74919
      at /rustc/94736c434ee154b30e2ec22ec112b79e3f6c5884/library/std/src/sys/unix/thread.rs:71
  76: start_thread<unknown>
      at <unknown source file>:<unknown line>
  77: clone<unknown>
      at <unknown source file>:<unknown line>

the code is available here: https://github.com/PaulGrandperrin/cachou/blob/crash_in_sqlx/server/src/db/sql.rs#L166

@PaulGrandperrin
Copy link
Author

changing

        if db_version != version { 
            return Err(api::Error::VersionConflict(db_version, version));
        };

to

        if db_version != version {
            drop(tx); // <-------- added line 
            return Err(api::Error::VersionConflict(db_version, version));
        };

leads to the same panic.

but changing it to:

        if db_version != version { 
            tx.rollback().await.map_err(|e| api::Error::ServerSideError(e.into()))?;
            return Err(api::Error::VersionConflict(db_version, version));
        };

works.

So manually rollbacking the transaction at every failure code path can be a temporary workaround.

@HirbodBehnam
Copy link

Hello
I'm a novice rust programmer and I ran into this issue as well. My workaround was a small macro which checked a Result and rolled back the transaction if it had an error.
Here is the macro definition:

macro_rules! try_rollback {
    ($tx:expr,$result:expr) => {{
        if let Err(err) = $result {
            let _ = $tx.rollback().await; // you can handle this error if you like
            return Err(err.into());
        }
    }};
}

And here is how I use it:

let mut tx = conn.begin().await?;
try_rollback!(tx, Database::my_function(&mut tx, id).await);

@abonander
Copy link
Collaborator

Closed by #1439

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

No branches or pull requests

3 participants