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

Server panic on player disconnect #151

Closed
jivvy opened this issue Nov 8, 2022 · 4 comments
Closed

Server panic on player disconnect #151

jivvy opened this issue Nov 8, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@jivvy
Copy link
Contributor

jivvy commented Nov 8, 2022

Can be reproduced with the combat example:

  1. Connect two clients to the combat example
  2. Disconnect one of them
Backtrace
thread '<unnamed>' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/context.rs:56:13
stack backtrace:
   0: rust_begin_unwind
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/core/src/panicking.rs:65:14
   2: core::panicking::panic_display
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/core/src/panicking.rs:139:5
   3: core::panicking::panic_str
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/core/src/panicking.rs:123:5
   4: core::option::expect_failed
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/core/src/option.rs:1876:5
   5: core::option::Option<T>::expect
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/context.rs:56:13
   6: tokio::runtime::context::time_handle::{{closure}}
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/context.rs:56:13
   7: std::thread::local::LocalKey<T>::try_with
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/thread/local.rs:446:16
   8: tokio::runtime::context::time_handle
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/context.rs:54:15
   9: tokio::time::driver::handle::Handle::current
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/time/driver/handle.rs:58:13
  10: tokio::time::driver::sleep::Sleep::new_timeout
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/time/driver/sleep.rs:260:22
  11: tokio::time::timeout::timeout
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/time/timeout.rs:79:27
  12: <valence::server::packet_controller::PlayPacketController as core::ops::drop::Drop>::drop
             at /home/jivvy/.cargo/git/checkouts/valence-a175964027d82956/43435b8/src/server/packet_controller.rs:218:30
  13: core::ptr::drop_in_place<valence::server::packet_controller::PlayPacketController>
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/core/src/ptr/mod.rs:491:1
  14: valence::client::Client<C>::handle_serverbound_packets
             at /home/jivvy/.cargo/git/checkouts/valence-a175964027d82956/43435b8/src/client.rs:911:9
  15: valence::server::do_update_loop::{{closure}}
             at /home/jivvy/.cargo/git/checkouts/valence-a175964027d82956/43435b8/src/server.rs:407:13
  16: <rayon::iter::for_each::ForEachConsumer<F> as rayon::iter::plumbing::Folder<T>>::consume
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/for_each.rs:47:9
  17: <rayon::iter::map::MapFolder<C,F> as rayon::iter::plumbing::Folder<T>>::consume
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/map.rs:239:19
  18: <rayon::iter::map::MapFolder<C,F> as rayon::iter::plumbing::Folder<T>>::consume
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/map.rs:239:19
  19: <rayon::iter::filter_map::FilterMapFolder<C,P> as rayon::iter::plumbing::Folder<T>>::consume
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/filter_map.rs:124:24
  20: rayon::iter::plumbing::Folder::consume_iter
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/plumbing/mod.rs:179:20
  21: rayon::iter::plumbing::Producer::fold_with
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/plumbing/mod.rs:110:9
  22: rayon::iter::plumbing::bridge_producer_consumer::helper
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/plumbing/mod.rs:438:13
  23: rayon::iter::plumbing::bridge_producer_consumer::helper::{{closure}}
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.5.3/src/iter/plumbing/mod.rs:418:21
  24: rayon_core::join::join_context::call_a::{{closure}}
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/join/mod.rs:124:17
  25: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/core/src/panic/unwind_safe.rs:271:9
  26: std::panicking::try::do_call
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/panicking.rs:483:40
  27: std::panicking::try
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/panicking.rs:447:19
  28: std::panic::catch_unwind
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/panic.rs:137:14
  29: rayon_core::unwind::halt_unwinding
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/unwind.rs:17:5
  30: rayon_core::join::join_context::{{closure}}
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/join/mod.rs:141:24
  31: rayon_core::registry::Registry::in_worker_cold::{{closure}}::{{closure}}
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:468:21
  32: <rayon_core::job::StackJob<L,F,R> as rayon_core::job::Job>::execute::call::{{closure}}
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/job.rs:113:21
  33: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/core/src/panic/unwind_safe.rs:271:9
  34: std::panicking::try::do_call
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/panicking.rs:483:40
  35: std::panicking::try
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/panicking.rs:447:19
  36: std::panic::catch_unwind
             at /rustc/bf15a9e5263fcea065a7ae9c179b2d24c2deb670/library/std/src/panic.rs:137:14
  37: rayon_core::unwind::halt_unwinding
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/unwind.rs:17:5
  38: <rayon_core::job::StackJob<L,F,R> as rayon_core::job::Job>::execute
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/job.rs:119:38
  39: rayon_core::job::JobRef::execute
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:729:17
  40: rayon_core::registry::WorkerThread::execute
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:752:9
  41: rayon_core::registry::WorkerThread::wait_until_cold
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:729:17
  42: rayon_core::registry::WorkerThread::wait_until
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:703:13
  43: rayon_core::registry::main_loop
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:836:5
  44: rayon_core::registry::ThreadBuilder::run
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:55:18
  45: <rayon_core::registry::DefaultSpawn as rayon_core::registry::ThreadSpawn>::spawn::{{closure}}
             at /home/jivvy/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-core-1.9.3/src/registry.rs:100:20
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@rj00a rj00a added the bug Something isn't working label Nov 8, 2022
@rj00a
Copy link
Member

rj00a commented Nov 8, 2022

The Drop impl on PlayPacketController spawns a tokio task (requiring a tokio context). The PlayPacketController is dropped whenever a client is deleted. Looks like it's not in a tokio context when that happens for some reason.

@jivvy
Copy link
Contributor Author

jivvy commented Nov 9, 2022

So I started trying to investigate this

It looks like there is a tokio context up until this point:

valence/src/server.rs

Lines 405 to 408 in bb43856

// Get serverbound packets first so they are not dealt with a tick late.
server.clients.par_iter_mut().for_each(|(_, client)| {
client.handle_serverbound_packets(&server.entities);
});

If iter_mut() is used instead of par_iter_mut(), the server doesn't panic

@rj00a
Copy link
Member

rj00a commented Nov 10, 2022

Nice find. This is happening because the tokio context is thread-local, but rayon tasks run in their own thread pool. Fixed in 923fabe.

@rj00a rj00a closed this as completed Nov 10, 2022
@jivvy
Copy link
Contributor Author

jivvy commented Nov 10, 2022

Thanks for explaining this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants