From ab2cae5746e005db3f7c21c49b5635899cf832b2 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Tue, 7 Apr 2026 18:35:06 +0800 Subject: [PATCH] Fix crash when adjusting volume via button while Es8311 audio device is disabled (#1917) * Initial plan * Fix crash when adjusting volume via button while audio device is disabled When both input and output are disabled, UpdateDeviceState() sets dev_ to nullptr. Pressing a volume button calls SetOutputVolume() which previously called esp_codec_dev_set_out_vol(dev_, volume) without null-checking dev_, causing a crash via ESP_ERROR_CHECK. Fix: Add null guard for dev_ and mutex lock for thread safety. The volume is still saved via AudioCodec::SetOutputVolume() and will be applied when the device is reopened by UpdateDeviceState(). Agent-Logs-Url: https://github.com/78/xiaozhi-esp32/sessions/945c653a-ed16-49af-aefe-5cfb473402c6 Co-authored-by: 78 <4488133+78@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 78 <4488133+78@users.noreply.github.com> --- main/audio/codecs/es8311_audio_codec.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main/audio/codecs/es8311_audio_codec.cc b/main/audio/codecs/es8311_audio_codec.cc index 708b7da..12ecd62 100644 --- a/main/audio/codecs/es8311_audio_codec.cc +++ b/main/audio/codecs/es8311_audio_codec.cc @@ -156,7 +156,10 @@ void Es8311AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp } void Es8311AudioCodec::SetOutputVolume(int volume) { - ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(dev_, volume)); + std::lock_guard lock(data_if_mutex_); + if (dev_ != nullptr) { + ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(dev_, volume)); + } AudioCodec::SetOutputVolume(volume); }