fix(logger): two thread safety fixes (#29996)

* fix(logger): hold the writer lock while starting a log file

LogFileBuffer::start_log() resets _fd, _buffer, _head, _count and
_should_run without the lock, while the writer thread reads them under it
(it can already be running for the other log type). TSan flags this on
log start in SITL CI.

The PX4_CRYPTO failure path had the same problem, and could additionally
let the writer thread write to a file that is being closed.
init_logfile_encryption() also sets _min_blocksize, which the writer
reads under the lock, so it moves into the locked section as well.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Balduin <balduin@auterion.com>

* fix(logger): make the log buffer run flag atomic

is_started() reads _should_run without the writer lock, from the logger
thread and from print_status() on the shell thread. The writer thread
clears it on a write error.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Balduin <balduin@auterion.com>

* refactor(logger): use LockGuard instead of manual lock/unlock

 - Create extra scope in which to define the LockGuard
 - Replace return logic with early-return to avoid deepening scopes

---------

Signed-off-by: Balduin <balduin@auterion.com>
This commit is contained in:
Balduin authored and GitHub committed 2026-11-25 13:10:09 +01:00
1 parent 4c1d82bf37
commit 07154a8eca
2 files changed
+25 -19

No files matched your search

+23 -17
View File
@@ -38,6 +38,7 @@
#include <string.h>
#include <errno.h>
#include <containers/LockGuard.hpp>
#include <mathlib/mathlib.h>
#include <px4_platform_common/posix.h>
#include <px4_platform_common/log.h>
@@ -217,7 +218,13 @@ bool LogWriterFile::start_log(LogType type, const char *filename)
}
}
if (_buffers[(int)type].start_log(filename)) {
{
// The writer thread may already run for the other log type and reads the buffer state under the lock
LockGuard lg{_mtx};
if (!_buffers[(int)type].start_log(filename)) {
return false;
}
#if PX4_CRYPTO
bool enc_init = init_logfile_encryption(type);
@@ -225,20 +232,18 @@ bool LogWriterFile::start_log(LogType type, const char *filename)
if (!enc_init) {
PX4_ERR("Failed to start encrypted logging");
_crypto.close();
_buffers[(int)type]._should_run = false;
_buffers[(int)type]._should_run.store(false);
_buffers[(int)type].close_file();
_buffers[(int)type].reset();
return false;
}
#endif
PX4_INFO("Opened %s log file: %s", log_type_str(type), filename);
notify();
return true;
}
return false;
PX4_INFO("Opened %s log file: %s", log_type_str(type), filename);
notify();
return true;
}
int LogWriterFile::hardfault_store_filename(const char *log_file)
@@ -276,7 +281,7 @@ int LogWriterFile::hardfault_store_filename(const char *log_file)
void LogWriterFile::stop_log(LogType type)
{
lock();
_buffers[(int)type]._should_run = false;
_buffers[(int)type]._should_run.store(false);
unlock();
notify();
}
@@ -308,7 +313,8 @@ void LogWriterFile::thread_stop()
// this will terminate the main loop of the writer thread
lock();
_exit_thread.store(true);
_buffers[0]._should_run = _buffers[1]._should_run = false;
_buffers[0]._should_run.store(false);
_buffers[1]._should_run.store(false);
unlock();
notify();
@@ -338,7 +344,7 @@ void LogWriterFile::run()
bool start = false;
pthread_mutex_lock(&_mtx);
pthread_cond_wait(&_cv, &_mtx);
start = _buffers[0]._should_run || _buffers[1]._should_run;
start = _buffers[0]._should_run.load() || _buffers[1]._should_run.load();
pthread_mutex_unlock(&_mtx);
if (start) {
@@ -388,7 +394,7 @@ void LogWriterFile::run()
#endif // PX4_CRYPTO
/* if sufficient data available or partial read or terminating, write data */
if (available >= min_available[i] || is_part || (!buffer._should_run && available > 0)) {
if (available >= min_available[i] || is_part || (!buffer._should_run.load() && available > 0)) {
pthread_mutex_unlock(&_mtx);
#if defined(PX4_CRYPTO)
@@ -433,7 +439,7 @@ void LogWriterFile::run()
/* subtract bytes written from number in buffer (count -= written) */
buffer.mark_read(written);
if (!buffer._should_run && written == static_cast<int>(available) && !is_part) {
if (!buffer._should_run.load() && written == static_cast<int>(available) && !is_part) {
/* Stop only when all data written */
pthread_mutex_unlock(&_mtx);
buffer.close_file();
@@ -444,19 +450,19 @@ void LogWriterFile::run()
} else {
PX4_ERR("write failed (%i)", errno);
buffer._had_write_error.store(true);
buffer._should_run = false;
buffer._should_run.store(false);
pthread_mutex_unlock(&_mtx);
buffer.close_file();
pthread_mutex_lock(&_mtx);
buffer.reset();
}
} else if (call_fsync && buffer._should_run) {
} else if (call_fsync && buffer._should_run.load()) {
pthread_mutex_unlock(&_mtx);
buffer.fsync();
pthread_mutex_lock(&_mtx);
} else if (available == 0 && !buffer._should_run) {
} else if (available == 0 && !buffer._should_run.load()) {
pthread_mutex_unlock(&_mtx);
buffer.close_file();
pthread_mutex_lock(&_mtx);
@@ -487,7 +493,7 @@ void LogWriterFile::run()
* not an issue because notify() is called regularly.
* If the logger was switched off in the meantime, do not wait for data, instead run this loop
* once more to write remaining data and close the file. */
if (_buffers[0]._should_run || _buffers[1]._should_run) {
if (_buffers[0]._should_run.load() || _buffers[1]._should_run.load()) {
pthread_cond_wait(&_cv, &_mtx);
}
}
@@ -685,7 +691,7 @@ bool LogWriterFile::LogFileBuffer::start_log(const char *filename)
_count = 0;
_total_written.store(0);
_should_run = true;
_should_run.store(true);
return true;
}
+2 -2
View File
@@ -86,7 +86,7 @@ public:
void stop_log(LogType type);
bool is_started(LogType type) const { return _buffers[(int)type]._should_run; }
bool is_started(LogType type) const { return _buffers[(int)type]._should_run.load(); }
/** @see LogWriter::write_message() */
int write_message(LogType type, void *ptr, size_t size, uint64_t dropout_start = 0);
@@ -208,7 +208,7 @@ private:
size_t buffer_size() const { return _buffer_size; }
size_t count() const { return _count; }
bool _should_run = false;
px4::atomic_bool _should_run{false}; // read without the lock in is_started()
px4::atomic_bool _had_write_error{false};
private:
size_t _buffer_size;