Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions playback/src/audio_backend/alsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::convert::Converter;
use crate::decoder::AudioPacket;
use crate::{NUM_CHANNELS, SAMPLE_RATE};
use alsa::device_name::HintIter;
use alsa::pcm::{Access, Format, Frames, HwParams, PCM};
use alsa::pcm::{Access, Format, Frames, HwParams, State, PCM};
use alsa::{Direction, ValueOr};
use std::process::exit;
use thiserror::Error;
Expand Down Expand Up @@ -442,7 +442,14 @@ impl Sink for AlsaSink {

let pcm = self.pcm.take().ok_or(AlsaError::NotConnected)?;

pcm.drain().map_err(AlsaError::DrainFailure)?;
// Only drain if the PCM is in Running state. After a write error,
// try_recover() leaves the PCM in Prepared state, and calling drain()
// on a Prepared-state USB PCM can deadlock the kernel driver
// (confirmed on Raspberry Pi 3 with the dwc_otg USB controller).
// In that case, dropping the PCM is sufficient to release resources.
if pcm.state() == State::Running {
pcm.drain().map_err(AlsaError::DrainFailure)?;
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When drain() is skipped because the PCM is not in Running state, there is no log message emitted. In the error path described in the PR (after a broken pipe triggers try_recover()), this means the skip happens silently. Since the codebase already uses warn! to log the original write error on line 503, it would be helpful to also add a debug! or warn! here to indicate that drain was intentionally skipped and the PCM will be closed by dropping — for example: "PCM not in Running state (state: {:?}), skipping drain and dropping PCM", pcm.state(). This would make diagnosing future issues on other hardware much easier.

Suggested change
if pcm.state() == State::Running {
pcm.drain().map_err(AlsaError::DrainFailure)?;
let state = pcm.state();
if state == State::Running {
pcm.drain().map_err(AlsaError::DrainFailure)?;
} else {
debug!(
"PCM not in Running state (state: {:?}), skipping drain and dropping PCM",
state
);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — added in 816a488.

}
}

Ok(())
Expand Down
Loading