From 253ef12f50f97e6be2cb36a6e57d725d1a3fa559 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Tue, 13 Aug 2019 15:00:54 -0700 Subject: [PATCH] leak_test: use tgkill to check if threads are alive. If one of our threads exits and a new process shows up with the same tid, we would previously loop until that process exits, leading to flakiness in tests. Also fix use of uninitialized memory that happens when threads_count != kThreadCount. Bug: http://b/139315254 Test: /data/nativetest/bionic-unit-tests/bionic-unit-tests --gtest_filter="pthread_leak.detach" on cuttlefish Change-Id: I2aad7f3f105aeba87c3929bfce53ef08e1484b2d --- tests/leak_test.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/leak_test.cpp b/tests/leak_test.cpp index 600520959..3cc1a0a91 100644 --- a/tests/leak_test.cpp +++ b/tests/leak_test.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -30,26 +31,28 @@ #include #include +#include #include "utils.h" using namespace std::chrono_literals; -static void WaitUntilAllExited(pid_t* pids, size_t pid_count) { +static void WaitUntilAllThreadsExited(pid_t* tids, size_t tid_count) { // Wait until all children have exited. bool alive = true; while (alive) { alive = false; - for (size_t i = 0; i < pid_count; ++i) { - if (pids[i] != 0) { - if (kill(pids[i], 0) == 0) { + for (size_t i = 0; i < tid_count; ++i) { + if (tids[i] != 0) { + if (tgkill(getpid(), tids[i], 0) == 0) { alive = true; } else { EXPECT_EQ(errno, ESRCH); - pids[i] = 0; // Skip in next loop. + tids[i] = 0; // Skip in next loop. } } } + sched_yield(); } } @@ -155,7 +158,7 @@ TEST(pthread_leak, detach) { pthread_barrier_wait(&barrier); ASSERT_EQ(pthread_barrier_destroy(&barrier), 0); - WaitUntilAllExited(tids, arraysize(tids)); + WaitUntilAllThreadsExited(tids, threads_count); // A native bridge implementation might need a warm up pass to reach a steady state. // http://b/37920774.