From 358b090c4e0d4ff429e365c7c3250f19700d23d3 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Wed, 1 Jul 2026 11:40:18 +1200 Subject: [PATCH] fix(posix): initialize PX4 before starting the daemon server The daemon server's client-handler thread spawns module tasks (e.g. dataman). Start px4::init_once()/init() before creating px4_daemon::Server so platform init - uORB, work queues, logging - completes with a happens-before edge to those tasks. Otherwise a module task racing the still-running init reads globals like uORB::Manager's instance pointer or the log message advertisement without synchronization (ThreadSanitizer flags it) - and an atomic would not make it correct, since the publication could simply be skipped. --- platforms/posix/src/px4/common/main.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/platforms/posix/src/px4/common/main.cpp b/platforms/posix/src/px4/common/main.cpp index e34452d5b2e..a23d4db2611 100644 --- a/platforms/posix/src/px4/common/main.cpp +++ b/platforms/posix/src/px4/common/main.cpp @@ -368,18 +368,23 @@ int main(int argc, char **argv) register_sig_handler(); set_cpu_scaling(); - px4_daemon::Server server(instance); - server.start(); - ret = create_dirs(); if (ret != PX4_OK) { return ret; } + // Initialize PX4 (uORB, work queues, logging, ...) before starting the + // daemon server. The server's client-handler thread spawns module tasks + // (e.g. dataman), so platform init must happen-before that thread is + // created. Otherwise those tasks race on globals such as the uORB::Manager + // instance pointer and the log message advertisement. px4::init_once(); px4::init(argc, argv, "px4"); + px4_daemon::Server server(instance); + server.start(); + // Don't set this up until PX4 is up and running ret = set_server_running(instance);