Skip to content

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

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

fix(alsa): hanging double-opens with BufferSize::Fixed om some drivers#1214
roderickvd wants to merge 2 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
Comment on lines +1567 to 1578
// Validate the requested size against the device's supported range using the same PCM
// handle we'll use for streaming. This avoids a second PCM open (which can disturb
// hardware clock state on some drivers) while still catching wildly out-of-range
// requests before set_period_size_near silently rounds them.
let (min_buffer, max_buffer) = hw_params_buffer_size_min_max(&hw_params);
if !(min_buffer..=max_buffer).contains(&buffer_frames) {
return Err(Error::with_message(
ErrorKind::UnsupportedConfig,
format!("Buffer size {buffer_frames} is not in the supported range {min_buffer}..={max_buffer}"),
));
}
hw_params.set_buffer_size_near(DEFAULT_PERIODS * buffer_frames as alsa::pcm::Frames)?;
@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.

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