From: Andy Saul Date: Fri, 20 Nov 2026 15:34:55 +0000 (+0000) Subject: pcm: direct: roll back appl_ptr when mmap_commit fails X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=9a87ee54bfca5208c0d710aff2de1ad8ccdddf38;p=alsa-lib.git pcm: direct: roll back appl_ptr when mmap_commit fails dmix and dshare forward their appl_ptr in mmap_commit before they start the timer or sync the hardware pointer. If either step fails, for example when sync_ptr finds that the buffer ran dry and returns -EPIPE, the commit returns the error but leaves appl_ptr forwarded. The frames are never mixed or copied. A plugin such as softvol above dmix does not forward its own appl_ptr when the slave commit fails, so the two now differ. The next snd_pcm_status() aborts in snd_pcm_plugin_status() on pcm_plugin.c: snd_pcm_plugin_status: Assertion `status->appl_ptr == *pcm->appl.ptr' failed. The plugin calls avail_update on dmix before it converts and commits, so this needs the buffer to run dry between those two calls. A thread preempted inside the plugin's commit while the buffer is nearly empty is enough. Move appl_ptr back by the committed size when start_timer or sync_ptr fails, so a failed commit leaves no trace, as the early check_xrun return already does. Closes: https://github.com/alsa-project/alsa-lib/pull/542 Signed-off-by: Andy Saul Signed-off-by: Jaroslav Kysela --- diff --git a/src/pcm/pcm_dmix.c b/src/pcm/pcm_dmix.c index 22fcf3e5..4922b626 100644 --- a/src/pcm/pcm_dmix.c +++ b/src/pcm/pcm_dmix.c @@ -815,11 +815,11 @@ static snd_pcm_sframes_t snd_pcm_dmix_mmap_commit(snd_pcm_t *pcm, snd_pcm_mmap_appl_forward(pcm, size); if (dmix->state == STATE_RUN_PENDING) { if ((err = snd_pcm_dmix_start_timer(pcm, dmix)) < 0) - return err; + goto _undo; } else if (dmix->state == SND_PCM_STATE_RUNNING || dmix->state == SND_PCM_STATE_DRAINING) { if ((err = snd_pcm_dmix_sync_ptr(pcm)) < 0) - return err; + goto _undo; } if (dmix->state == SND_PCM_STATE_RUNNING || dmix->state == SND_PCM_STATE_DRAINING) { @@ -831,6 +831,13 @@ static snd_pcm_sframes_t snd_pcm_dmix_mmap_commit(snd_pcm_t *pcm, snd_pcm_direct_clear_timer_queue(dmix); } return size; + + _undo: + /* nothing was mixed, so the caller must not see these frames as + * committed. A plugin above us keeps its appl_ptr in step with ours. + */ + snd_pcm_mmap_appl_backward(pcm, size); + return err; } static snd_pcm_sframes_t snd_pcm_dmix_avail_update(snd_pcm_t *pcm) diff --git a/src/pcm/pcm_dshare.c b/src/pcm/pcm_dshare.c index 4ef158c0..b0b4f537 100644 --- a/src/pcm/pcm_dshare.c +++ b/src/pcm/pcm_dshare.c @@ -520,11 +520,11 @@ static snd_pcm_sframes_t snd_pcm_dshare_mmap_commit(snd_pcm_t *pcm, if (dshare->state == STATE_RUN_PENDING) { err = snd_pcm_dshare_start_timer(pcm, dshare); if (err < 0) - return err; + goto _undo; } else if (dshare->state == SND_PCM_STATE_RUNNING || dshare->state == SND_PCM_STATE_DRAINING) { if ((err = snd_pcm_dshare_sync_ptr(pcm)) < 0) - return err; + goto _undo; } if (dshare->state == SND_PCM_STATE_RUNNING || dshare->state == SND_PCM_STATE_DRAINING) { @@ -536,6 +536,13 @@ static snd_pcm_sframes_t snd_pcm_dshare_mmap_commit(snd_pcm_t *pcm, snd_pcm_direct_clear_timer_queue(dshare); } return size; + + _undo: + /* nothing was copied, so the caller must not see these frames as + * committed. A plugin above us keeps its appl_ptr in step with ours. + */ + snd_pcm_mmap_appl_backward(pcm, size); + return err; } static snd_pcm_sframes_t snd_pcm_dshare_avail_update(snd_pcm_t *pcm)