aboutsummaryrefslogtreecommitdiff
path: root/src/zencore
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-03-21 13:03:41 +0100
committerGitHub Enterprise <[email protected]>2024-03-21 13:03:41 +0100
commitf60aec8607aa4ef70b4653d201c854b00a538951 (patch)
treea10a9b168fbc4f1f9a64c52f3126faecf845b795 /src/zencore
parent5.4.2-pre6 (diff)
downloadzen-f60aec8607aa4ef70b4653d201c854b00a538951.tar.xz
zen-f60aec8607aa4ef70b4653d201c854b00a538951.zip
harden attach sponsor process (#14)
- Improvement: Delay exiting due to no sponsor processes by one second to handle race conditions - Improvement: Safer IsProcessRunning check - Improvement: make sure we can RequestApplicationExit safely from any thread
Diffstat (limited to 'src/zencore')
-rw-r--r--src/zencore/include/zencore/zencore.h2
-rw-r--r--src/zencore/process.cpp24
-rw-r--r--src/zencore/zencore.cpp11
3 files changed, 26 insertions, 11 deletions
diff --git a/src/zencore/include/zencore/zencore.h b/src/zencore/include/zencore/zencore.h
index e8c734ba9..1a9060e29 100644
--- a/src/zencore/include/zencore/zencore.h
+++ b/src/zencore/include/zencore/zencore.h
@@ -84,7 +84,7 @@ protected:
namespace zen {
ZENCORE_API bool IsApplicationExitRequested();
-ZENCORE_API void RequestApplicationExit(int ExitCode);
+ZENCORE_API bool RequestApplicationExit(int ExitCode);
ZENCORE_API int ApplicationExitCode();
ZENCORE_API bool IsDebuggerPresent();
ZENCORE_API void SetIsInteractiveSession(bool Value);
diff --git a/src/zencore/process.cpp b/src/zencore/process.cpp
index e0a395494..d8be0d343 100644
--- a/src/zencore/process.cpp
+++ b/src/zencore/process.cpp
@@ -661,14 +661,13 @@ IsProcessRunning(int pid)
if (!hProc)
{
DWORD Error = zen::GetLastError();
-
if (Error == ERROR_INVALID_PARAMETER)
{
return false;
}
-
ThrowSystemError(Error, fmt::format("failed to open process with pid {}", pid));
}
+ auto _ = MakeGuard([hProc]() { CloseHandle(hProc); });
bool bStillActive = true;
DWORD ExitCode = 0;
@@ -678,14 +677,25 @@ IsProcessRunning(int pid)
}
else
{
- ZEN_WARN("Unable to get exit code from handle for process '{}', treating the process as active", pid);
+ DWORD Error = GetLastError();
+ ThrowSystemError(Error, fmt::format("failed to get process exit code for pid {}", pid));
}
-
- CloseHandle(hProc);
-
return bStillActive;
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
- return (kill(pid_t(pid), 0) == 0);
+ int Res = kill(pid_t(pid), 0);
+ if (Res == 0)
+ {
+ return true;
+ }
+ int Error = errno;
+ if (Error == ESRCH) // No such process
+ {
+ return false;
+ }
+ else
+ {
+ ThrowSystemError(Error, fmt::format("Failed to signal running process %d: %d", pid, Error));
+ }
#endif
}
diff --git a/src/zencore/zencore.cpp b/src/zencore/zencore.cpp
index d0acac608..b80b1d280 100644
--- a/src/zencore/zencore.cpp
+++ b/src/zencore/zencore.cpp
@@ -115,11 +115,16 @@ IsApplicationExitRequested()
return s_ApplicationExitRequested;
}
-void
+bool
RequestApplicationExit(int ExitCode)
{
- s_ApplicationExitCode = ExitCode;
- s_ApplicationExitRequested = true;
+ bool Expected = false;
+ if (s_ApplicationExitRequested.compare_exchange_weak(Expected, true))
+ {
+ s_ApplicationExitCode = ExitCode;
+ return true;
+ }
+ return false;
}
int