From c9c2c7d2b62d7a83026ff8127e6a071e443185ac Mon Sep 17 00:00:00 2001 From: kun chen Date: Thu, 11 Sep 2025 19:02:26 +0800 Subject: [PATCH] catch thread exception of FileIOOnThread Signed-off-by: kun chen --- src/common/file_descriptor.cpp | 17 ++++++++++++++--- src/daemon/daemon_unity.cpp | 4 +++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/common/file_descriptor.cpp b/src/common/file_descriptor.cpp index 24c18f68..6c816ca7 100644 --- a/src/common/file_descriptor.cpp +++ b/src/common/file_descriptor.cpp @@ -222,10 +222,21 @@ int HdcFileDescriptor::LoopReadOnThread() } contextIO->bufIO = buf; contextIO->thisClass = this; + try { + std::thread([contextIO, readMax]() { + HdcFileDescriptor::FileIOOnThread(contextIO, readMax); + }).detach(); + } catch (std::exception &e) { + if (contextIO) { + delete contextIO; + } + if (buf) { + delete[] buf; + } + WRITE_LOG(LOG_FATAL, "std::thread exception %s", e.what()); + return -1; + } ++refIO; - std::thread([contextIO, readMax]() { - HdcFileDescriptor::FileIOOnThread(contextIO, readMax); - }).detach(); return 0; } diff --git a/src/daemon/daemon_unity.cpp b/src/daemon/daemon_unity.cpp index 69161d12..2cee32d6 100644 --- a/src/daemon/daemon_unity.cpp +++ b/src/daemon/daemon_unity.cpp @@ -111,7 +111,9 @@ int HdcDaemonUnity::ExecuteShell(const string &shellCommand, string optionPath) if (!asyncCommand.Initial(loopTask, funcResultOutput)) { break; } - asyncCommand.ExecuteCommand(shellCommand, optionPath); + if (!asyncCommand.ExecuteCommand(shellCommand, optionPath)) { + break; + } ++refCount; return RET_SUCCESS; } while (false); -- Gitee