Skip to content

fix(alsa): hanging double-opens with BufferSize::Fixed om some drivers#1214

Open
roderickvd wants to merge 6 commits into
masterfrom
fix/alsa-double-open
Open

fix(alsa): hanging double-opens with BufferSize::Fixed om some drivers#1214
roderickvd wants to merge 6 commits into
masterfrom
fix/alsa-double-open

Conversation

@roderickvd
Copy link
Copy Markdown
Member

The range validation for BufferSize::Fixed was calling default_output_config() before opening the stream, which opened and closed the PCM device once just to get the buffer size range. This may leave the driver in a bad state for the subsequent streaming open.

Second, to defend against hanging processes in such an event, the worker now checks PCM state when poll() times out.

Fixes #1157

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the ALSA backend to avoid an extra open/close cycle when validating BufferSize::Fixed (which can leave some drivers in a bad state), and adds a defensive PCM-state check when poll() times out to prevent hangs when error events aren’t delivered.

Changes:

  • Move BufferSize::Fixed range validation to set_hw_params_from_format() so it uses the same PCM handle as the streaming open (avoids a second open/close).
  • On poll() timeout, query PCM state and surface Disconnected / XRun / Suspended conditions even if POLLERR/POLLHUP wasn’t delivered.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/host/alsa/mod.rs Outdated
@roderickvd roderickvd changed the title fix(alsa): hanging double-opens with fixed buffersize om some drivers fix(alsa): hanging double-opens with BufferSize::Fixed om some drivers May 20, 2026
@DanielHabenicht
Copy link
Copy Markdown

As I wrote in the issue there are no apparent changes for me, but it also does not negatively effect the current behavior on the rasperry pi zero w, so I would say its better than before.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/host/alsa/mod.rs Outdated
Comment thread CHANGELOG.md Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/host/alsa/mod.rs Outdated
Comment thread src/host/alsa/mod.rs Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread src/host/alsa/mod.rs
Comment on lines 518 to 520
let min_channels = hw_params.get_channels_min()?;
let max_channels = hw_params.get_channels_max()?;
let max_channels = hw_params.get_channels_max()?.min(32); // TODO: cap at 32 or too many configs

Comment thread src/host/alsa/mod.rs
Err(err) if err.errno() == libc::EPIPE => {
return Err(Error::with_message(ErrorKind::Xrun, err.to_string()))
}
Err(err) if err.errno() == libc::EPIPE => return Err(ErrorKind::Xrun.into()),
Comment thread src/host/alsa/mod.rs
Comment on lines 1682 to 1691
impl From<alsa::Error> for Error {
fn from(err: alsa::Error) -> Self {
match err.errno() {
libc::ENODEV | libc::ENOENT | LIBC_ENOTSUPP => {
Error::with_message(ErrorKind::DeviceNotAvailable, err.to_string())
}
libc::EPERM | libc::EACCES => {
Error::with_message(ErrorKind::PermissionDenied, err.to_string())
}
libc::EBUSY | libc::EAGAIN => {
Error::with_message(ErrorKind::DeviceBusy, err.to_string())
}
libc::EINVAL => Error::with_message(ErrorKind::InvalidInput, err.to_string()),
libc::EPIPE => Error::with_message(ErrorKind::Xrun, err.to_string()),
libc::ENOSYS => Error::with_message(ErrorKind::UnsupportedOperation, err.to_string()),
libc::ENODEV | libc::ENOENT | LIBC_ENOTSUPP => ErrorKind::DeviceNotAvailable.into(),
libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied.into(),
libc::EBUSY | libc::EAGAIN => ErrorKind::DeviceBusy.into(),
libc::EINVAL | libc::ENOSYS => ErrorKind::UnsupportedConfig.into(),
libc::EPIPE => ErrorKind::Xrun.into(),
_ => Error::with_message(ErrorKind::BackendError, err.to_string()),
}
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment thread src/host/alsa/mod.rs
Comment thread src/host/alsa/mod.rs
Comment thread src/host/alsa/mod.rs
libc::ENODEV | libc::ENOENT | LIBC_ENOTSUPP => ErrorKind::DeviceNotAvailable.into(),
libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied.into(),
libc::EBUSY | libc::EAGAIN => ErrorKind::DeviceBusy.into(),
libc::EINVAL => ErrorKind::UnsupportedConfig.into(),
Comment thread src/lib.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

alsa: xrun on RPi Zero since v0.17

3 participants