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

PgListener causes a panic on-drop in async fn main() #1389

Closed
Freyskeyd opened this issue Aug 18, 2021 · 7 comments · Fixed by #1393
Closed

PgListener causes a panic on-drop in async fn main() #1389

Freyskeyd opened this issue Aug 18, 2021 · 7 comments · Fixed by #1393
Labels
bug db:postgres Related to PostgreSQL

Comments

@Freyskeyd
Copy link
Contributor

Hello,

I ran into an issue with SQLx PgListener when using it with Actix stream.

Everything works fine unless I CtrlC my application. When killing the application I expect that the application stops without panicking, but I'm facing some weird thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime'.

When my actor gets dropped he drop the listener stream which execute the drop method on the PgListener.

I suppose that the panic is due to the sqlx_rt trying to execute something on an already closed system.

I'm using rustc 1.54.0, SQLX master - 207e6db2, actix 0.12.0.

I've update the Postgres listen example on my fork to allow anyone to reproduce: https://github.com/Freyskeyd/sqlx/tree/crash_ctrlc/examples/postgres/listen

here's the panic:

^Cthread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', sqlx-core/src/postgres/listener.rs:264:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', sqlx-core/src/pool/connection.rs:77:13
stack backtrace:
   0:        0x1057d0bc4 - std::backtrace_rs::backtrace::libunwind::trace::h03e89bdd1996f493
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:        0x1057d0bc4 - std::backtrace_rs::backtrace::trace_unsynchronized::hb5af08e4de8ca785
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:        0x1057d0bc4 - std::sys_common::backtrace::_print_fmt::h4eafc628aec41041
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/sys_common/backtrace.rs:67:5
   3:        0x1057d0bc4 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h6a8908fa3ed6f9e8
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/sys_common/backtrace.rs:46:22
   4:        0x1057e94bc - core::fmt::write::h4be00f71c5582919
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/fmt/mod.rs:1110:17
   5:        0x1057cc71a - std::io::Write::write_fmt::h49e76926070788f1
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/io/mod.rs:1588:15
   6:        0x1057d29bf - std::sys_common::backtrace::_print::h2f7b8ad2bea4f859
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/sys_common/backtrace.rs:49:5
   7:        0x1057d29bf - std::sys_common::backtrace::print::h97402070cfceb1a3
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/sys_common/backtrace.rs:36:9
   8:        0x1057d29bf - std::panicking::default_hook::{{closure}}::h1577f0656e419c0e
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:208:50
   9:        0x1057d24bd - std::panicking::default_hook::h1aef594179c4fd25
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:225:9
  10:        0x1057d30c0 - std::panicking::rust_panic_with_hook::h10bc487d002f6c42
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:622:17
  11:        0x1057d2b65 - std::panicking::begin_panic_handler::{{closure}}::hf4cfa78c105ce648
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:519:13
  12:        0x1057d1038 - std::sys_common::backtrace::__rust_end_short_backtrace::h1df96a166e4351c4
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/sys_common/backtrace.rs:141:18
  13:        0x1057d2aca - rust_begin_unwind
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:515:5
  14:        0x1057f94cf - core::panicking::panic_fmt::hea8fe6c9e0720810
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/panicking.rs:92:14
  15:        0x1057f93da - core::option::expect_failed::h2f35a999a864e306
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/option.rs:1243:5
  16:        0x1052e7bbb - core::option::Option<T>::expect::h3c109e1a3f68946b
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/option.rs:351:21
  17:        0x1053475c2 - tokio::task::spawn::spawn::h5a96a92310563012
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/task/spawn.rs:132:28
  18:        0x10539bb19 - <sqlx_core::pool::connection::PoolConnection<DB> as core::ops::drop::Drop>::drop::ha5891ffd2cd53079
                               at /opensource/sqlx/sqlx-core/src/pool/connection.rs:77:13
  19:        0x10538bda5 - core::ptr::drop_in_place<sqlx_core::pool::connection::PoolConnection<sqlx_core::postgres::database::Postgres>>::h3353a43a49bfb765
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  20:        0x10538d542 - core::ptr::drop_in_place<<sqlx_core::postgres::listener::PgListener as core::ops::drop::Drop>::drop::{{closure}}>::h100ea8085d10b175
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:264:39
  21:        0x105390b51 - core::ptr::drop_in_place<core::future::from_generator::GenFuture<<sqlx_core::postgres::listener::PgListener as core::ops::drop::Drop>::drop::{{closure}}>>::h9dfa2289234a905e
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  22:        0x105347555 - tokio::task::spawn::spawn::h361ebf3a54aeb73f
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/task/spawn.rs:136:5
  23:        0x10539bd4e - <sqlx_core::postgres::listener::PgListener as core::ops::drop::Drop>::drop::hf523117dfdc00f76
                               at /opensource/sqlx/sqlx-core/src/postgres/listener.rs:264:13
  24:        0x105395de5 - core::ptr::drop_in_place<sqlx_core::postgres::listener::PgListener>::ha3b2f1f652c0b8bd
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  25:        0x10538d264 - core::ptr::drop_in_place<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}}::{{closure}}>::hee35592a83d2ccd2
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:71:9
  26:        0x105390641 - core::ptr::drop_in_place<core::future::from_generator::GenFuture<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}}::{{closure}}>>::haa185195e156cf19
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  27:        0x1053950c9 - core::ptr::drop_in_place<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>::new<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}},core::future::from_generator::GenFuture<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}}::{{closure}}>>::{{closure}}>::h23817b8ee73d8ccd
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:28:33
  28:        0x105395601 - core::ptr::drop_in_place<core::future::from_generator::GenFuture<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>::new<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}},core::future::from_generator::GenFuture<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}}::{{closure}}>>::{{closure}}>>::h3bb35c9d6fc19158
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  29:        0x1053956ef - core::ptr::drop_in_place<core::option::Option<core::future::from_generator::GenFuture<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>::new<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}},core::future::from_generator::GenFuture<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}}::{{closure}}>>::{{closure}}>>>::h1386245c122e5ee6
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  30:        0x105395781 - core::ptr::drop_in_place<futures_util::future::future::fuse::Fuse<core::future::from_generator::GenFuture<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>::new<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}},core::future::from_generator::GenFuture<sqlx_core::postgres::listener::PgListener::into_stream::{{closure}}::{{closure}}>>::{{closure}}>>>::h76afc58f891fb010
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  31:        0x10539109f - core::ptr::drop_in_place<alloc::boxed::Box<dyn core::future::future::Future+Output = core::result::Result<(),sqlx_core::error::Error>+core::marker::Send>>::hb0433ed72663e834
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  32:        0x105391741 - core::ptr::drop_in_place<core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future+Output = core::result::Result<(),sqlx_core::error::Error>+core::marker::Send>>>::h5a58b0fde7b9b0af
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  33:        0x1052afd74 - core::ptr::drop_in_place<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>>::h8d147d3bf8a35aa0
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  34:        0x1052b05a8 - core::ptr::drop_in_place<alloc::boxed::Box<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>>>::he32a92ac5ee9def7
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  35:        0x1052b0d01 - core::ptr::drop_in_place<core::pin::Pin<alloc::boxed::Box<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>>>>::h1b72d8eb43d43511
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  36:        0x1052b1431 - core::ptr::drop_in_place<actix::stream::ActorStream<core::pin::Pin<alloc::boxed::Box<sqlx_core::ext::async_stream::TryAsyncStream<sqlx_core::postgres::listener::PgNotification>>>>>::h1dc6a6b88b0412bb
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  37:        0x1052b102f - core::ptr::drop_in_place<alloc::boxed::Box<dyn actix::fut::future::ActorFuture<sqlx_example_postgres_listen::ListenerActor>+Output = ()>>::h57df8a548ecb9c96
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  38:        0x1052b13f1 - core::ptr::drop_in_place<core::pin::Pin<alloc::boxed::Box<dyn actix::fut::future::ActorFuture<sqlx_example_postgres_listen::ListenerActor>+Output = ()>>>::hd6baeff122aeb17f
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  39:        0x1052b1715 - core::ptr::drop_in_place<(actix::actor::SpawnHandle,core::pin::Pin<alloc::boxed::Box<dyn actix::fut::future::ActorFuture<sqlx_example_postgres_listen::ListenerActor>+Output = ()>>)>::h8b4d005fb167894e
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  40:        0x1052b18d0 - core::ptr::drop_in_place<[(actix::actor::SpawnHandle,core::pin::Pin<alloc::boxed::Box<dyn actix::fut::future::ActorFuture<sqlx_example_postgres_listen::ListenerActor>+Output = ()>>)]>::hdac455b5d518b02d
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  41:        0x1052b3f7e - <smallvec::SmallVec<A> as core::ops::drop::Drop>::drop::h753aa409816189c7
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-1.6.1/src/lib.rs:1716:17
  42:        0x1052b1fa1 - core::ptr::drop_in_place<smallvec::SmallVec<[(actix::actor::SpawnHandle,core::pin::Pin<alloc::boxed::Box<dyn actix::fut::future::ActorFuture<sqlx_example_postgres_listen::ListenerActor>+Output = ()>>); 3]>>::hed2364a0355e0cc2
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  43:        0x1052b1201 - core::ptr::drop_in_place<actix::contextimpl::ContextFut<sqlx_example_postgres_listen::ListenerActor,actix::context::Context<sqlx_example_postgres_listen::ListenerActor>>>::h8baabb5a18d0d398
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  44:        0x1052b15fd - core::ptr::drop_in_place<tokio::runtime::task::core::Stage<actix::contextimpl::ContextFut<sqlx_example_postgres_listen::ListenerActor,actix::context::Context<sqlx_example_postgres_listen::ListenerActor>>>>::h0bd7dcbccbd07960
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  45:        0x105283699 - tokio::runtime::task::core::CoreStage<T>::set_stage::{{closure}}::hcc8ceb7d3639ce3d
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/core.rs:288:35
  46:        0x10529d190 - tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut::h2ac5fd06046d2646
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/loom/std/unsafe_cell.rs:14:9
  47:        0x10528347a - tokio::runtime::task::core::CoreStage<T>::set_stage::h0b621ce748f9082a
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/core.rs:288:9
  48:        0x105282ef6 - tokio::runtime::task::core::CoreStage<T>::drop_future_or_output::h414c088076e094aa
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/core.rs:254:13
  49:        0x105273377 - tokio::runtime::task::harness::cancel_task::{{closure}}::h1e09f95a6e2d8c48
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/harness.rs:387:9
  50:        0x1052af3e5 - core::ops::function::FnOnce::call_once::hfb322d125261dc15
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ops/function.rs:227:5
  51:        0x10528f2c1 - <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hfc68792752503ebe
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panic.rs:347:9
  52:        0x1052abdfa - std::panicking::try::do_call::h2e9b8e58c513c7e6
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:401:40
  53:        0x1052ac33b - ___rust_try
  54:        0x1052ab9d2 - std::panicking::try::h21eea6d9a508f199
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:365:19
  55:        0x10528f3d1 - std::panic::catch_unwind::hc76d0e2839857bec
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panic.rs:434:14
  56:        0x105273075 - tokio::runtime::task::harness::cancel_task::h5de85ae161d2187a
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/harness.rs:386:15
  57:        0x105276408 - tokio::runtime::task::harness::Harness<T,S>::shutdown::h6f782ccf6bf9e7d2
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/harness.rs:163:19
  58:        0x1052aa6d2 - tokio::runtime::task::raw::shutdown::h96d91db8e46cf987
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/raw.rs:130:5
  59:        0x1056bde4f - tokio::runtime::task::raw::RawTask::shutdown::h458e9113fa2c4997
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/raw.rs:90:18
  60:        0x105721254 - tokio::runtime::task::Task<S>::shutdown::h9c4eff53ec88c915
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/runtime/task/mod.rs:164:9
  61:        0x105723597 - <tokio::task::local::LocalSet as core::ops::drop::Drop>::drop::{{closure}}::he63cd7d2cc35962a
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/task/local.rs:600:17
  62:        0x1056b8cb8 - tokio::macros::scoped_tls::ScopedKey<T>::set::h060dca12118192c4
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/macros/scoped_tls.rs:61:9
  63:        0x105723462 - tokio::task::local::LocalSet::with::h40cc05681b79dc77
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/task/local.rs:548:9
  64:        0x105723491 - <tokio::task::local::LocalSet as core::ops::drop::Drop>::drop::he8775c1495209492
                               at /tmp/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.0/src/task/local.rs:590:9
  65:        0x1056a78d5 - core::ptr::drop_in_place<tokio::task::local::LocalSet>::h67783a68285cea14
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  66:        0x1056a7805 - core::ptr::drop_in_place<actix_rt::runtime::Runtime>::h1a9355317cc36ae7
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  67:        0x1052b2915 - core::ptr::drop_in_place<actix_rt::system::SystemRunner>::h543199ccb33da322
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ptr/mod.rs:192:1
  68:        0x10528ef0d - sqlx_example_postgres_listen::main::h4118475ea6291b6d
                               at /opensource/sqlx/examples/postgres/listen/src/main.rs:16:14
  69:        0x1052af32e - core::ops::function::FnOnce::call_once::h69944caaaada83c4
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ops/function.rs:227:5
  70:        0x105281551 - std::sys_common::backtrace::__rust_begin_short_backtrace::h6c9e6a0b419876d9
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/sys_common/backtrace.rs:125:18
  71:        0x105292f44 - std::rt::lang_start::{{closure}}::ha6852f98ae65ce3a
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/rt.rs:49:18
  72:        0x1057d35e1 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h7adc4e20a318e389
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/core/src/ops/function.rs:259:13
  73:        0x1057d35e1 - std::panicking::try::do_call::h687af85ae86a6e14
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:401:40
  74:        0x1057d35e1 - std::panicking::try::hf74f2790da0186f3
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panicking.rs:365:19
  75:        0x1057d35e1 - std::panic::catch_unwind::hd14a420ea313edb8
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/panic.rs:434:14
  76:        0x1057d35e1 - std::rt::lang_start_internal::h4428f22d05a79c62
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/rt.rs:34:21
  77:        0x105292f1e - std::rt::lang_start::h343b11da32c54d7f
                               at /rustc/a178d0322ce20e33eac124758e837cbd80a6f633/library/std/src/rt.rs:48:5
  78:        0x10528ef46 - _main
thread panicked while panicking. aborting.
zsh: illegal hardware instruction  cargo run

Feel free to ask for more details if needed !

Thank's a lot !

@abonander
Copy link
Collaborator

abonander commented Aug 18, 2021

I think I understand why this is happening and it doesn't have anything to do with the ctrl_c handler.

The issue is that PgListener spawns a task on-drop, which normally wouldn't be an issue as the runtime hasn't gone away yet when it is dropped.

However, that task contains a PoolConnection which also spawns a task on-drop, which is a problem because when the task spawned by PgListener is being cleaned up, in this case it means the runtime is going away and is no longer set as the current runtime. And so we get a panic.

Unfortunately the fix for this would be AsyncDrop which isn't a thing yet.

However, another potential fix is to not spawn a task in the Drop for PgListener but instead just have it set an internal flag on the connection telling it to execute UNLISTEN *; before the next time it's used. That would make it work both when it's internally using a &mut PgConnection and a PoolConnection. However, that may lead to the server queuing up a lot of notifications that we just end up throwing away.

Alternatively, we could inline the async code from PoolConnection's drop handler into PgListener's drop handler and only spawn a single task overall. I think I like that option better as it can execute the UNLISTEN *; ASAP, although we may want to do both approaches cause I don't think we do UNLISTEN *; at all when we're using a connection by-reference.

In the meantime, what should prevent this panic from happening is calling pool.close().await?; just before returning from main() which will keep the runtime going until the PgListener and its underlying PoolConnection are gracefully closed. However, you also need to send a stop message to your actor or else it'll keep going indefinitely, which you can do like this:

#[derive(actix::Message)]
#[rtype("()")]
struct StopMessage;

impl Handler<StopMessage> for ListenerActor {
    fn handle(&mut self, _: StopMessage, ctx: &mut Context<Self>) -> () {
        ctx.stop();
    }
}

// then in `main()`
let addr = ListenerActor::create(|ctx| ...);

actix_rt::signal::ctrl_c().await;

addr.send(StopMessage).await?;

pool.close().await?;

Ok(())

@abonander abonander changed the title Postgres Listener crashes when using ctrlc_c don't spawn a task in PgListener::drop() Aug 18, 2021
@abonander abonander changed the title don't spawn a task in PgListener::drop() PgListener causes a panic on-drop in async fn main() Aug 18, 2021
@abonander abonander added bug db:postgres Related to PostgreSQL labels Aug 18, 2021
@Freyskeyd
Copy link
Contributor Author

Thank's for this explanation !

Could we have the UNLISTEN *; executed when the connection returns to the pool?

Anyway in my use case I can't send a StopMessage and close the pool because I don't have access to the underlying actors / pool which are dynamically created...

How can we iterate over a solution for this bug tho?

@abonander
Copy link
Collaborator

I've opened #1393 with a fix, I forgot PgListener doesn't have the option to take a connection by-reference so spawning a task to execute UNLISTEN *; is the easiest option and then inlining the async code in PoolConnection::drop() so it doesn't try to spawn another task.

@Freyskeyd
Copy link
Contributor Author

Hello and thank's for this PR.

Anyway, I tried with master and I keep having the same error when kill the application.
The listener doesn't have access to a runtime when the Drop occurs.

@Freyskeyd
Copy link
Contributor Author

@abonander I proposed a new update on the Drop method: #1395

@cortopy
Copy link

cortopy commented Oct 1, 2021

I think #1395 is what did it for me. Just tried with git/main and error is gone.

Could it be released please?

@Freyskeyd
Copy link
Contributor Author

Hello @cortopy

I think it will be released in the next minor sooner :)

Tell me if I'm wrong @abonander !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug db:postgres Related to PostgreSQL
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants