]> git.alsa-project.org Git - alsa-lib.git/commitdiff
pcm: direct: roll back appl_ptr when mmap_commit fails
authorAndy Saul <asaul@brightsign.biz>
Fri, 20 Nov 2026 15:34:55 +0000 (15:34 +0000)
committerJaroslav Kysela <perex@perex.cz>
Mon, 30 Nov 2026 21:00:35 +0000 (22:00 +0100)
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 <asaul@brightsign.biz>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
src/pcm/pcm_dmix.c
src/pcm/pcm_dshare.c

index 22fcf3e5c528b96a2866dbcd082034b3a7012e86..4922b626825e7fff30197b7a7771afddd4081342 100644 (file)
@@ -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)
index 4ef158c09286032779f14631861dbe13103cde19..b0b4f5372d0bbc6b4423e912c0479e9cba760e89 100644 (file)
@@ -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)