tesselode / kira

Library for expressive game audio.
https://crates.io/crates/kira
Apache License 2.0
837 stars 42 forks source link

Dropping `AudioManager` panics on Android #37

Open Mivik opened 1 year ago

Mivik commented 1 year ago

The minimal reproducing steps are simply dropping a AudioManager.

PanicInfo { payload: Any { .. }, message: Some(called `Option::unwrap()` on a `None` value), location: Location { file: "~/.cargo/registry/src/github.com-1ecc6299db9ec823/kira-0.7.1/src/manager/backend/cpal/desktop/stream_manager/renderer_wrapper.rs", line: 40, col: 9 }, can_unwind: true }

Currently this issue is temporarily fixed by implementing the Drop trait for the oboe backend of cpal (host/oboe/mod.rs):

impl Drop for Stream {
    fn drop(&mut self) {
        match self {
            Self::Input(stream) => {
                stream.borrow_mut().stop();
                stream.borrow_mut().close();
            }
            Self::Output(stream) => {
                stream.borrow_mut().stop();
                stream.borrow_mut().close();
            }
        }
    }
}

Not sure if this is an issue of cpal.

tesselode commented 1 year ago

I don't currently have a dev environment for compiling Rust code for Android. I can work on setting one up, but in the meantime, can you get a stacktrace for that panic?

Mivik commented 1 year ago

I don't currently have a dev environment for compiling Rust code for Android. I can work on setting one up, but in the meantime, can you get a stacktrace for that panic?

For some reason I was unable to get a concrete stacktrace. I debugged the program and here's roughly the call stack:

  1. oboe::AudioStream::fireDataCallback (oboe/src/common/AudioStream.cpp:63)
  2. StreamManager audio output stream callback https://github.com/tesselode/kira/blob/dc66cd995c5b522ba68f20a305bc43f4e6ab718d/crates/kira/src/manager/backend/cpal/desktop/stream_manager.rs#L130
  3. RendererWrapper deref() https://github.com/tesselode/kira/blob/dc66cd995c5b522ba68f20a305bc43f4e6ab718d/crates/kira/src/manager/backend/cpal/desktop/stream_manager/renderer_wrapper.rs#L34

Here the renderer is already taken by drop and thus the program panics.

tesselode commented 1 year ago

@Mivik it seems like cpal is using Kira's stream manager after dropping it, which would be a bug in cpal. I'd open an issue in their repo and see what they say.