Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[22624] System Log error: infinite loop when setting thread affinity fails #5611

Merged
merged 9 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/cpp/utils/threading/threading.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*!
* @file threading.hpp
*/

#define THREAD_EPROSIMA_LOG_ERROR(thread_name, msg) \
do{ \
if (strcmp(thread_name, "dds.log") == 0) \
{ \
std::cerr << msg << std::endl; \
} \
else \
{ \
EPROSIMA_LOG_ERROR(SYSTEM, msg); \
} \
} while (0)
9 changes: 5 additions & 4 deletions src/cpp/utils/threading/threading_osx.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/attributes/ThreadSettings.hpp>
#include <utils/threading/threading.hpp>

namespace eprosima {

Expand Down Expand Up @@ -98,13 +99,13 @@ static void configure_current_thread_scheduler(
result = setpriority(PRIO_PROCESS, tid, sched_priority);
if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set priority of thread with id [" << tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set priority of thread with id [" << tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
result) << "'");
}
}
else if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
result) << "'");
}
}
Expand All @@ -118,7 +119,7 @@ static void configure_current_thread_scheduler(
result = pthread_setschedparam(self_tid, sched_class, &param);
if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << " with priority " << param.sched_priority << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << " with priority " << param.sched_priority << ". Error '" << strerror(
result) << "'");
}
}
Expand All @@ -138,7 +139,7 @@ static void configure_current_thread_affinity(
1);
if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity << ". Error '" << strerror(
result) << "'");
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/cpp/utils/threading/threading_pthread.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/attributes/ThreadSettings.hpp>
#include <utils/threading/threading.hpp>

#if defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ <= 30)))
#include <sys/syscall.h>
Expand Down Expand Up @@ -114,13 +115,13 @@ static void configure_current_thread_scheduler(
result = setpriority(PRIO_PROCESS, gettid(), sched_priority);
if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set priority of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set priority of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
result) << "'");
}
}
else if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
result) << "'");
}
}
Expand All @@ -135,7 +136,7 @@ static void configure_current_thread_scheduler(
result = pthread_setschedparam(self_tid, sched_class, &param);
if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << " with priority " << param.sched_priority << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << " with priority " << param.sched_priority << ". Error '" << strerror(
result) << "'");
}
}
Expand Down Expand Up @@ -178,7 +179,7 @@ static void configure_current_thread_affinity(

if (affinity_mask > 0)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Affinity mask has more processors than the ones present in the system");
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Affinity mask has more processors than the ones present in the system");
}

if (result > 0)
Expand All @@ -192,7 +193,7 @@ static void configure_current_thread_affinity(

if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity_mask << ". Error '" << strerror(
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity_mask << ". Error '" << strerror(
result) << "'");
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/cpp/utils/threading/threading_win32.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <processthreadsapi.h>

#include <fastdds/rtps/attributes/ThreadSettings.hpp>
#include <utils/threading/threading.hpp>

namespace eprosima {

Expand Down Expand Up @@ -69,7 +70,7 @@ static void configure_current_thread_priority(
{
if (0 == SetThreadPriority(GetCurrentThread(), priority))
{
EPROSIMA_LOG_ERROR(SYSTEM,
THREAD_EPROSIMA_LOG_ERROR(thread_name,
"Problem to set priority of thread with id [" << GetCurrentThreadId() << "," << thread_name << "] to value " << priority <<
". Error '" << GetLastError() << "'");
}
Expand All @@ -84,7 +85,7 @@ static void configure_current_thread_affinity(
{
if (0 == SetThreadAffinityMask(GetCurrentThread(), static_cast<DWORD_PTR>(affinity_mask)))
{
EPROSIMA_LOG_ERROR(SYSTEM,
THREAD_EPROSIMA_LOG_ERROR(thread_name,
"Problem to set affinity of thread with id [" << GetCurrentThreadId() << "," << thread_name << "] to value " << affinity_mask <<
". Error '" << GetLastError() << "'");
}
Expand Down
18 changes: 18 additions & 0 deletions test/unittest/logging/LogTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,24 @@ TEST_F(LogTests, thread_config)
EXPECT_EQ(entries.size(), n_logs);
}

/**
* Regression test 22624: when setting thread affinity fails, eprosima log error throws another error,
* and calls eprosima log error. This causes a looping recursive call for eprosima log error.
*/
TEST_F(LogTests, thread_log_error_loop)
{
// Set general verbosity
Log::SetVerbosity(Log::Error);

// Set thread settings
eprosima::fastdds::rtps::ThreadSettings thr_settings{};
thr_settings.affinity = 0xFFFFFFFFFFFFFFFF;
Log::SetThreadConfig(thr_settings);

// Start the error message
EPROSIMA_LOG_ERROR(SYSTEM, "Recursive error loop avoided");
}

int main(
int argc,
char** argv)
Expand Down
Loading