From a1d6b8884f1b8ed27b7a289ebda28e51e9a4fbc3 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 31 Jul 2019 16:34:32 +0200 Subject: [PATCH] engine: fix race condition when waiting for functions Without the added lock of the message mutex it can happen that the calling thread wakes up and checks the completion condition in waitForMessagesInternal(), but the runner engine finishes and signals the condition variable in processFunctions() before the calling thread enters waiting state again. Signed-off-by: Johannes Meyer --- rtt/ExecutionEngine.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rtt/ExecutionEngine.cpp b/rtt/ExecutionEngine.cpp index aa9aa4348..5f69a809c 100644 --- a/rtt/ExecutionEngine.cpp +++ b/rtt/ExecutionEngine.cpp @@ -105,6 +105,14 @@ namespace RTT assert(foo); if ( foo->execute() == false ){ foo->unloaded(); + { + // There's no need to hold the lock while + // processing the queue. But we must hold the + // lock once between foo->execute() and the + // broadcast to avoid the race condition in + // waitForMessagesInternal(). + MutexLock locker( msg_lock ); + } msg_cond.broadcast(); // required for waitForFunctions() (3rd party thread) } else { f_queue->enqueue( foo );