8000 Logger is not thread safe · Issue #5102 · esphome/issues · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Logger is not thread safe #5102
Closed
esphome/esphome
#8765
@rguca

Description

@rguca

The problem

If a task is created using xTaskCreate() and the task creates log entries with ESP_LOG, not all will be visible in the log.

Reason:
The logger omits the entry, if another task is creating an entry at the same time.
recursion_guard_
https://github.com/esphome/esphome/blob/45276cc24452b7ea1a7a0c6f88ed11ca38a4a1e6/esphome/components/logger/logger.cpp#L67

Possible fix:
Replace recursion_guard_ with a mutex created by xSemaphoreCreateMutex and claim/release it with xSemaphoreTake and xSemaphoreGive.

Working PoC:

static SemaphoreHandle_t log_guard = xSemaphoreCreateMutex();

void HOT Logger::log_vprintf_(int level, const char *tag, int line, const char *format, va_list args) {  // NOLINT
  if (level > this->level_for(tag))
    return;
  xSemaphoreTake(log_guard, portMAX_DELAY);
  this->reset_buffer_();
  this->write_header_(level, tag, line);
  this->vprintf_to_buffer_(format, args);
  this->write_footer_();
  this->log_message_(level, tag);
  xSemaphoreGive(log_guard);
}

Which version of ESPHome has the issue?

2023.10.6

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

No response

What platform are you using?

ESP32

Board

esp32dev

Component causing the issue

logger

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0