dimforge / rapier

2D and 3D physics engines focused on performance.
https://rapier.rs
Apache License 2.0
3.77k stars 235 forks source link

Crash with `attempt to subtract with overflow` in `SAPRegion::update_after_subregion_removal()` #593

Open ppykalo opened 4 months ago

ppykalo commented 4 months ago

I am using rapier 0.18.0 through bevy_rapier2d 0.24.0 and noticed occasional attempt to subtract with overflow in update_after_subregion_removal() on subtract from self.subproper_proxy_count. I cloned rapier repo, added some debug and turns out that axis.delete_deleted_proxies_and_endpoints_after_subregion_removal() call returns 1 but self.subproper_proxy_count equals 0 so subtraction causes panic in debug build.

As quick workaround I changed the subtraction to self.subproper_proxy_count = self.subproper_proxy_count.saturating_sub(num_deleted_proxies) but it's out of my rapier knowledge to fix this properly.

I'm attaching log containing debug outputs and stack backtrace + diff with changes I made. I know how to reproduce this problem (~50% chance) with current state of my project so it's possible to provide additional logs if needed. log.txt diff.txt

feroldi commented 3 months ago

I'm having this exact problem as well.

DiSaber commented 2 weeks ago

I'm also having this issue but with rapier 0.19.0 and bevy_rapier3d 0.26.0. I managed to trigger the panic by manually setting the position of the transform attached to my player with the rigidbody. I still sometimes have issues reproducing it but my setup is basically an empty world where the player position and velocity are reset to Vec3::ZERO if the Y coordinate passes below -10.

Panic message for reference:

thread 'Compute Task Pool (11)' panicked at C:\Users\{username}\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rapier3d-0.19.0\src\geometry\broad_phase_multi_sap\sap_region.rs:195:17:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_rapier3d::plugin::systems::step_simulation<()>`!
Encountered a panic in system `bevy_app::main_schedule::FixedMain::run_fixed_main`!
Encountered a panic in system `bevy_time::fixed::run_fixed_main_schedule`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!