Skip to content

[MySQL] Panic caused by dropping a transaction #1358

Closed
@Kinrany

Description

@Kinrany

Repo with a minimal reproducible demo: https://github.com/Kinrany/sqlx_transaction_panic

Dropping a transaction causes a panic in a worker thread.

Only happens with a connection pool and the fetch_one method.

Possibly related: #1078

Code

  • Uses a MySQL connection pool
  • Runs "SELECT 1" in a transaction
  • Explicitly drops the transaction, intending to rollback
  • Waits two seconds, to make sure there's time for the panic to happen
// main.rs
use sqlx::mysql::MySqlPoolOptions;
use std::time::Duration;

#[tokio::main]
async fn main() {
    let conn = MySqlPoolOptions::new()
        .connect(&std::env::var("DATABASE_URL").unwrap())
        .await
        .unwrap();

    let handle = tokio::spawn(tokio::time::sleep(Duration::from_secs(2)));

    let mut tx = conn.begin().await.unwrap();
    sqlx::query("SELECT 1").fetch_one(&mut tx).await.unwrap();
    drop(tx);

    handle.await.unwrap();
}

Output

kinrany:~/projects/sqlx_transaction_panic$ DATABASE_URL="mysql://root:example@localhost/Db" cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/sqlx_transaction_panic`
thread 'tokio-runtime-worker' panicked at 'assertion failed: self.remaining() >= dst.len()', /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.0.1/src/buf/buf_impl.rs:250:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
With backtrace:
kinrany:~/projects/sqlx_transaction_panic$ DATABASE_URL="mysql://root:example@localhost/Db" RUST_BACKTRACE=1 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/sqlx_transaction_panic`
thread 'tokio-runtime-worker' panicked at 'assertion failed: self.remaining() >= dst.len()', /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.0.1/src/buf/buf_impl.rs:250:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:515:5
   1: core::panicking::panic_fmt
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:92:14
   2: core::panicking::panic
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:50:5
   3: bytes::buf::buf_impl::Buf::copy_to_slice
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.0.1/src/buf/buf_impl.rs:250:9
   4: bytes::buf::buf_impl::Buf::get_u64_le
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.0.1/src/buf/buf_impl.rs:511:9
   5: <bytes::bytes::Bytes as sqlx_core::mysql::io::buf::MySqlBufExt>::get_uint_lenenc
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.5/src/mysql/io/buf.rs:25:21
   6: sqlx_core::mysql::connection::stream::MySqlStream::skip_result_metadata::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.5/src/mysql/connection/stream.rs:185:32
   7: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/future/mod.rs:80:19
   8: sqlx_core::mysql::connection::stream::MySqlStream::wait_until_ready::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.5/src/mysql/connection/stream.rs:109:21
   9: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/future/mod.rs:80:19
  10: <sqlx_core::mysql::connection::MySqlConnection as sqlx_core::connection::Connection>::ping::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.5/src/mysql/connection/mod.rs:61:13
  11: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/future/mod.rs:80:19
  12: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/future/future.rs:119:9
  13: <sqlx_core::pool::connection::PoolConnection<DB> as core::ops::drop::Drop>::drop::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.5/src/pool/connection.rs:83:33
  14: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/future/mod.rs:80:19
  15: tokio::runtime::task::core::CoreStage<T>::poll::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/core.rs:147:17
  16: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/loom/std/unsafe_cell.rs:14:9
  17: tokio::runtime::task::core::CoreStage<T>::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/core.rs:137:13
  18: tokio::runtime::task::harness::poll_future::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:437:23
  19: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panic.rs:347:9
  20: std::panicking::try::do_call
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:401:40
  21: __rust_try
  22: std::panicking::try
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:365:19
  23: std::panic::catch_unwind
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panic.rs:434:14
  24: tokio::runtime::task::harness::poll_future
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:424:19
  25: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:89:9
  26: tokio::runtime::task::harness::Harness<T,S>::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:59:15
  27: tokio::runtime::task::raw::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/raw.rs:113:5
  28: tokio::runtime::task::raw::RawTask::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/raw.rs:70:18
  29: tokio::runtime::task::Notified<S>::run
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/mod.rs:166:9
  30: tokio::runtime::thread_pool::worker::Context::run_task::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/thread_pool/worker.rs:396:13
  31: tokio::coop::with_budget::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/coop.rs:106:9
  32: std::thread::local::LocalKey<T>::try_with
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/thread/local.rs:400:16
  33: std::thread::local::LocalKey<T>::with
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/thread/local.rs:376:9
  34: tokio::coop::with_budget
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/coop.rs:99:5
  35: tokio::coop::budget
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/coop.rs:76:5
  36: tokio::runtime::thread_pool::worker::Context::run_task
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/thread_pool/worker.rs:395:9
  37: tokio::runtime::thread_pool::worker::Context::run
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/thread_pool/worker.rs:365:24
  38: tokio::runtime::thread_pool::worker::run::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/thread_pool/worker.rs:350:17
  39: tokio::macros::scoped_tls::ScopedKey<T>::set
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/macros/scoped_tls.rs:61:9
  40: tokio::runtime::thread_pool::worker::run
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/thread_pool/worker.rs:347:5
  41: tokio::runtime::thread_pool::worker::Launch::launch::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/thread_pool/worker.rs:326:45
  42: <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/blocking/task.rs:42:21
  43: tokio::runtime::task::core::CoreStage<T>::poll::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/core.rs:147:17
  44: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/loom/std/unsafe_cell.rs:14:9
  45: tokio::runtime::task::core::CoreStage<T>::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/core.rs:137:13
  46: tokio::runtime::task::harness::poll_future::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:437:23
  47: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panic.rs:347:9
  48: std::panicking::try::do_call
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:401:40
  49: __rust_try
  50: std::panicking::try
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:365:19
  51: std::panic::catch_unwind
             at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panic.rs:434:14
  52: tokio::runtime::task::harness::poll_future
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:424:19
  53: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:89:9
  54: tokio::runtime::task::harness::Harness<T,S>::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/harness.rs:59:15
  55: tokio::runtime::task::raw::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/raw.rs:113:5
  56: tokio::runtime::task::raw::RawTask::poll
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/raw.rs:70:18
  57: tokio::runtime::task::Notified<S>::run
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/task/mod.rs:166:9
  58: tokio::runtime::blocking::pool::Inner::run
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/blocking/pool.rs:265:17
  59: tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}
             at /home/kinrany/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.9.0/src/runtime/blocking/pool.rs:245:17
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions