aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2024-03-21 10:50:44 +0100
committerGitHub Enterprise <[email protected]>2024-03-21 10:50:44 +0100
commitd7f422f02976b8d48cce533f9292400f2f85a141 (patch)
tree74032ff63b5308ef6012526959ebd0fcde7ff4ba /src
parent5.4.2-pre5 (diff)
downloadzen-d7f422f02976b8d48cce533f9292400f2f85a141.tar.xz
zen-d7f422f02976b8d48cce533f9292400f2f85a141.zip
improved process monitoring behaviour with invalid pids (#16)
Diffstat (limited to 'src')
-rw-r--r--src/zencore/include/zencore/process.h1
-rw-r--r--src/zencore/process.cpp41
-rw-r--r--src/zenserver/zenserver.cpp5
-rw-r--r--src/zenutil/zenserverprocess.cpp9
4 files changed, 42 insertions, 14 deletions
diff --git a/src/zencore/include/zencore/process.h b/src/zencore/include/zencore/process.h
index d90a32301..a14aa5a71 100644
--- a/src/zencore/include/zencore/process.h
+++ b/src/zencore/include/zencore/process.h
@@ -22,6 +22,7 @@ public:
ZENCORE_API ~ProcessHandle();
ZENCORE_API void Initialize(int Pid);
+ ZENCORE_API void Initialize(int Pid, std::error_code& OutEc);
ZENCORE_API void Initialize(void* ProcessHandle); /// Initialize with an existing handle - takes ownership of the handle
ZENCORE_API [[nodiscard]] bool IsRunning() const;
ZENCORE_API [[nodiscard]] bool IsValid() const;
diff --git a/src/zencore/process.cpp b/src/zencore/process.cpp
index 2d0ec2de6..e0a395494 100644
--- a/src/zencore/process.cpp
+++ b/src/zencore/process.cpp
@@ -75,8 +75,9 @@ ProcessHandle::~ProcessHandle()
}
void
-ProcessHandle::Initialize(int Pid)
+ProcessHandle::Initialize(int Pid, std::error_code& OutEc)
{
+ OutEc.clear();
ZEN_ASSERT(m_ProcessHandle == nullptr);
#if ZEN_PLATFORM_WINDOWS
@@ -90,7 +91,21 @@ ProcessHandle::Initialize(int Pid)
if (!m_ProcessHandle)
{
- ThrowLastError(fmt::format("ProcessHandle::Initialize(pid: {}) failed", Pid));
+ OutEc = MakeErrorCodeFromLastError();
+ }
+
+ m_Pid = Pid;
+}
+
+void
+ProcessHandle::Initialize(int Pid)
+{
+ std::error_code Ec;
+ Initialize(Pid, Ec);
+
+ if (Ec)
+ {
+ throw std::system_error(Ec, fmt::format("ProcessHandle::Initialize(pid: {}) failed", Pid));
}
m_Pid = Pid;
@@ -576,12 +591,19 @@ ProcessMonitor::IsRunning()
#if ZEN_PLATFORM_WINDOWS
DWORD ExitCode = 0;
- GetExitCodeProcess(Proc, &ExitCode);
- ProcIsActive = (ExitCode == STILL_ACTIVE);
- if (!ProcIsActive)
+ if (Proc)
+ {
+ GetExitCodeProcess(Proc, &ExitCode);
+ ProcIsActive = (ExitCode == STILL_ACTIVE);
+ if (!ProcIsActive)
+ {
+ CloseHandle(Proc);
+ }
+ }
+ else
{
- CloseHandle(Proc);
+ ProcIsActive = false;
}
#else
int Pid = int(intptr_t(Proc));
@@ -613,11 +635,8 @@ ProcessMonitor::AddPid(int Pid)
ProcessHandle = HandleType(intptr_t(Pid));
#endif
- if (ProcessHandle)
- {
- RwLock::ExclusiveLockScope _(m_Lock);
- m_ProcessHandles.push_back(ProcessHandle);
- }
+ RwLock::ExclusiveLockScope _(m_Lock);
+ m_ProcessHandles.push_back(ProcessHandle);
}
bool
diff --git a/src/zenserver/zenserver.cpp b/src/zenserver/zenserver.cpp
index d8176678d..daec743a0 100644
--- a/src/zenserver/zenserver.cpp
+++ b/src/zenserver/zenserver.cpp
@@ -124,8 +124,9 @@ ZenServer::Initialize(const ZenServerOptions& ServerOptions, ZenServerState::Zen
if (ParentPid)
{
- ProcessHandle OwnerProcess;
- OwnerProcess.Initialize(ParentPid);
+ std::error_code Ec;
+ ProcessHandle OwnerProcess;
+ OwnerProcess.Initialize(ParentPid, /* out */ Ec);
if (!OwnerProcess.IsValid())
{
diff --git a/src/zenutil/zenserverprocess.cpp b/src/zenutil/zenserverprocess.cpp
index 0b0127f93..3667ad2c6 100644
--- a/src/zenutil/zenserverprocess.cpp
+++ b/src/zenutil/zenserverprocess.cpp
@@ -673,7 +673,14 @@ ZenServerInstance::AttachToRunningServer(int BasePort)
throw std::runtime_error("No server found");
}
- m_Process.Initialize(Entry->Pid);
+ std::error_code Ec;
+ m_Process.Initialize(Entry->Pid, Ec);
+
+ if (Ec)
+ {
+ throw std::system_error(Ec, fmt::format("failed to attach to running server on port {} using pid {}", BasePort, Entry->Pid.load()));
+ }
+
CreateShutdownEvent(Entry->EffectiveListenPort);
m_BasePort = Entry->EffectiveListenPort;
}