Skip to content

Panic reported on disconnect #75

@andrewdavidmackenzie

Description

@andrewdavidmackenzie

Describe the bug
When I do a disconnect from a device (see code further down).

I get a panic at the write() in this code in ble_handler.rs:246:47

    pub async fn write_to_radio(&self, buffer: &[u8]) -> Result<(), Error> {
        self.radio
            // TODO: remove the skipping of the first 4 bytes
            .write(&self.toradio_char, &buffer[4..], WriteType::WithResponse)
            .await
            .map_err(|e: btleplug::Error| {
                Error::InternalStreamError(InternalStreamError::StreamWriteError {
                    source: Box::new(e),
                })
            })
    }

with this backtrace:

thread 'tokio-runtime-worker' panicked at /Users/amackenz/.cargo/git/checkouts/rust-a1709e8f8e020629/bd5fe19/src/connections/ble_handler.rs:246:47:
range start index 4 out of range for slice of length 0
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_fmt
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panicking.rs:75:14
   2: core::slice::index::slice_start_index_len_fail::do_panic::runtime
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panic.rs:218:21
   3: core::slice::index::slice_start_index_len_fail::do_panic
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/intrinsics/mod.rs:2367:9
   4: core::slice::index::slice_start_index_len_fail
             at /rustc/1159e78c4747b02ef996e55082b704c09b970588/library/core/src/panic.rs:223:9
   5: <core::ops::range::RangeFrom<usize> as core::slice::index::SliceIndex<[T]>>::index
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/slice/index.rs:570:13
   6: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/slice/index.rs:18:15
   7: meshtastic::connections::ble_handler::BleHandler::write_to_radio::{{closure}}
             at /Users/amackenz/.cargo/git/checkouts/rust-a1709e8f8e020629/bd5fe19/src/connections/ble_handler.rs:246:47
   8: meshtastic::utils_internal::build_ble_stream::{{closure}}::{{closure}}
             at /Users/amackenz/.cargo/git/checkouts/rust-a1709e8f8e020629/bd5fe19/src/utils_internal.rs:334:61
   9: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:365:24
  10: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/loom/std/unsafe_cell.rs:16:9
  11: tokio::runtime::task::core::Core<T,S>::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:354:30
  12: tokio::runtime::task::harness::poll_future::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:535:30
  13: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272:9
  14: std::panicking::catch_unwind::do_call
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:589:40
  15: ___rust_try
  16: std::panicking::catch_unwind
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:552:19
  17: std::panic::catch_unwind
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:359:14
  18: tokio::runtime::task::harness::poll_future
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:523:18
  19: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:210:27
  20: tokio::runtime::task::harness::Harness<T,S>::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:155:20
  21: tokio::runtime::task::raw::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:325:13
  22: tokio::runtime::task::raw::RawTask::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:255:18
  23: tokio::runtime::task::LocalNotified<S>::run
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/mod.rs:509:13
  24: tokio::runtime::scheduler::multi_thread::worker::Context::run_task::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:600:18
  25: tokio::task::coop::with_budget
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/task/coop/mod.rs:167:5
  26: tokio::task::coop::budget
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/task/coop/mod.rs:133:5
  27: tokio::runtime::scheduler::multi_thread::worker::Context::run_task
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:591:9
  28: tokio::runtime::scheduler::multi_thread::worker::Context::run
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:551:29
  29: tokio::runtime::scheduler::multi_thread::worker::run::{{closure}}::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:504:24
  30: tokio::runtime::context::scoped::Scoped<T>::set
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context/scoped.rs:40:9
  31: tokio::runtime::context::set_scheduler::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context.rs:176:38
  32: std::thread::local::LocalKey<T>::try_with
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/local.rs:315:12
  33: std::thread::local::LocalKey<T>::with
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/local.rs:279:20
  34: tokio::runtime::context::set_scheduler
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context.rs:176:17
  35: tokio::runtime::scheduler::multi_thread::worker::run::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:499:9
  36: tokio::runtime::context::runtime::enter_runtime
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context/runtime.rs:65:16
  37: tokio::runtime::scheduler::multi_thread::worker::run
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:491:5
  38: tokio::runtime::scheduler::multi_thread::worker::Launch::launch::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:457:45
  39: <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/task.rs:42:21
  40: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:365:24
  41: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/loom/std/unsafe_cell.rs:16:9
  42: tokio::runtime::task::core::Core<T,S>::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:354:30
  43: tokio::runtime::task::harness::poll_future::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:535:30
  44: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272:9
  45: std::panicking::catch_unwind::do_call
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:589:40
  46: ___rust_try
  47: std::panicking::catch_unwind
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:552:19
  48: std::panic::catch_unwind
             at /Users/amackenz/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:359:14
  49: tokio::runtime::task::harness::poll_future
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:523:18
  50: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:210:27
  51: tokio::runtime::task::harness::Harness<T,S>::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:155:20
  52: tokio::runtime::task::raw::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:325:13
  53: tokio::runtime::task::raw::RawTask::poll
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:255:18
  54: tokio::runtime::task::UnownedTask<S>::run
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/mod.rs:546:13
  55: tokio::runtime::blocking::pool::Task::run
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/pool.rs:161:19
  56: tokio::runtime::blocking::pool::Inner::run
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/pool.rs:516:22
  57: tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}
             at /Users/amackenz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/pool.rs:474:47

I see there is a TODO there, that might be related.

To Reproduce
This is the code I use to disconnect from a device, when a UI button to do so is clicked:

async fn do_disconnect(stream_api: ConnectedStreamApi) -> Result<(), anyhow::Error> {
    stream_api
        .disconnect()
        .await
        .context("Failed to disconnect")?;
    Ok(())
}

Expected behavior
Call to disconnect either works or returns an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions