From: GitHub pull_request - opened <github@alsa-project.org>
To: alsa-devel@alsa-project.org
Subject: pcm: direct: roll back appl_ptr when mmap_commit fails
Date: Mon, 23 Nov 2026 10:25:09 +0100 (CET) [thread overview]
Message-ID: <18eaa259224c1400-webhooks-bot@alsa-project.org> (raw)
In-Reply-To: <alsa-project/alsa-lib/pr/542@alsa-project.org>
alsa-project/alsa-lib pull request #542 was opened from andy-brightsign:
dmix and dshare move their `appl_ptr` forward in `mmap_commit` before they start the timer or sync the hardware pointer. If either step fails, the commit returns the error but leaves `appl_ptr` forwarded, although the frames were never mixed or copied. The usual case is `sync_ptr` finding that the buffer has run dry and returning `-EPIPE`.
A `snd_pcm_plugin` based PCM above (softvol, route, linear and so on) does not move its own `appl_ptr` when the slave commit fails, so the two pointers now differ. The next `snd_pcm_status()` aborts:
```text
pcm_plugin.c:547: snd_pcm_plugin_status: Assertion `status->appl_ptr == *pcm->appl.ptr' failed.
```
This is the same assert as #541 but a different cause. #541 is about mmap_emul. This one needs only a plugin over dmix, which is the usual shape of a plug or softvol config.
The plugin calls `avail_update` on dmix before it converts and commits, and that call catches an xrun that has already happened. So the assert 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. We hit it in the field with mpv on an RK3399 player (alsa-lib 1.2.11), playing through plug → softvol → dmix after a run of underruns. mpv calls `snd_pcm_status()` in `recover_and_get_state()` right after a write fails with `-EPIPE`.
The patch moves `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. Nothing has been mixed or copied at that point, because `sync_area` runs only on success.
**Reproducer**
`dmix-stall.c` (below) opens a PCM for MMAP access with the default stop_threshold, queues one period, starts the stream and commits a 256 frame chunk. It defines its own `snd_pcm_mmap_commit()`, which libasound calls through its PLT, so the plugin's call to dmix lands in the program first. The program sleeps once when the target is a dmix or dshare PCM, then calls the real function. That models the thread being preempted inside the plugin's commit. After `-EPIPE` it calls `snd_pcm_status()`, as mpv does. In `app` mode it sleeps in the application instead, between `mmap_begin` and `mmap_commit`, which is the harmless case.
```sh
gcc -O2 -Wall -o dmix-stall dmix-stall.c -lasound -ldl
ALSA_CONFIG_PATH=/usr/share/alsa/alsa.conf:repro.conf ./dmix-stall repro 50 256 3 dmix
ALSA_CONFIG_PATH=/usr/share/alsa/alsa.conf:repro.conf ./dmix-stall repro_dshare 50 256 3 dmix
```
Usage is `dmix-stall [device] [stall_ms] [chunk_frames] [iterations] [app|dmix] [nth]`. `repro.conf` (below) defines `repro` as route over dmix and `repro_dshare` as route over dshare. Set its `hw:1,0` to a free playback device.
**Testing**
x86_64, Linux 6.1, an HDA card, S16_LE stereo at 48 kHz, buffer 16384, period 1024, chunk 256, 3 iterations per run.
| PCM | Stall | master f44e0abd | With this patch |
| :--- | :--- | :--- | :--- |
| route over dmix | 50 ms in the chain | aborts on iteration 1, 5/5 runs | 3/3 `-EPIPE` then XRUN status, 0/5 runs abort |
| route over dmix | 5 ms in the chain | 3/3 ok | 3/3 ok |
| route over dmix | 50 ms in the app | 3/3 `-EPIPE`, no abort | 3/3 `-EPIPE`, no abort |
| route over dmix | none | 3/3 ok | 3/3 ok |
| route over dshare | 50 ms in the chain | aborts on iteration 1, every run that opened (6) | 3/3 `-EPIPE` then XRUN status, 0/5 runs abort |
| route over dshare | none | not run | 3/3 ok |
On master, a dshare run started straight after an aborted one fails in `snd_pcm_open()` with `-EINVAL`, so those runs are not counted.
The same change on alsa-lib 1.2.11 was also tested on the RK3399 player, through plug → multi → three legs of plug → softvol → dmix. The reproducer gave `-EPIPE` and an XRUN status with no abort on every leg. Unpatched 1.2.11 aborted on iteration 1 when the first leg stalled. aplay showed no underruns idle or under CPU load.
<details>
<summary>dmix-stall.c</summary>
```c
/*
* Reproducer for the snd_pcm_plugin_status() appl_ptr assert through dmix.
*
* dmix's mmap_commit moves its appl_ptr forward and only then syncs the
* hardware pointer. If the client buffer ran dry while the caller was between
* avail_update and mmap_commit, sync_ptr returns -EPIPE after the forward.
* A snd_pcm_plugin based PCM above (route, softvol, linear and so on) sees
* the error and does not move its own appl_ptr, so the next snd_pcm_status()
* asserts in snd_pcm_plugin_status().
*
* This program opens the window on purpose. It queues a short burst, starts
* the stream, then commits a chunk with a stall of longer than the queued
* audio plus the chunk. That is what a preempted player thread does under
* load. After an -EPIPE it calls snd_pcm_status() the way mpv's
* recover_and_get_state() does.
*
* Stall modes:
* app stall in the application between mmap_begin and mmap_commit.
* The plugin's mmap_commit calls avail_update on dmix first, so dmix
* sees the xrun before it moves any pointer. Expected: no assert.
* dmix stall inside the chain, right before the plugin's commit reaches
* dmix (after the plugin's avail_update on dmix). This program
* defines snd_pcm_mmap_commit(), which libasound calls through its
* PLT, and sleeps once when the target is a dmix PCM. That models the
* thread being preempted while the plugin processes the chunk.
*
* The optional nth argument (dmix mode only, default 1) stalls on the Nth
* DMIX commit of the chunk instead of the first. Through a multi PCM the legs
* commit in order, so nth 2 stalls leg 1 after leg 0 has already committed.
*
* Usage: dmix-stall [device] [stall_ms] [chunk_frames] [iterations] [app|dmix] [nth]
*
* Build: gcc -O2 -Wall -o dmix-stall dmix-stall.c -lasound -ldl
* Run: ALSA_CONFIG_PATH=<prefix>/share/alsa/alsa.conf:repro.conf ./dmix-stall repro 50 256 3 dmix
* where repro.conf defines "repro" as a route PCM over a dmix PCM.
*/
#define _GNU_SOURCE
#include <alsa/asoundlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int dmix_stall_ms; /* armed for one dmix commit when > 0 */
static int dmix_nth = 1; /* which dmix commit of the chunk to stall */
static int dmix_seen; /* dmix commits seen since arming */
snd_pcm_sframes_t snd_pcm_mmap_commit(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
snd_pcm_uframes_t frames)
{
static snd_pcm_sframes_t (*real)(snd_pcm_t *, snd_pcm_uframes_t, snd_pcm_uframes_t);
if (!real)
real = dlvsym(RTLD_NEXT, "snd_pcm_mmap_commit", "ALSA_0.9");
if (dmix_stall_ms > 0 &&
(snd_pcm_type(pcm) == SND_PCM_TYPE_DMIX || snd_pcm_type(pcm) == SND_PCM_TYPE_DSHARE) &&
++dmix_seen == dmix_nth) {
usleep(dmix_stall_ms * 1000);
dmix_stall_ms = 0;
}
return real(pcm, offset, frames);
}
static void die(const char *what, int err)
{
fprintf(stderr, "%s: %s\n", what, snd_strerror(err));
exit(1);
}
/* Write frames of silence through the mmap API. Returns the commit result. */
static snd_pcm_sframes_t mmap_write(snd_pcm_t *pcm, snd_pcm_uframes_t want, int stall_ms)
{
const snd_pcm_channel_area_t *areas;
snd_pcm_uframes_t offset, frames = want;
snd_pcm_sframes_t avail;
unsigned int ch;
int err;
avail = snd_pcm_avail_update(pcm);
if (avail < 0)
return avail;
err = snd_pcm_mmap_begin(pcm, &areas, &offset, &frames);
if (err < 0)
return err;
for (ch = 0; ch < 2; ch++)
snd_pcm_areas_silence(&areas[ch], offset, 1, frames, SND_PCM_FORMAT_S16_LE);
if (stall_ms)
usleep(stall_ms * 1000);
return snd_pcm_mmap_commit(pcm, offset, frames);
}
int main(int argc, char **argv)
{
const char *dev = argc > 1 ? argv[1] : "repro";
int stall_ms = argc > 2 ? atoi(argv[2]) : 50;
snd_pcm_uframes_t chunk = argc > 3 ? strtoul(argv[3], NULL, 0) : 256;
int iterations = argc > 4 ? atoi(argv[4]) : 5;
int in_dmix = !(argc > 5 && strcmp(argv[5], "app") == 0);
if (argc > 6)
dmix_nth = atoi(argv[6]);
snd_pcm_hw_params_t *hw;
snd_pcm_sw_params_t *sw;
snd_pcm_status_t *st;
snd_pcm_uframes_t buffer, period;
snd_pcm_t *pcm;
int err, i, epipe = 0;
err = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0);
if (err < 0)
die("open", err);
snd_pcm_hw_params_alloca(&hw);
snd_pcm_hw_params_any(pcm, hw);
if ((err = snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0)
die("access", err);
if ((err = snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_S16_LE)) < 0)
die("format", err);
if ((err = snd_pcm_hw_params_set_channels(pcm, hw, 2)) < 0)
die("channels", err);
if ((err = snd_pcm_hw_params_set_rate(pcm, hw, 48000, 0)) < 0)
die("rate", err);
if ((err = snd_pcm_hw_params(pcm, hw)) < 0)
die("hw_params", err);
snd_pcm_hw_params_get_buffer_size(hw, &buffer);
snd_pcm_hw_params_get_period_size(hw, &period, NULL);
/* Default stop_threshold (= buffer_size), as mpv leaves it. */
snd_pcm_sw_params_alloca(&sw);
snd_pcm_sw_params_current(pcm, sw);
snd_pcm_sw_params_set_start_threshold(pcm, sw, buffer);
if ((err = snd_pcm_sw_params(pcm, sw)) < 0)
die("sw_params", err);
snd_pcm_status_alloca(&st);
printf("%s: buffer %lu period %lu, burst %lu, stall %d ms in %s (commit %d), chunk %lu\n",
dev, buffer, period, period, stall_ms, in_dmix ? "dmix" : "app", dmix_nth, chunk);
fflush(stdout);
for (i = 1; i <= iterations; i++) {
snd_pcm_sframes_t r;
if ((err = snd_pcm_prepare(pcm)) < 0)
die("prepare", err);
/* Queue one period and start, so the buffer is nearly empty. */
r = mmap_write(pcm, period, 0);
if (r < 0)
die("first commit", r);
if ((err = snd_pcm_start(pcm)) < 0)
die("start", err);
/* Stall inside the avail_update..mmap_commit window. */
if (in_dmix) {
dmix_seen = 0;
dmix_stall_ms = stall_ms;
}
r = mmap_write(pcm, chunk, in_dmix ? 0 : stall_ms);
dmix_stall_ms = 0;
printf("iteration %d: commit after stall returned %ld (%s)\n", i, (long)r,
r < 0 ? snd_strerror(r) : "ok");
fflush(stdout);
if (r == -EPIPE)
epipe++;
/* What mpv's recover_and_get_state() does next. */
err = snd_pcm_status(pcm, st);
printf("iteration %d: status ok (err %d), state %s, delay %ld avail %lu\n", i, err,
snd_pcm_state_name(snd_pcm_status_get_state(st)),
(long)snd_pcm_status_get_delay(st), (unsigned long)snd_pcm_status_get_avail(st));
fflush(stdout);
snd_pcm_drop(pcm);
}
printf("done: %d/%d commits returned -EPIPE, no assert\n", epipe, iterations);
snd_pcm_close(pcm);
return 0;
}
```
</details>
<details>
<summary>repro.conf</summary>
```text
# Set slave.pcm to a free playback device on your machine.
pcm.repro_dmix {
type dmix
ipc_key 22705
slave {
pcm "hw:1,0"
format S16_LE
channels 2
rate 48000
period_size 1024
buffer_size 16384
}
}
pcm.repro {
type route
slave {
pcm "repro_dmix"
channels 2
}
ttable.0.0 1
ttable.1.1 1
}
pcm.repro_dshare_slave {
type dshare
ipc_key 22706
bindings.0 0
bindings.1 1
slave {
pcm "hw:1,0"
format S16_LE
channels 2
rate 48000
period_size 1024
buffer_size 16384
}
}
pcm.repro_dshare {
type route
slave {
pcm "repro_dshare_slave"
channels 2
}
ttable.0.0 1
ttable.1.1 1
}
```
</details>
Signed-off-by: Andy Saul <asaul@brightsign.biz>
Request URL : https://github.com/alsa-project/alsa-lib/pull/542
Patch URL : https://github.com/alsa-project/alsa-lib/pull/542.patch
Repository URL: https://github.com/alsa-project/alsa-lib
parent reply other threads:[~2026-11-23 9:25 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <alsa-project/alsa-lib/pr/542@alsa-project.org>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=18eaa259224c1400-webhooks-bot@alsa-project.org \
--to=github@alsa-project.org \
--cc=alsa-devel@alsa-project.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.