diff --git a/src/drivers/pcm_buffer.cpp b/src/drivers/pcm_buffer.cpp index 3f4a0443..142a6376 100644 --- a/src/drivers/pcm_buffer.cpp +++ b/src/drivers/pcm_buffer.cpp @@ -66,10 +66,17 @@ IRAM_ATTR auto PcmBuffer::receive(std::span dest, bool isr) auto PcmBuffer::clear() -> void { while (!isEmpty()) { - size_t bytes_cleared; + size_t bytes_cleared = 0; void* data = xRingbufferReceive(ringbuf_, &bytes_cleared, 0); - vRingbufferReturnItem(ringbuf_, data); - received_ += bytes_cleared / sizeof(int16_t); + if (data) { + vRingbufferReturnItem(ringbuf_, data); + received_ += bytes_cleared / sizeof(int16_t); + } else { + // Defensively guard against looping forever if for some reason the + // buffer isn't draining. + ESP_LOGW(kTag, "PcmBuffer not draining"); + break; + } } } diff --git a/src/tangara/audio/track_queue.cpp b/src/tangara/audio/track_queue.cpp index 5d730a09..6faa5340 100644 --- a/src/tangara/audio/track_queue.cpp +++ b/src/tangara/audio/track_queue.cpp @@ -290,6 +290,7 @@ auto TrackQueue::finish() -> void { auto TrackQueue::clear() -> void { { const std::unique_lock lock(mutex_); + position_ = 0; playlist_.clear(); opened_playlist_.reset(); if (shuffle_) {