From 3c183627016525bdb54782f2aebce3ef4444d9ed Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Thu, 11 Sep 2025 19:35:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?waitpid=E6=B7=BB=E5=8A=A0timeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id8579dfa1c39ecef5a70069d99abd040836a67af Signed-off-by: leiguangyu --- .../innerkits/dump_catcher/lite_perf.cpp | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/interfaces/innerkits/dump_catcher/lite_perf.cpp b/interfaces/innerkits/dump_catcher/lite_perf.cpp index c1bfd55c1..bab2ae530 100644 --- a/interfaces/innerkits/dump_catcher/lite_perf.cpp +++ b/interfaces/innerkits/dump_catcher/lite_perf.cpp @@ -67,6 +67,7 @@ private: bool DoReadBuf(int fd); bool DoReadRes(int fd, int& pollRet); bool ParseSampleStacks(const std::string& datas); + int WaitpidTimeout(pid_t pid, int *status); std::string bufMsg_; std::string resMsg_; @@ -134,6 +135,8 @@ int LitePerf::Impl::StartProcessStackSampling(const std::vector& tids, int } } while (false); FinishDump(); + close(pipeReadFd[0]); + close(pipeReadFd[1]); return res; } @@ -292,6 +295,28 @@ bool LitePerf::Impl::InitDumpParam(const std::vector& tids, int freq, int d return true; } +int LitePerf::Impl::WaitpidTimeout(pid_t pid, int *status) { + for (int i = 0; i <= 10; i++) { + int result = waitpid(pid, status, WNOHANG); + + if (result == pid) { + return 0; + } + if (result == -1) { + DFXLOGE("Failed to wait pid(%{public}d)", pid); + kill(pid, SIGKILL); + return -1; + } + + constexpr time_t usleepTime = 100000; + usleep(usleepTime); + } + DFXLOGE("Failed to wait pid(%{public}d), timeout", pid); + kill(pid, SIGKILL); + constexpr int waitTimeout = -2; + return waitTimeout; +} + int LitePerf::Impl::ExecDump(const std::vector& tids, int freq, int durationMs) { static LitePerfParam lperf; @@ -324,9 +349,10 @@ int LitePerf::Impl::ExecDump(const std::vector& tids, int freq, int duratio _exit(0); } } - int res = waitpid(pid, nullptr, 0); + int res = WaitpidTimeout(pid, nullptr); if (res < 0) { DFXLOGE("Failed to wait pid(%{public}d), errno(%{public}d)", pid, errno); + return -1; } else { DFXLOGI("wait pid(%{public}d) exit", pid); } -- Gitee From 24d2dcbef36c0975d4ed10dd1d58a02847b46492 Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Fri, 12 Sep 2025 14:07:50 +0800 Subject: [PATCH 2/3] =?UTF-8?q?waitpid=E6=B7=BB=E5=8A=A0timeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I8a22b0abc6304a515bd5fbc225ee4033190a9c7b Signed-off-by: leiguangyu --- interfaces/innerkits/dump_catcher/lite_perf.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/interfaces/innerkits/dump_catcher/lite_perf.cpp b/interfaces/innerkits/dump_catcher/lite_perf.cpp index bab2ae530..d2f875ff5 100644 --- a/interfaces/innerkits/dump_catcher/lite_perf.cpp +++ b/interfaces/innerkits/dump_catcher/lite_perf.cpp @@ -67,7 +67,7 @@ private: bool DoReadBuf(int fd); bool DoReadRes(int fd, int& pollRet); bool ParseSampleStacks(const std::string& datas); - int WaitpidTimeout(pid_t pid, int *status); + int WaitpidTimeout(pid_t pid); std::string bufMsg_; std::string resMsg_; @@ -295,10 +295,11 @@ bool LitePerf::Impl::InitDumpParam(const std::vector& tids, int freq, int d return true; } -int LitePerf::Impl::WaitpidTimeout(pid_t pid, int *status) { - for (int i = 0; i <= 10; i++) { - int result = waitpid(pid, status, WNOHANG); - +int LitePerf::Impl::WaitpidTimeout(pid_t pid) +{ + constexpr int waitCount = 150; + for (int i = 0; i <= waitCount; i++) { + int result = waitpid(pid, nullptr, WNOHANG); if (result == pid) { return 0; } @@ -313,8 +314,7 @@ int LitePerf::Impl::WaitpidTimeout(pid_t pid, int *status) { } DFXLOGE("Failed to wait pid(%{public}d), timeout", pid); kill(pid, SIGKILL); - constexpr int waitTimeout = -2; - return waitTimeout; + return -1; } int LitePerf::Impl::ExecDump(const std::vector& tids, int freq, int durationMs) @@ -349,7 +349,7 @@ int LitePerf::Impl::ExecDump(const std::vector& tids, int freq, int duratio _exit(0); } } - int res = WaitpidTimeout(pid, nullptr); + int res = WaitpidTimeout(pid); if (res < 0) { DFXLOGE("Failed to wait pid(%{public}d), errno(%{public}d)", pid, errno); return -1; -- Gitee From 9bc62e141e7b0366cfde4487103e833366d9810c Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Fri, 12 Sep 2025 18:13:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?waitpid=E6=B7=BB=E5=8A=A0timeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I9ac7a1a964401cc8faa67e13c948c7c8963f6dd7 Signed-off-by: leiguangyu --- interfaces/innerkits/dump_catcher/lite_perf.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/interfaces/innerkits/dump_catcher/lite_perf.cpp b/interfaces/innerkits/dump_catcher/lite_perf.cpp index d2f875ff5..e048bc198 100644 --- a/interfaces/innerkits/dump_catcher/lite_perf.cpp +++ b/interfaces/innerkits/dump_catcher/lite_perf.cpp @@ -119,6 +119,12 @@ int LitePerf::Impl::StartProcessStackSampling(const std::vector& tids, int static_cast(timeout / 1000)); if (req != ResponseCode::REQUEST_SUCCESS) { DFXLOGE("Failed to request liteperf pipe read."); + if (pipeReadFd[0] != -1) { + close(pipeReadFd[0]); + } + if (pipeReadFd[1] != -1) { + close(pipeReadFd[1]); + } isRunning_.store(false); return -1; } -- Gitee