ActivityWatch/aw-server-rust

Called `Option::unwrap()` on a `None` value

BelKed opened this issue · 2 comments

BelKed commented

While browsing the history in ActivityWatch, I ran across the following error — AxiosError: Request failed with status code 500. See dev console (F12) and/or server logs for more info.


Here are the logs from the server with RUST_BACKTRACE=full:

$ RUST_BACKTRACE=full /Applications/ActivityWatch.app/Contents/MacOS/aw-server-rust  
[2023-07-06 12:32:25][INFO][aw_server]: Using DB at path "/Users/BelKed/Library/Application Support/activitywatch/aw-server-rust/sqlite.db"
[2023-07-06 12:32:44][INFO][aw_server]: Cannot find assets "./aw-webui/dist/"
[2023-07-06 12:32:44][INFO][aw_server]: Using aw-webui assets at path "/Applications/ActivityWatch.app/Contents/Resources/aw_server_rust/static/"
[2023-07-06 12:32:44][INFO][aw_server::endpoints]: Starting aw-server-rust at 127.0.0.1:5600
[2023-07-06 12:33:26][WARN][aw_transform::flood]: Gap was of negative duration (-PT31.362Ss), but could be safely merged. This error will only show once per batch.

[2023-07-06 12:33:27][WARN][aw_transform::flood]: Gap was of negative duration and could NOT be safely merged (-PT0.307Ss). This warning will only show once per batch.
thread 'rocket-worker-thread' panicked at 'called `Option::unwrap()` on a `None` value', aw-transform/src/period_union.rs:30:46
stack backtrace:
   0:        0x107cb3b06 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h435ed4208ca0b670
   1:        0x107cd352a - core::fmt::write::h4d6b93c4e5363221
   2:        0x107cb001c - std::io::Write::write_fmt::hb2fc6460df89e01d
   3:        0x107cb38ea - std::sys_common::backtrace::print::hf905284f85d9e767
   4:        0x107cb5023 - std::panicking::default_hook::{{closure}}::ha243261ecc68d5da
   5:        0x107cb4d78 - std::panicking::default_hook::h4ea85a788051c785
   6:        0x107cb560b - std::panicking::rust_panic_with_hook::hb96b547a0b8d1a53
   7:        0x107cb54e3 - std::panicking::begin_panic_handler::{{closure}}::h6f5f9fa2c728c192
   8:        0x107cb3f49 - std::sys_common::backtrace::__rust_end_short_backtrace::h3f3e277c843dc0b8
   9:        0x107cb51ed - _rust_begin_unwind
  10:        0x107cfe413 - core::panicking::panic_fmt::h635b9a113c844567
  11:        0x107cfe4a7 - core::panicking::panic::hdc2dc31080e72287
  12:        0x107843ac8 - aw_transform::period_union::period_union::h26c2c688703a7fb2
  13:        0x1077ede1d - aw_query::functions::qfunctions::period_union::h303151dfd0e7d205
  14:        0x1077f765a - aw_query::interpret::interpret_expr::h102f862e0204ea63
  15:        0x1077f66de - aw_query::interpret::interpret_expr::h102f862e0204ea63
  16:        0x1077f5bcf - aw_query::interpret::interpret_prog::h7accd96d33aa5d7e
  17:        0x1077fd720 - aw_query::query::ha81678dfc976ae78
  18:        0x107764440 - aw_server::endpoints::query::query::into_info::monomorphized_function::{{closure}}::h2ce45efc5604f1d3
  19:        0x10773b77b - rocket::server::<impl rocket::rocket::Rocket<rocket::phase::Orbit>>::route::{{closure}}::hd7649f8ce50864c3
  20:        0x107736e89 - rocket::server::hyper_service_fn::{{closure}}::{{closure}}::h1b9b968a50ccec41
  21:        0x1076685c1 - tokio::runtime::task::core::Core<T,S>::poll::h71a1d22b9f5c5568
  22:        0x1076c4a1c - tokio::runtime::task::raw::poll::h01634bdcc468144a
  23:        0x107c2c42c - tokio::runtime::scheduler::multi_thread::worker::Context::run_task::h1be8248031b73740
  24:        0x107c2bcb1 - tokio::runtime::scheduler::multi_thread::worker::Context::run::h31a3f36c362d2ff4
  25:        0x107c3d429 - tokio::macros::scoped_tls::ScopedKey<T>::set::h4d2c1cc2160cf81d
  26:        0x107c2b8d2 - tokio::runtime::scheduler::multi_thread::worker::run::h2e732cabe9a39e6a
  27:        0x107c1cde4 - tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut::h19339f8203a85ca9
  28:        0x107c17aa0 - tokio::runtime::task::core::Core<T,S>::poll::ha464ace5ac671b0d
  29:        0x107c10bcb - tokio::runtime::task::harness::Harness<T,S>::poll::hf936bd75cc65e17d
  30:        0x107c36fb7 - tokio::runtime::blocking::pool::Inner::run::hd8e7ddcbb099c51f
  31:        0x107c287bb - std::sys_common::backtrace::__rust_begin_short_backtrace::h8ffbfd4d18cd569c
  32:        0x107c3804e - core::ops::function::FnOnce::call_once{{vtable.shim}}::h7bfa3ffea6846f06
  33:        0x107cb7b87 - std::sys::unix::thread::Thread::new::thread_start::hc6546297a1d6bf9e
  34:     0x7ff80077b4e1 - __pthread_start
[2023-07-06 12:33:27][ERROR][_]: Handler query panicked.
[2023-07-06 12:33:27][WARN][_]: A panic is treated as an internal server error.
[2023-07-06 12:33:27][WARN][_]: No 500 catcher registered. Using Rocket default.
[2023-07-06 12:33:27][WARN][aw_server::endpoints::bucket]: Taking datastore lock failed, returning 504: poisoned lock: another task failed inside
[2023-07-06 12:33:28][WARN][aw_server::endpoints::bucket]: Taking datastore lock failed, returning 504: poisoned lock: another task failed inside

let last_event = events_union.last().unwrap();

The problem is that events_union.last() is None which causes the error.

let last_event = events_union.last().unwrap();

The problem is that events_union.last() is None which causes the error.

How is it possible?

This conditional statement check sorted_events not empty:

https://github.com/ActivityWatch/aw-server-rust/blob/73b5ae31826315b94208805f222b42a7818169b3/aw-transform/src/period_union.rs#L25C5-L27

Then, events_union array has at least one item

And Else of that conditional, this loop will not run if sorted_events is empty:

https://github.com/ActivityWatch/aw-server-rust/blob/73b5ae31826315b94208805f222b42a7818169b3/aw-transform/src/period_union.rs#L29C4-L30

So we don't have any issues with events_union array

To be sure, I ran the tests and added a test like this:
‍‍‍‍‍‍‍

#[test]
fn test_period_union_nop_multi() {
    let e1 = Event {
        id: None,
        timestamp: DateTime::from_str("2000-01-01T00:00:01Z").unwrap(),
        duration: Duration::seconds(1),
        data: json_map! {"test": json!(1)},
    };

    let mut e2 = e1.clone();
    e2.timestamp = DateTime::from_str("2000-01-01T00:00:03Z").unwrap();

    let mut e3 = e1.clone();
    e3.timestamp = DateTime::from_str("2000-01-01T00:00:05Z").unwrap();

    let mut e4 = e1.clone();
    e4.timestamp = DateTime::from_str("2000-01-01T00:00:07Z").unwrap();

    let e_result = period_union(&[e1, e2, e3], &[e4]);
    assert_eq!(e_result.len(), 4);
}

And it's passed :)

BelKed commented

Thanks :)
Maybe it was a just rare bug on the hardware level of my computer or something :D


Since I can't remember how I managed to get this error, and now, after a bit of trying, I haven't managed to get it, I'm closing this issue...