Skip to content

Commit 0c52d1a

Browse files
authored
Addl loop prevention for traverse_parent_state (#1414)
* Addl loop prevention for traverse_parent_state Add some additional ways to prevent potential loops when traversing parent state: - If the slow threadinfo ever has a tid of -1, stop. - If the slow threadinfo ever has ptid == tid, declare a loop and stop. * Add more info to loop warning. Include current pointer's tid/ptid.
1 parent d06636f commit 0c52d1a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

userspace/libsinsp/threadinfo.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ void sinsp_threadinfo::traverse_parent_state(visitor_func_t &visitor)
904904
// Move fast to its parent
905905
fast = (fast ? fast->get_parent_thread() : fast);
906906

907-
while(slow)
907+
// The slow pointer must be valid and not have a tid of -1.
908+
while(slow && slow->m_tid != -1)
908909
{
909910
if(!visitor(slow))
910911
{
@@ -919,15 +920,20 @@ void sinsp_threadinfo::traverse_parent_state(visitor_func_t &visitor)
919920
for (uint32_t i = 0; i < 2; i++) {
920921
fast = (fast ? fast->get_parent_thread() : fast);
921922

922-
// If not at the end but fast == slow, there's a loop
923-
// in the thread state.
924-
if(slow && (slow == fast))
923+
// If not at the end but fast == slow or if
924+
// slow points to itself, there's a loop in
925+
// the thread state.
926+
if(slow && (slow == fast ||
927+
slow->m_tid == slow->m_ptid))
925928
{
926929
// Note we only log a loop once for a given main thread, to avoid flooding logs.
927930
if(!m_parent_loop_detected)
928931
{
929932
g_logger.log(string("Loop in parent thread state detected for pid ") +
930-
std::to_string(m_pid), sinsp_logger::SEV_WARNING);
933+
std::to_string(m_pid) +
934+
". stopped at tid= " + std::to_string(slow->m_tid) +
935+
" ptid=" + std::to_string(slow->m_ptid),
936+
sinsp_logger::SEV_WARNING);
931937
m_parent_loop_detected = true;
932938
}
933939
return;

0 commit comments

Comments
 (0)