Repository navigation
Description
Summary
A USB DAC powered off mid-playback and powered back on never returns to the
running player. AlsaAudioSink::recover_() treats -ENODEV as a failed write
rather than a dead device: it logs, returns false, and leaves pcm_ open. The
sink then holds a handle to hardware that is gone, and nothing during a run
retires it, so the only fix is restarting the process.
The reason this reads as a gap rather than a feature request: SinkRecovery
already encodes the policy for exactly this case, and the other three sinks use
it. ALSA is the one backend that does not.
Reported downstream as music-assistant/local-audio-addon#28.
Environment
| sendspin-cli | v0.1.4, via ghcr.io/music-assistant/local-audio-addon:0.1.8 |
| Host | Debian + Docker (Compose), /dev/snd passed through |
| Output | SENDSPIN_OUTPUT=hw:CARD=DA100 (-o hw:CARD=DA100) |
| Sink | SMSL DA100, USB |
| Server | Music Assistant |
Read at v0.1.5; recover_() and configure() are unchanged from v0.1.4
(the only alsa_sink.cpp change between the tags is inside list_devices()), so
line numbers below are v0.1.5's and the analysis applies to the current tag.
Timeline
Powering the amp off:
E audio: alsa: No such device
repeating from then on. Powering it back on brings the device back on the host
and inside the container -- aplay -l shows it -- and the player keeps logging
the same line. Restarting the container fixes it immediately.
Where the line comes from
log.cpp:121 writes "%s%c %s: %s\n", so that is level ERROR, tag audio,
message body exactly alsa: No such device -- snd_strerror(-ENODEV) with
nothing around it. There are two "alsa: %s" format strings in the tree:
alsa_sink.cpp:496 at ERROR with snd_strerror(err), and alsa_sink.cpp:733
at DEBUG, which can only print muted or unmuted. Only the first fits, and
the level letter agrees.
That matters because every other ALSA error in the file wraps the strerror string
in more text -- alsa: cannot open '<dev>': ... at 328, alsa: '<dev>' is not open -- discarding audio... at 557. The bare form is neither a reopen being
refused nor a sink that has noticed it has no device. It is a call made on a
handle that is still open, against hardware that is gone.
What is happening
recover_() (456) handles -EINTR/-EAGAIN, -EPIPE and -ESTRPIPE.
Everything else falls through to
cli_log(LogLevel::ERROR, "alsa: %s", snd_strerror(err));
return false;at 496. -ENODEV lands there. pcm_ is left open, failed_ is left clear, and
the sink records nothing about having hit an unrecoverable error.
Its three callers are all in write()'s inner loop -- snd_pcm_avail_update()
at 615, snd_pcm_wait() at 630, snd_pcm_writei() at 642 -- and each treats
false as break. frames_done stays 0, so write() returns 0 (677), the sync
task re-presents the same buffer, and the ERROR at 496 has no throttle. Hence
one line per retry rather than one line per outage.
write()'s pcm_ == nullptr branch at 552 is the code that would cope with
this: it sets failed_, logs once behind failed_.exchange(true), and discards
rather than returning 0 -- its comment says returning 0 forever "would spin the
sync task on a buffer it can never hand off". It is unreachable here, because the
handle outlives the hardware.
close_device_() is the only thing that closes and nulls pcm_. During a run it
is reached only from configure() (519, 529, and 378/391/417/425 inside
open_device_(), which configure() alone calls); the rest are the destructor
and stop(), which runs only at shutdown (main.cpp:1036). configure() has a
single caller, player_listener.cpp:73, on a stream announcing its parameters.
So a stream that simply keeps running never reaches anything that could retire
the handle.
The recovery for this already exists
sink_recovery.h's class docstring names this scenario outright -- "a USB DAC
pulled out and pushed back in, a sound server being restarted". SinkRecovery
encodes the inline reopen from write(), the rescan from poll() behind a 2 s
delay doubling to a 30 s ceiling, and the five-attempt per-stream budget.
PulseAudioSink, PipeWireSink and PortAudioSink each hold a SinkRecovery recovery_ (pulse_sink.h:311, pipewire_sink.h:302, portaudio_sink.h:283).
AlsaAudioSink holds none. It is also the only sink with a real device that does
not override poll() -- NullAudioSink is the other, and it has nothing to
lose -- so the tick main.cpp:991 already hands every sink goes unused here.
What it does not appear to be
- Not a failed reopen. The bare
"alsa: %s"form rules that out; a refused
open logsalsa: cannot open '<dev>': ...at 328 instead. - Not the container losing the device.
docker-compose.ymlresolves
devices: - /dev/snd:/dev/sndat container start, so a DAC returning on a
different card number could fail to reappear inside the container -- but the
reporter saysaplay -lshows it there, andhw:CARD=survives an index
change anyway. I have asked them to confirm. - Not fixed by upgrading.
recover_()andconfigure()are identical
betweenv0.1.4andv0.1.5. - Not something the downstream add-on can catch. Its health check asks
sendspin-cli status, which reports the output as healthy throughout -- see
ask 3.
Asks
The shape is yours to choose; this is what I think would close it.
1. Wire AlsaAudioSink into SinkRecovery, as the other three sinks are.
Roughly the PulseAudioSink shape: recover_() records the failure on a
device-level error instead of returning a bare false; write() attempts the
inline reopen via reopen_due()/reopen_done() (pulse_sink.cpp:452, 901-925);
a new AlsaAudioSink::poll() does the close-and-reopen off the sync thread via
rescan_due()/rescan_done() (639-688); and configure() calls reset() on a
stream that really opened (440). No new policy and no new constants.
PulseAudioSink rather than PortAudioSink is the model, and the reason is
rescan_done() rather than threading. ALSA's second attempt is a fresh
snd_pcm_open(), which re-resolves hw:CARD=DA100 at open time, so a device
absent now may be present in four seconds -- the repeatable class the class
docstring describes as "a reconnect that failed leaves everything to try". ALSA
would therefore need to report the true outcome to rescan_done(); reporting
true unconditionally the way PortAudioSink does would collapse the budget to
one attempt and reintroduce this bug for any outage longer than a tick.
Two things the Pulse shape does not carry over, and both are calls I would
rather you made than guessed at:
close_device_()destroys the format the reopen needs. It zeroesrate_,
channels_,bits_andbytes_per_frame_(449-453).PulseAudioSinkand
PipeWireSinkreopen from alast_format_(pulse_sink.h:308,
pipewire_sink.h:299);AlsaAudioSinkhas no equivalent, so the wiring needs
one, or needs the format captured before the close.- The inline attempt may not be cheap enough for ALSA. Pulse's is cheap
because its context is already up --pulse_sink.h:72-74is explicit that
"write() never reconnects". ALSA's equivalent is the blocking
snd_pcm_open()at 326 (flags0) on an exclusivehw:PCM, from the sync
task's thread. The sink already treats that as a hazard elsewhere:probe()
passesSND_PCM_NONBLOCKat 237 "so probing a busy exclusive device returns
instead of parking the startup path on it". So ALSA may want to skip the inline
attempt and escalate straight topoll(), the way Pulse does when its context
is down. Your call.
2. configure() should honour that record. Its same-format fast path drops
and re-prepares the existing pcm_, reopening only if that snd_pcm_prepare()
happens to fail. On a removed device it would be expected to fail, so a fresh
stream may well recover today -- but by accident rather than by design. Once
recover_() records the failure there is a fact for the fast path to consult.
3. Give format_status() a sink-health field. status currently prints
output: hw:CARD=DA100 (44100 Hz / 2 ch / 16-bit)
from sink_.name() (main.cpp:497) and the negotiated stream format -- and it
prints exactly that for as long as the outage lasts, since nothing ever retires
the handle. Nothing in the snapshot asks the sink whether it is working, so
status reports a healthy output when there is not one. That is true on any
host, container or no.
Most of the state exists: failed_ is on all four sinks, and
SinkRecovery::pending() is documented as the one method safe to call without
the lock. A virtual bool healthy() const { return true; } on AudioSink,
overridden per sink and surfaced as a suffix on the existing output line or as
its own output-state: line, would cover it. It would also give the Music
Assistant add-on's health check something to look at, which is how this was
found -- that check cannot probe the device itself, since a hw: PCM is
exclusive and the probe would become a second consumer of it.
Open questions
- Does the same thing happen on
-o pulseand-o pipewirewhen the device
rather than the daemon disappears? Those sinks haveSinkRecovery, so they
should recover, but I have not tested a USB DAC pull under either. - Which errno set counts as device-level?
-ENODEVand-ENXIOseem
unambiguous.-EIOI have deliberately left out of ask 1: it also covers
transient faults aprepare()can clear, and closing the handle on those would
reopen a device that did not need it. Your call whether it belongs. - Should the reopen re-resolve
hw:CARD=NAMEagainst a rebuilt device list, or
is reopening the same name string sufficient? The by-name form ought to survive
a card-index change, but I have not checked what alsa-lib caches.
Reproduction
Not reduced to a script -- reported once, reproduced from the code rather than on
hardware. Powering off a USB DAC mid-stream with -o hw:CARD=<name> should do
it; snd_pcm_avail_update() returning -ENODEV is the entry point.
Activity
Wire AlsaAudioSink into SinkRecovery so a replugged device recovers