brianch / offline-chess-puzzles

Tool to view and solve puzzles from the lichess puzzle database
MIT License
122 stars 17 forks source link

Sporadic Index Out of Bounds Error in Main Thread #27

Closed cyrillkuettel closed 2 days ago

cyrillkuettel commented 1 week ago

The app is encountering an intermittent panic in the main thread due to an index out of bounds error.

thread 'main' panicked at src/main.rs:297:62:
index out of bounds: the len is 6 but the index is 6
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:387:33:
called `Result::unwrap()` on an `Err` value: PoisonError { .. }

Heads up: I can't actually reproduce it reliably. It just pops up sometimes. Today it happened 2 times in a row. Could be some weird edge case.

Attempts to reproduce (incomplete)
  • Build latest master. Just `cargo run`
  • My db_puzzles.cv is from Aug 15 2023
  • Set theme="Mate in 3", rating range=2286 and 2500, popularity=96
  • Then the error happened after playing 3-4 puzzles.
brianch commented 6 days ago

Hi! Sorry for the delay.

Hmmm, I tried all the puzzles in that search twice but my DB is more recent, so I guess the one that triggers the problem might be in another range or popularity now.

At first glance I don't see what might be wrong, can you just put some println statements above that line 297 and tell me the info of the puzzle that makes it crash? You can copy this:

            println!("Current puzzle ID: {}", self.puzzle_tab.puzzles[self.puzzle_tab.current_puzzle].puzzle_id);
            println!("Current puzzle FEN: {}", self.puzzle_tab.current_puzzle_fen);
            println!("moves: {}", self.puzzle_tab.puzzles[self.puzzle_tab.current_puzzle].moves);
            println!("current move: {}", &correct_moves[self.puzzle_tab.current_puzzle_move]);

This part of the code is a bit messy tbh, it should be refactored sooner or later.

Thanks a lot for reporting!!

cyrillkuettel commented 4 days ago

Woohoo! 🎉 managed to reproduce it. The range I gave above was wrong.

Here are the key settings:

And here is my puzzles.csv https://we.tl/t-MMAkJFkNFO (604MB) I pushed my changes to a fork: https://github.com/brianch/offline-chess-puzzles/compare/main...cyrillkuettel:offline-chess-puzzles:main

Auto load next puzzle was set to false, but that's likely not relevant.

Below is the full stacktrace. Let me know if you need any help analyzing the error or have questions about next steps!

$ RUST_BACKTRACE=1 cargo run
warning: field `stream` is never read
   --> src/main.rs:133:5
    |
131 | struct SoundPlayback {
    |        ------------- field in this struct
132 |     // it's not directly used, but we need to keep it: https://github.com/RustAudio/rodio/issues/330
133 |     stream: OutputStream,
    |     ^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: `offline-chess-puzzles` (bin "offline-chess-puzzles") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.22s
     Running `target/debug/offline-chess-puzzles`
Current puzzle ID: F3ITl
Current puzzle FEN: r1b2b1r/1p1k1ppp/p1n1p1n1/1N1p2B1/3P4/2PB1Q2/3NKPPP/7q w - - 0 1
moves: a1h1 f3d5 e6d5 d3f5 d7e8 b5c7
current move: f3d5
Current puzzle ID: F3ITl
Current puzzle FEN: r1b2b1r/1p1k1ppp/p1n1p1n1/1N1p2B1/3P4/2PB1Q2/3NKPPP/7q w - - 0 1
moves: a1h1 f3d5 e6d5 d3f5 d7e8 b5c7
current move: f3d5
Current puzzle ID: F3ITl
Current puzzle FEN: r1b2b1r/1p1k1ppp/p1n1p1n1/1N1p2B1/3P4/2PB1Q2/3NKPPP/7q w - - 0 1
moves: a1h1 f3d5 e6d5 d3f5 d7e8 b5c7
current move: d3f5
Current puzzle ID: F3ITl
Current puzzle FEN: r1b2b1r/1p1k1ppp/p1n1p1n1/1N1p2B1/3P4/2PB1Q2/3NKPPP/7q w - - 0 1
moves: a1h1 f3d5 e6d5 d3f5 d7e8 b5c7
current move: b5c7
Current puzzle ID: QO0ZN
Current puzzle FEN: r4r2/p3kpp1/bp2p2p/2n1N3/2N5/1P6/P5PP/3R1RK1 w - - 0 1
moves: h8f8 d1d7 c5d7 e5c6 e7e8 c4d6
current move: d1d7
Current puzzle ID: QO0ZN
Current puzzle FEN: r4r2/p3kpp1/bp2p2p/2n1N3/2N5/1P6/P5PP/3R1RK1 w - - 0 1
moves: h8f8 d1d7 c5d7 e5c6 e7e8 c4d6
current move: e5c6
Current puzzle ID: QO0ZN
Current puzzle FEN: r4r2/p3kpp1/bp2p2p/2n1N3/2N5/1P6/P5PP/3R1RK1 w - - 0 1
moves: h8f8 d1d7 c5d7 e5c6 e7e8 c4d6
current move: c4d6
Current puzzle ID: P0SCA
Current puzzle FEN: r7/1p3RB1/2b3p1/P1Pp2kp/1ppP4/4P3/6PP/6K1 w - - 0 1
moves: h6g5 f7f4 h5h4 g7f6 g5h6 f4h4
current move: f7f4
Current puzzle ID: P0SCA
Current puzzle FEN: r7/1p3RB1/2b3p1/P1Pp2kp/1ppP4/4P3/6PP/6K1 w - - 0 1
moves: h6g5 f7f4 h5h4 g7f6 g5h6 f4h4
current move: g7f6
Current puzzle ID: P0SCA
Current puzzle FEN: r7/1p3RB1/2b3p1/P1Pp2kp/1ppP4/4P3/6PP/6K1 w - - 0 1
moves: h6g5 f7f4 h5h4 g7f6 g5h6 f4h4
current move: f4h4
Current puzzle ID: ergkn
Current puzzle FEN: 4r3/4rR2/p1p3p1/2b5/6kP/2P3P1/PP3PK1/4R3 w - - 0 1
moves: a7e7 e1e5 e7e5 f2f3 g4h5 f7h7
current move: e1e5
Current puzzle ID: ergkn
Current puzzle FEN: 4r3/4rR2/p1p3p1/2b5/6kP/2P3P1/PP3PK1/4R3 w - - 0 1
moves: a7e7 e1e5 e7e5 f2f3 g4h5 f7h7
current move: e1e5
Current puzzle ID: ergkn
Current puzzle FEN: 4r3/4rR2/p1p3p1/2b5/6kP/2P3P1/PP3PK1/4R3 w - - 0 1
moves: a7e7 e1e5 e7e5 f2f3 g4h5 f7h7
current move: f2f3
Current puzzle ID: ergkn
Current puzzle FEN: 4r3/4rR2/p1p3p1/2b5/6kP/2P3P1/PP3PK1/4R3 w - - 0 1
moves: a7e7 e1e5 e7e5 f2f3 g4h5 f7h7
current move: f2f3
Current puzzle ID: ergkn
Current puzzle FEN: 4r3/4rR2/p1p3p1/2b5/6kP/2P3P1/PP3PK1/4R3 w - - 0 1
moves: a7e7 e1e5 e7e5 f2f3 g4h5 f7h7
current move: f7h7
Current puzzle ID: ergkn
Current puzzle FEN: 4r3/4rR2/p1p3p1/2b5/6kP/2P3P1/PP3PK1/4R3 w - - 0 1
moves: a7e7 e1e5 e7e5 f2f3 g4h5 f7h7
current move: f7h7
Current puzzle ID: ergkn
Current puzzle FEN: 4r3/4rR2/p1p3p1/2b5/6kP/2P3P1/PP3PK1/4R3 w - - 0 1
moves: a7e7 e1e5 e7e5 f2f3 g4h5 f7h7
thread 'main' panicked at src/main.rs:300:56:
index out of bounds: the len is 6 but the index is 6
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::panicking::panic_bounds_check
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:208:5
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/slice/index.rs:255:10
   4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/slice/index.rs:18:9
   5: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/vec/mod.rs:2770:9
   6: offline_chess_puzzles::OfflinePuzzles::verify_and_make_move
             at ./src/main.rs:300:56
   7: <offline_chess_puzzles::OfflinePuzzles as iced::application::Application>::update
             at ./src/main.rs:795:25
   8: <iced::application::Instance<A> as iced_runtime::program::Program>::update
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced-0.12.1/src/application.rs:227:9
   9: iced_winit::application::update::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_winit-0.12.2/src/application.rs:641:40
  10: iced_futures::backend::native::tokio::<impl iced_futures::executor::Executor for tokio::runtime::runtime::Runtime>::enter
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_futures-0.12.0/src/backend/native/tokio.rs:19:9
  11: iced_futures::runtime::Runtime<Executor,Sender,Message>::enter
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_futures-0.12.0/src/runtime.rs:51:9
  12: iced_winit::application::update
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_winit-0.12.2/src/application.rs:641:23
  13: iced_winit::application::run_instance::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_winit-0.12.2/src/application.rs:528:21
  14: iced_winit::application::run::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_winit-0.12.2/src/application.rs:228:24
  15: <winit::platform_impl::platform::app_state::EventLoopHandler<T> as winit::platform_impl::platform::app_state::EventHandler>::handle_nonuser_event::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:87:13
  16: winit::platform_impl::platform::app_state::EventLoopHandler<T>::with_callback
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:70:13
  17: <winit::platform_impl::platform::app_state::EventLoopHandler<T> as winit::platform_impl::platform::app_state::EventHandler>::handle_nonuser_event
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:86:9
  18: winit::platform_impl::platform::app_state::Handler::handle_nonuser_event
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:318:13
  19: winit::platform_impl::platform::app_state::AppState::cleared
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:653:9
  20: winit::platform_impl::platform::observer::control_flow_end_handler::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/observer.rs:78:21
  21: winit::platform_impl::platform::observer::control_flow_handler::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/observer.rs:40:9
  22: std::panicking::try::do_call
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:552:40
  23: ___rust_try
  24: std::panicking::try
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:516:19
  25: std::panic::catch_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panic.rs:142:14
  26: winit::platform_impl::platform::event_loop::stop_app_on_panic
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:469:11
  27: winit::platform_impl::platform::observer::control_flow_handler
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/observer.rs:38:5
  28: winit::platform_impl::platform::observer::control_flow_end_handler
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/observer.rs:73:9
  29: <unknown>
  30: <unknown>
  31: <unknown>
  32: <unknown>
  33: <unknown>
  34: <unknown>
  35: <unknown>
  36: <unknown>
  37: <unknown>
  38: <unknown>
  39: <() as objc2::message::MessageArguments>::__invoke
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/message/mod.rs:531:39
  40: objc2::message::platform::send_unverified
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/message/apple/mod.rs:35:34
  41: objc2::message::MessageReceiver::send_message
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/message/mod.rs:233:53
  42: winit::platform_impl::platform::appkit::application::NSApplication::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/macros/__attribute_helpers.rs:126:21
  43: winit::platform_impl::platform::event_loop::EventLoop<T>::run_on_demand::{{closure}}::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:310:26
  44: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
  45: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panic/unwind_safe.rs:272:9
  46: std::panicking::try::do_call
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:552:40
  47: ___rust_try
  48: std::panicking::try
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:516:19
  49: std::panic::catch_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panic.rs:142:14
  50: winit::platform_impl::platform::event_loop::EventLoop<T>::run_on_demand::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:299:32
  51: objc2::rc::autorelease::autoreleasepool
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/rc/autorelease.rs:438:15
  52: winit::platform_impl::platform::event_loop::EventLoop<T>::run_on_demand
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:281:9
  53: winit::platform_impl::platform::event_loop::EventLoop<T>::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:248:9
  54: winit::event_loop::EventLoop<T>::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/event_loop.rs:249:9
  55: iced_winit::application::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_winit-0.12.2/src/application.rs:243:13
  56: iced::application::Application::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced-0.12.1/src/application.rs:208:12
  57: offline_chess_puzzles::main
             at ./src/main.rs:1271:5
  58: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread 'main' panicked at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:387:33:
called `Result::unwrap()` on an `Err` value: PoisonError { .. }
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1649:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1073:23
   4: winit::platform_impl::platform::app_state::AppState::clear_callback
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/app_state.rs:387:9
   5: winit::platform_impl::platform::event_loop::EventLoop<T>::run_on_demand::{{closure}}
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:329:13
   6: objc2::rc::autorelease::autoreleasepool
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/rc/autorelease.rs:438:15
   7: winit::platform_impl::platform::event_loop::EventLoop<T>::run_on_demand
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:281:9
   8: winit::platform_impl::platform::event_loop::EventLoop<T>::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/platform_impl/macos/event_loop.rs:248:9
   9: winit::event_loop::EventLoop<T>::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.29.14/src/event_loop.rs:249:9
  10: iced_winit::application::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced_winit-0.12.2/src/application.rs:243:13
  11: iced::application::Application::run
             at /Users/cyrill/.cargo/registry/src/index.crates.io-6f17d22bba15001f/iced-0.12.1/src/application.rs:208:12
  12: offline_chess_puzzles::main
             at ./src/main.rs:1271:5
  13: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
brianch commented 3 days ago

Ohhh, thanks a lot!! That's incredibly helpful! 😀

I'll take a look tonight or tomorrow!

brianch commented 2 days ago

Hi,

from what I saw, it was due to the game allowing moves after the end of the puzzle, which lead to that index out of bounds. I pushed a fix, it should be fine now.

Instit on lichess (the French translator) had mentioned this same issue some time ago, but I didn't fix it at the time 😅

Thanks a lot for taking the time to report and provide all the info! :)