bevy
bevy copied to clipboard
Fix panicking on another scope
Objective
Fix #6453. Fix #6603.
Solution
Use the solution mentioned in the issue by catching the unwind and dropping the error. Wrap the executor.try_tick calls with std::catch::unwind.
Ideally this would be moved outside of the hot loop, but the mut ref to the spawned future is not UnwindSafe.
This PR only addresses the bug, we can address the perf issues (should there be any) later.
FYI I tried to move it out of the hot loop and haven't figured out a way yet that works. Wrapping executor.run(local_executor.tick().or(get_results) with a catch_unwind doesn't work. A key piece of this pr is that the scope keeps executing after the panic. Wrapping the whole loop in a catch_unwind would also not work for the same reason. It would break the loop and execution would not continue.
Edit: figured out there is a async catch unwind. will try that and see if that works.
We should probably benchmark this. I don't think the extra overhead of catch_unwind is all that big of a deal, even in a hot loop like this.
small regression on many foxes

busy systems sees some random regressions and contrived sees a small consistent regression. empty systems are consistently improved which seems a bit suspicious. Might be that ticking slower actually improves performance on empty systems (less contention).
group main pr
----- ----------------- ----------
busy_systems/01x_entities_03_systems 1.15 30.2±5.96µs ? ?/sec 1.00 26.3±0.38µs ? ?/sec
busy_systems/01x_entities_06_systems 1.02 48.4±0.49µs ? ?/sec 1.00 47.3±0.51µs ? ?/sec
busy_systems/01x_entities_09_systems 1.00 74.4±0.73µs ? ?/sec 1.05 78.4±3.20µs ? ?/sec
busy_systems/01x_entities_12_systems 1.01 102.0±1.37µs ? ?/sec 1.00 101.3±5.78µs ? ?/sec
busy_systems/01x_entities_15_systems 1.03 122.0±1.37µs ? ?/sec 1.00 118.0±2.96µs ? ?/sec
busy_systems/02x_entities_03_systems 1.04 50.1±0.39µs ? ?/sec 1.00 48.1±9.37µs ? ?/sec
busy_systems/02x_entities_06_systems 1.00 91.7±1.45µs ? ?/sec 1.03 94.3±13.52µs ? ?/sec
busy_systems/02x_entities_09_systems 1.11 148.5±4.04µs ? ?/sec 1.00 133.7±6.60µs ? ?/sec
busy_systems/02x_entities_12_systems 1.09 190.8±1.54µs ? ?/sec 1.00 175.6±2.66µs ? ?/sec
busy_systems/02x_entities_15_systems 1.06 231.6±3.41µs ? ?/sec 1.00 218.3±3.59µs ? ?/sec
busy_systems/03x_entities_03_systems 1.06 67.7±0.69µs ? ?/sec 1.00 64.2±0.54µs ? ?/sec
busy_systems/03x_entities_06_systems 1.00 133.1±7.55µs ? ?/sec 1.06 141.6±1.87µs ? ?/sec
busy_systems/03x_entities_09_systems 1.00 194.4±2.55µs ? ?/sec 1.02 199.0±2.03µs ? ?/sec
busy_systems/03x_entities_12_systems 1.00 265.4±5.64µs ? ?/sec 1.00 264.2±4.29µs ? ?/sec
busy_systems/03x_entities_15_systems 1.00 316.5±3.22µs ? ?/sec 1.02 323.8±4.72µs ? ?/sec
busy_systems/04x_entities_03_systems 1.00 82.7±0.83µs ? ?/sec 1.09 89.8±1.15µs ? ?/sec
busy_systems/04x_entities_06_systems 1.00 171.7±1.95µs ? ?/sec 1.05 180.8±2.85µs ? ?/sec
busy_systems/04x_entities_09_systems 1.00 250.8±3.12µs ? ?/sec 1.04 261.7±18.56µs ? ?/sec
busy_systems/04x_entities_12_systems 1.00 339.5±4.68µs ? ?/sec 1.04 354.6±8.06µs ? ?/sec
busy_systems/04x_entities_15_systems 1.00 421.3±4.59µs ? ?/sec 1.05 441.8±27.38µs ? ?/sec
busy_systems/05x_entities_03_systems 1.03 111.4±2.06µs ? ?/sec 1.00 108.3±3.08µs ? ?/sec
busy_systems/05x_entities_06_systems 1.00 220.8±2.38µs ? ?/sec 1.00 220.2±3.48µs ? ?/sec
busy_systems/05x_entities_09_systems 1.00 319.5±5.75µs ? ?/sec 1.01 322.0±2.66µs ? ?/sec
busy_systems/05x_entities_12_systems 1.00 419.2±4.23µs ? ?/sec 1.05 441.5±6.03µs ? ?/sec
busy_systems/05x_entities_15_systems 1.00 540.9±23.21µs ? ?/sec 1.02 551.0±10.23µs ? ?/sec
contrived/01x_entities_03_systems 1.00 13.6±0.30µs ? ?/sec 1.17 15.9±0.41µs ? ?/sec
contrived/01x_entities_06_systems 1.00 27.3±0.78µs ? ?/sec 1.06 29.0±0.76µs ? ?/sec
contrived/01x_entities_09_systems 1.00 41.4±0.58µs ? ?/sec 1.07 44.1±0.90µs ? ?/sec
contrived/01x_entities_12_systems 1.02 55.3±0.80µs ? ?/sec 1.00 54.4±1.62µs ? ?/sec
contrived/01x_entities_15_systems 1.00 64.4±1.03µs ? ?/sec 1.13 72.8±9.32µs ? ?/sec
contrived/02x_entities_03_systems 1.01 25.5±0.67µs ? ?/sec 1.00 25.3±1.06µs ? ?/sec
contrived/02x_entities_06_systems 1.00 47.4±1.02µs ? ?/sec 1.02 48.4±1.15µs ? ?/sec
contrived/02x_entities_09_systems 1.00 69.1±0.81µs ? ?/sec 1.02 70.7±1.34µs ? ?/sec
contrived/02x_entities_12_systems 1.00 93.1±1.14µs ? ?/sec 1.01 93.6±1.42µs ? ?/sec
contrived/02x_entities_15_systems 1.00 116.4±2.85µs ? ?/sec 1.03 120.2±2.10µs ? ?/sec
contrived/03x_entities_03_systems 1.00 33.5±1.91µs ? ?/sec 1.02 34.1±0.53µs ? ?/sec
contrived/03x_entities_06_systems 1.00 65.9±1.29µs ? ?/sec 1.01 66.8±2.83µs ? ?/sec
contrived/03x_entities_09_systems 1.00 98.8±7.07µs ? ?/sec 1.02 100.6±2.82µs ? ?/sec
contrived/03x_entities_12_systems 1.00 130.5±1.69µs ? ?/sec 1.01 131.8±1.62µs ? ?/sec
contrived/03x_entities_15_systems 1.00 159.3±1.51µs ? ?/sec 1.02 161.7±1.81µs ? ?/sec
contrived/04x_entities_03_systems 1.00 40.8±0.68µs ? ?/sec 1.02 41.4±0.56µs ? ?/sec
contrived/04x_entities_06_systems 1.00 79.6±0.94µs ? ?/sec 1.04 82.5±0.73µs ? ?/sec
contrived/04x_entities_09_systems 1.00 121.3±1.95µs ? ?/sec 1.04 125.9±10.83µs ? ?/sec
contrived/04x_entities_12_systems 1.00 159.9±2.97µs ? ?/sec 1.03 164.3±4.11µs ? ?/sec
contrived/04x_entities_15_systems 1.00 200.5±6.89µs ? ?/sec 1.02 204.0±3.24µs ? ?/sec
contrived/05x_entities_03_systems 1.00 47.9±0.97µs ? ?/sec 1.03 49.4±0.80µs ? ?/sec
contrived/05x_entities_06_systems 1.00 96.2±1.50µs ? ?/sec 1.00 96.0±1.91µs ? ?/sec
contrived/05x_entities_09_systems 1.00 142.2±3.02µs ? ?/sec 1.03 146.7±1.88µs ? ?/sec
contrived/05x_entities_12_systems 1.00 191.3±2.12µs ? ?/sec 1.01 193.4±6.07µs ? ?/sec
contrived/05x_entities_15_systems 1.00 234.6±6.74µs ? ?/sec 1.03 241.8±2.18µs ? ?/sec
empty_systems/000_systems 1.00 47.2±0.70ns ? ?/sec 1.07 50.4±0.41ns ? ?/sec
empty_systems/001_systems 1.02 1702.9±22.27ns ? ?/sec 1.00 1677.4±32.44ns ? ?/sec
empty_systems/002_systems 1.04 2.2±0.02µs ? ?/sec 1.00 2.1±0.02µs ? ?/sec
empty_systems/003_systems 1.01 2.6±0.06µs ? ?/sec 1.00 2.6±0.26µs ? ?/sec
empty_systems/004_systems 1.01 3.0±0.08µs ? ?/sec 1.00 3.0±0.10µs ? ?/sec
empty_systems/005_systems 1.02 3.5±0.06µs ? ?/sec 1.00 3.4±0.08µs ? ?/sec
empty_systems/010_systems 1.09 6.0±0.14µs ? ?/sec 1.00 5.5±0.10µs ? ?/sec
empty_systems/015_systems 1.11 8.5±0.36µs ? ?/sec 1.00 7.6±0.85µs ? ?/sec
empty_systems/020_systems 1.07 10.6±0.31µs ? ?/sec 1.00 9.9±0.23µs ? ?/sec
empty_systems/025_systems 1.06 13.0±0.22µs ? ?/sec 1.00 12.3±0.50µs ? ?/sec
empty_systems/030_systems 1.09 16.0±0.63µs ? ?/sec 1.00 14.6±0.39µs ? ?/sec
empty_systems/035_systems 1.06 18.2±0.39µs ? ?/sec 1.00 17.1±0.42µs ? ?/sec
empty_systems/040_systems 1.07 20.6±0.56µs ? ?/sec 1.00 19.3±0.35µs ? ?/sec
empty_systems/045_systems 1.07 23.0±0.59µs ? ?/sec 1.00 21.5±0.55µs ? ?/sec
empty_systems/050_systems 1.06 25.2±0.47µs ? ?/sec 1.00 23.7±0.66µs ? ?/sec
empty_systems/055_systems 1.08 27.6±0.66µs ? ?/sec 1.00 25.5±0.50µs ? ?/sec
empty_systems/060_systems 1.04 29.4±0.65µs ? ?/sec 1.00 28.3±1.59µs ? ?/sec
empty_systems/065_systems 1.07 31.9±0.75µs ? ?/sec 1.00 29.9±0.86µs ? ?/sec
empty_systems/070_systems 1.10 34.7±1.64µs ? ?/sec 1.00 31.5±1.20µs ? ?/sec
empty_systems/075_systems 1.09 37.1±1.83µs ? ?/sec 1.00 34.1±0.63µs ? ?/sec
empty_systems/080_systems 1.08 39.1±1.18µs ? ?/sec 1.00 36.3±0.88µs ? ?/sec
empty_systems/085_systems 1.04 41.3±1.22µs ? ?/sec 1.00 39.8±3.03µs ? ?/sec
empty_systems/090_systems 1.08 43.5±1.09µs ? ?/sec 1.00 40.4±0.93µs ? ?/sec
empty_systems/095_systems 1.07 45.9±1.36µs ? ?/sec 1.00 43.1±1.05µs ? ?/sec
empty_systems/100_systems 1.04 47.9±0.89µs ? ?/sec 1.00 46.1±1.81µs ? ?/sec
I experimented with some stuff like looping inside the catch unwind and while it did see improvements in the benchmarks, when I ran many foxes it had a significant regression.
The real world regression is low enough that I will approve. ~~Especially since this seems to fix some unsoundness.~~
Edit: didn't fix the unsoundness
~~probably should add fixes https://github.com/bevyengine/bevy/issues/6603 to the PR description~~
Edit: this didn't fix the linked issue
The scheduling microbenchmarks are notoriously noisy. These results seem to be within the margin of error.
bors r+
Pull request successfully merged into main.
Build succeeded:
- build-and-install-on-iOS
- build-android
- build (macos-latest)
- build (ubuntu-latest)
- build-wasm
- build (windows-latest)
- build-without-default-features (bevy)
- build-without-default-features (bevy_ecs)
- build-without-default-features (bevy_reflect)
- check-compiles
- check-doc
- check-missing-examples-in-docs
- ci
- markdownlint
- run-examples
- run-examples-on-wasm
- run-examples-on-windows-dx12