Skip to content

Commit 831c8bd

Browse files
System Log error: infinite loop when setting thread affinity fails (#5611) (#5630)
* Refs #22624: Regression test Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: Corrected bug Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: improved structure of the test Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: added reviewer suggestions Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: fixed reference to new file Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: fixed typo Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: Added reviwer suggestions Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: modified guard Signed-off-by: Juanjo Garcia <[email protected]> * Refs #22624: fixed typo Signed-off-by: Juanjo Garcia <[email protected]> --------- Signed-off-by: Juanjo Garcia <[email protected]> (cherry picked from commit 8097762) Co-authored-by: juanjo4936 <[email protected]>
1 parent fd8d24e commit 831c8bd

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/*!
16+
* @file thread_logging.hpp
17+
*/
18+
19+
#ifndef THREAD_LOGGING_HPP
20+
#define THREAD_LOGGING_HPP
21+
22+
#define THREAD_EPROSIMA_LOG_ERROR(thread_name, msg) \
23+
do{ \
24+
if (strcmp(thread_name, "dds.log") == 0) \
25+
{ \
26+
std::cerr << msg << std::endl; \
27+
} \
28+
else \
29+
{ \
30+
EPROSIMA_LOG_ERROR(SYSTEM, msg); \
31+
} \
32+
} while (0)
33+
34+
#endif // THREAD_LOGGING_HPP

src/cpp/utils/threading/threading_osx.ipp

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <fastdds/dds/log/Log.hpp>
2424
#include <fastdds/rtps/attributes/ThreadSettings.hpp>
25+
#include <utils/threading/thread_logging.hpp>
2526

2627
namespace eprosima {
2728

@@ -98,13 +99,13 @@ static void configure_current_thread_scheduler(
9899
result = setpriority(PRIO_PROCESS, tid, sched_priority);
99100
if (0 != result)
100101
{
101-
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set priority of thread with id [" << tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
102+
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set priority of thread with id [" << tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
102103
result) << "'");
103104
}
104105
}
105106
else if (0 != result)
106107
{
107-
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
108+
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
108109
result) << "'");
109110
}
110111
}
@@ -118,7 +119,7 @@ static void configure_current_thread_scheduler(
118119
result = pthread_setschedparam(self_tid, sched_class, &param);
119120
if (0 != result)
120121
{
121-
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(
122+
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(
122123
result) << "'");
123124
}
124125
}
@@ -138,7 +139,7 @@ static void configure_current_thread_affinity(
138139
1);
139140
if (0 != result)
140141
{
141-
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity << ". Error '" << strerror(
142+
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity << ". Error '" << strerror(
142143
result) << "'");
143144
}
144145
}

src/cpp/utils/threading/threading_pthread.ipp

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <fastdds/dds/log/Log.hpp>
2727
#include <fastdds/rtps/attributes/ThreadSettings.hpp>
28+
#include <utils/threading/thread_logging.hpp>
2829

2930
#if defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ <= 30)))
3031
#include <sys/syscall.h>
@@ -114,13 +115,13 @@ static void configure_current_thread_scheduler(
114115
result = setpriority(PRIO_PROCESS, gettid(), sched_priority);
115116
if (0 != result)
116117
{
117-
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set priority of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
118+
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set priority of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_priority << ". Error '" << strerror(
118119
result) << "'");
119120
}
120121
}
121122
else if (0 != result)
122123
{
123-
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
124+
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set scheduler of thread with id [" << self_tid << "," << thread_name << "] to value " << sched_class << ". Error '" << strerror(
124125
result) << "'");
125126
}
126127
}
@@ -135,7 +136,7 @@ static void configure_current_thread_scheduler(
135136
result = pthread_setschedparam(self_tid, sched_class, &param);
136137
if (0 != result)
137138
{
138-
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(
139+
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(
139140
result) << "'");
140141
}
141142
}
@@ -178,7 +179,7 @@ static void configure_current_thread_affinity(
178179

179180
if (affinity_mask > 0)
180181
{
181-
EPROSIMA_LOG_ERROR(SYSTEM, "Affinity mask has more processors than the ones present in the system");
182+
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Affinity mask has more processors than the ones present in the system");
182183
}
183184

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

193194
if (0 != result)
194195
{
195-
EPROSIMA_LOG_ERROR(SYSTEM, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity_mask << ". Error '" << strerror(
196+
THREAD_EPROSIMA_LOG_ERROR(thread_name, "Problem to set affinity of thread with id [" << self_tid << "," << thread_name << "] to value " << affinity_mask << ". Error '" << strerror(
196197
result) << "'");
197198
}
198199
}

src/cpp/utils/threading/threading_win32.ipp

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <processthreadsapi.h>
2020

2121
#include <fastdds/rtps/attributes/ThreadSettings.hpp>
22+
#include <utils/threading/thread_logging.hpp>
2223

2324
namespace eprosima {
2425

@@ -69,7 +70,7 @@ static void configure_current_thread_priority(
6970
{
7071
if (0 == SetThreadPriority(GetCurrentThread(), priority))
7172
{
72-
EPROSIMA_LOG_ERROR(SYSTEM,
73+
THREAD_EPROSIMA_LOG_ERROR(thread_name,
7374
"Problem to set priority of thread with id [" << GetCurrentThreadId() << "," << thread_name << "] to value " << priority <<
7475
". Error '" << GetLastError() << "'");
7576
}
@@ -84,7 +85,7 @@ static void configure_current_thread_affinity(
8485
{
8586
if (0 == SetThreadAffinityMask(GetCurrentThread(), static_cast<DWORD_PTR>(affinity_mask)))
8687
{
87-
EPROSIMA_LOG_ERROR(SYSTEM,
88+
THREAD_EPROSIMA_LOG_ERROR(thread_name,
8889
"Problem to set affinity of thread with id [" << GetCurrentThreadId() << "," << thread_name << "] to value " << affinity_mask <<
8990
". Error '" << GetLastError() << "'");
9091
}

test/unittest/logging/LogTests.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,24 @@ TEST_F(LogTests, thread_config)
695695
EXPECT_EQ(entries.size(), n_logs);
696696
}
697697

698+
/**
699+
* Regression test 22624: when setting thread affinity fails, eprosima log error throws another error,
700+
* and calls eprosima log error. This causes a looping recursive call for eprosima log error.
701+
*/
702+
TEST_F(LogTests, thread_log_error_loop)
703+
{
704+
// Set general verbosity
705+
Log::SetVerbosity(Log::Error);
706+
707+
// Set thread settings
708+
eprosima::fastdds::rtps::ThreadSettings thr_settings{};
709+
thr_settings.affinity = 0xFFFFFFFFFFFFFFFF;
710+
Log::SetThreadConfig(thr_settings);
711+
712+
// Start the error message
713+
EPROSIMA_LOG_ERROR(SYSTEM, "Recursive error loop avoided");
714+
}
715+
698716
int main(
699717
int argc,
700718
char** argv)

0 commit comments

Comments
 (0)