rodio icon indicating copy to clipboard operation
rodio copied to clipboard

output stream dropped when ignoring stream variable

Open joprice opened this issue 3 years ago • 2 comments

Not sure if this should be more obvious than it was for me, but I burned some time debugging a situation where ignoring the first return value of try_default below caused the stream to be dropped and the program to silently exit without playing anything.

 let (_stream, stream_handle) = rodio::OutputStream::try_default()?;

Replacing _stream with _ causes programs using the stream_handle to fail, as I assume the stream was optimized away or closed immediately after creation. It was surprising to me that the lifetime of the stream object wasn't connected to the handle's lifetime in a way to avoid this.

If there's nothing to be improved, hopefully this at least helps someone debug a similar situation, as I almost abandoned using the library, thinking it had some fundamental bug and only discovered this fix by mistake.

joprice avatar Oct 13 '20 23:10 joprice

Just had this bug myself. The stream is indeed being dropped. I assume this is due to the weak Arcs that OutputStreamHandle is using. It's very confusing since the error says NoDevice when doing something like creating a sink or using play_raw instead of like StreamDropped (even Copilot suggests this lol). image Full (bad) error output for future Googler's:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NoDevice'

clay53 avatar Nov 14 '21 20:11 clay53

For people getting this error when compiling for WASM, ensure (If you're not enabling all features) that you've got the wasm-bindgen feature enabled for rodio. That was my problem, anyway

Oberdiah avatar Mar 16 '22 20:03 Oberdiah

Replacing _stream with _ causes programs using the stream_handle to fail, as I assume the stream was optimized away or closed immediately after creation.

Underscore is a pattern that matches everything and does not bind to a value. You are effectively dropping (and thus closing) the output-stream at the end of the line.

Here are some docs regarding _ (its not really explained in the book I think).

  • https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html?highlight=underscore#ignoring-an-entire-value-with-_
  • https://doc.rust-lang.org/stable/reference/patterns.html?highlight=single%20underscore#wildcard-pattern
  • https://doc.rust-lang.org/stable/reference/identifiers.html?highlight=underscore#identifiers

Feel free to submit a PR to improve the docs. A warning about dropping Self would be useful here.

dvdsk avatar Mar 24 '24 22:03 dvdsk