dimforge / bevy_rapier

Official Rapier plugin for the Bevy game engine.
https://rapier.rs
Apache License 2.0
1.27k stars 259 forks source link

Panic handling collision events when despawning entities using TimestepMode Interpolated #285

Closed briansemrau closed 3 months ago

briansemrau commented 1 year ago

This issue happens rarely.

Version details from Cargo.toml:

[dependencies]
bevy = { version = "0.9.0", default-features = false, features = ["bevy_asset", "bevy_gilrs", "bevy_scene", "bevy_winit", "x11", "render", "png", "filesystem_watcher"] }
bevy_asset_loader = { version = "0.14.1", features = ["stageless"] }
bevy_kira_audio = { version = "0.13.0", features = ["ogg"] }
bevy_rapier2d = { version = "0.19.0", features = ["enhanced-determinism", "debug-render"] }
rand = "0.8.5"
smallvec = "1.10.0"

[profile.dev.package.bevy_rapier2d]
opt-level = 3

My game consists of a pile of blocks and a click-drag angry birds style projectile. The projectile destroys itself after a few seconds.

All the blocks have ActiveEvents::CONTACT_FORCE_EVENTS | ActiveEvents::COLLISION_EVENTS

thread 'Compute Task Pool (13)' panicked at 'Internal error: entity not found for collision event.', C:\Users\brian\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_rapier2d-0.19.0\src\pipeline\events.rs:58:9
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\std\src\panicking.rs:584
   1: core::panicking::panic_fmt
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\panicking.rs:142
   2: core::panicking::panic_display<str>
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\panicking.rs:72
   3: core::panicking::panic_str
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\panicking.rs:56
   4: core::option::expect_failed
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\option.rs:1880
   5: wgpu_core::device::Device<A>::dispose
   6: <bevy_rapier2d::pipeline::events::EventQueue as rapier2d::pipeline::event_handler::EventHandler>::handle_collision_event
   7: rapier2d::geometry::narrow_phase::NarrowPhase::handle_user_changes
   8: rapier2d::pipeline::physics_pipeline::PhysicsPipeline::new
   9: rapier2d::pipeline::physics_pipeline::PhysicsPipeline::step
  10: bevy_rapier2d::plugin::context::RapierContext::step_simulation
  11: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
  12: <bevy_ecs::system::function_system::FunctionSystem<In,Out,Param,Marker,F> as bevy_ecs::system::system::System>::run_unsafe
  13: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
  14: async_task::raw::RawTask<F,T,S>::run
  15: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
  16: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
  17: std::thread::local::LocalKey<T>::with
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
The rest of the error output: ``` thread 'main' panicked at 'task has failed', C:\Users\brian\.cargo\registry\src\github.com-1ecc6299db9ec823\async-task-4.3.0\src\task.rs:426:43 stack backtrace: 0: std::panicking::begin_panic_handler at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\std\src\panicking.rs:584 1: core::panicking::panic_fmt at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\panicking.rs:142 2: core::panicking::panic_display at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\panicking.rs:72 3: core::panicking::panic_str at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\panicking.rs:56 4: core::option::expect_failed at /rustc/897e37553bba8b42751c67658967889d11ecd120/library\core\src\option.rs:1880 5: as core::future::future::Future>::poll 6: as core::future::future::Future>::poll 7: as core::future::future::Future>::poll 8: async_task::raw::RawTask::run 9: async_executor::LocalExecutor::try_tick 10: bevy_tasks::task_pool::TaskPool::scope 11: ::run_systems 12: ::run 13: bevy_ecs::schedule::Schedule::run_once 14: bevy_app::app::App::update 15: winit::platform_impl::platform::event_loop::EventLoop::run_return::{{closure}} 16: as core::ops::function::FnOnce<()>>::call_once 17: winit::platform_impl::platform::event_loop::runner::EventLoopRunner::register_window 18: winit::platform_impl::platform::event_loop::runner::EventLoopRunner::move_state_to 19: as core::ops::function::FnOnce<()>>::call_once 20: winit::platform_impl::platform::event_loop::runner::EventLoopRunner::catch_unwind 21: winit::platform_impl::platform::event_loop::EventLoopThreadExecutor::execute_in_thread 22: CallWindowProcW 23: DispatchMessageW 24: SendMessageTimeoutW 25: KiUserCallbackDispatcher 26: NtUserDispatchMessage 27: DispatchMessageW 28: winit::platform_impl::platform::event_loop::EventLoop::run_return 29: winit::platform_impl::platform::event_loop::EventLoop::run 30: core::ptr::drop_in_place> 31: ::build 32: bevy_winit::winit_runner_with 33: core::ops::function::Fn::call 34: bevy_app::app::App::run 35: ::component_ids note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: process didn't exit successfully: `target\release\bevy_game_1.exe` (exit code: 101) ```
MJohnson459 commented 1 year ago

This is happening consistently for me when I despawn say 10 entities at a time. I played about to see what changed and it looks like this happens when the TimestepMode is Interpolated but doesn't happen when it is Variable. I suppose a system that should run every frame isn't being run.

JLerxky commented 1 year ago

+1

AnthonyTornetta commented 1 year ago

This is kinda fixed in https://github.com/dimforge/bevy_rapier/pull/328 -- I wasn't sure why systems weren't running properly, so I prevent the crash from being generated from dead entities. I don't think this will skip over any events, but I'm not 100% sure.

OwlyCode commented 1 year ago

I'm encountering this issue as well. TimestepMode::Variable doesn't help.

GrantMoyer commented 11 months ago

I see (probably) this issue only when I despawn an entity after I change the RigidBody component of the entity. I'm struggling to produce a succinct reproducer though.