Encountering an issue while using cpal

I've been playing around with cpal and gotten stuck trying to solve the following:

Using GitHub - dheijl/swyh-rs: Stream What You Hear written in rust, inspired by SWYH. as a base, I've removed everything but the bit that interested me (audio loopback), resulting in GitHub - 4JX/cpal-test.

The code as is runs without issue and captures audio fine (at least on Linux after setting up the input with pavucontrol). That is, until I add the following sleep:

Which makes all data coming out of rms_l and rms_r a constant stream of 0's. The sleep itself is just there as a "dummy" function to simplify the problem for explanation purposes, but causes the same issues as the one intended to go there (Which takes the data and then works with it).

That sounds like a race condition to me. I'd play with a few things:

  • I've never heard of the flume crate before, and that sticks out to me. Try replacing it with crossbeam-channel which is probably more battle-tested.
  • Run your code with TSan enabled, to see if that catches anything (requires a nightly version of Rust):
RUSTFLAGS="-Z sanitizer=thread" cargo run

It does fail to compile, so that's a change I guess :eyes:. Changing to crossbeam had no effect (as in when using the stable toolchain, it did not affect the output being all 0's).

  • Using crossbeam-channel
   Compiling thiserror v1.0.30
error: /home/infinity/Documents/Rust/cpal-test/target/debug/deps/libthiserror_impl-910d3caa4ea4cd01.so: undefined symbol: __tsan_read8
   --> /home/infinity/.cargo/registry/src/github.com-1ecc6299db9ec823/thiserror-1.0.30/src/lib.rs:213:9
213 | pub use thiserror_impl::*;
    |         ^^^^^^^^^^^^^^

error: could not compile `thiserror` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed
  • Using flume
   Compiling thiserror-impl v1.0.30
   Compiling pin-project v1.0.8
error: /home/infinity/Documents/Rust/cpal-test/target/debug/deps/libpin_project_internal-816f938d2fd95657.so: undefined symbol: __tsan_read8
  --> /home/infinity/.cargo/registry/src/github.com-1ecc6299db9ec823/pin-project-1.0.8/src/lib.rs:82:9
82 | pub use pin_project_internal::pin_project;
   |         ^^^^^^^^^^^^^^^^^^^^

error: /home/infinity/Documents/Rust/cpal-test/target/debug/deps/libpin_project_internal-816f938d2fd95657.so: undefined symbol: __tsan_read8
  --> /home/infinity/.cargo/registry/src/github.com-1ecc6299db9ec823/pin-project-1.0.8/src/lib.rs:84:9
84 | pub use pin_project_internal::pinned_drop;
   |         ^^^^^^^^^^^^^^^^^^^^

error: /home/infinity/Documents/Rust/cpal-test/target/debug/deps/libpin_project_internal-816f938d2fd95657.so: undefined symbol: __tsan_read8
   --> /home/infinity/.cargo/registry/src/github.com-1ecc6299db9ec823/pin-project-1.0.8/src/lib.rs:159:13
159 |     pub use pin_project_internal::__PinProjectInternalDerive;
    |             ^^^^^^^^^^^^^^^^^^^^

error: could not compile `pin-project` due to 3 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed

Using neither still fails since cpal and a few others still depend on thiserror.

Tested using rustc 1.59.0-nightly (e2116acae 2021-12-05).

Compiling and running either of the two with nightly but without the flag still yields the same behavior but does compile.

Edit: Tried a few older nightly versions to see if it was a temporary thing (11-01 and 12-01), they all return the same error at compile time which I really don't have much idea of what to do with it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.