Skip to content

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

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

fix(alsa): hanging double-opens with BufferSize::Fixed om some drivers#1214
roderickvd wants to merge 3 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
Comment on lines 1554 to +1563
if let BufferSize::Fixed(buffer_frames) = config.buffer_size {
hw_params.set_buffer_size_near(DEFAULT_PERIODS * buffer_frames as alsa::pcm::Frames)?;
hw_params
.set_period_size_near(buffer_frames as alsa::pcm::Frames, alsa::ValueOr::Nearest)?;
let buffer_frames = buffer_frames as alsa::pcm::Frames;

// Validate the requested size against the device's supported ranges 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.
if let Some((min_period, max_period)) = hw_params_period_size_min_max(&hw_params) {
if !(min_period..=max_period).contains(&buffer_frames) {
return Err(Error::with_message(
Comment thread CHANGELOG.md
- **ALSA**: Fix stream corruption on certain drivers with spurious wakeups.
- **ALSA**: Fix callbacks firing before `build_*_stream` returns the `Stream` handle.
- **ALSA**: Fix `BufferSize::Fixed` validation opening the PCM device a second time.
- **ALSA**: Fix hang on when device raced to an error state without delivering POLLERR.
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