px4_daemon server: fix startup race condition

The FIFO was created in the server thread, and the PX4 main thread could
already have continued and started to execute the bash script.
In that case the client tried to open the FIFO but it did not exist yet.

Client error:
ERROR [px4_daemon] pipe open fail
ERROR [px4_daemon] Could not send commands
This commit is contained in:
Beat Küng
2018-08-13 13:18:08 +02:00
parent 52168f9665
commit e5ed05766d

View File

@@ -72,6 +72,15 @@ Server::~Server()
int int
Server::start() Server::start()
{ {
std::string client_send_pipe_path = get_client_send_pipe_path(_instance_id);
// Delete pipe in case it exists already.
unlink(client_send_pipe_path.c_str());
// Create new pipe to listen to clients.
// This needs to happen before we return from this method, so that the caller can launch clients.
mkfifo(client_send_pipe_path.c_str(), 0666);
if (0 != pthread_create(&_server_main_pthread, if (0 != pthread_create(&_server_main_pthread,
nullptr, nullptr,
_server_main_trampoline, _server_main_trampoline,
@@ -112,12 +121,6 @@ Server::_server_main(void *arg)
} }
std::string client_send_pipe_path = get_client_send_pipe_path(_instance_id); std::string client_send_pipe_path = get_client_send_pipe_path(_instance_id);
// Delete pipe in case it exists already.
unlink(client_send_pipe_path.c_str());
// Create new pipe to listen to clients.
mkfifo(client_send_pipe_path.c_str(), 0666);
int client_send_pipe_fd = open(client_send_pipe_path.c_str(), O_RDONLY); int client_send_pipe_fd = open(client_send_pipe_path.c_str(), O_RDONLY);
while (true) { while (true) {