From b39ef766a759448cf9f0f87dd129ead3c1657395 Mon Sep 17 00:00:00 2001 From: Joseph Scheuhammer Date: Mon, 30 Oct 2017 11:21:43 -0400 Subject: [PATCH 1/2] GPII-2668: Windows Process Reporter: C# "process not running" exception. Modified C# code to handle the exception and not add the process to the list of running processes. --- .../processReporter/dotNetProcesses.csx | 94 ++++++++++--------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/gpii/node_modules/processReporter/dotNetProcesses.csx b/gpii/node_modules/processReporter/dotNetProcesses.csx index 1c8504a49..d6a5162d0 100644 --- a/gpii/node_modules/processReporter/dotNetProcesses.csx +++ b/gpii/node_modules/processReporter/dotNetProcesses.csx @@ -33,9 +33,6 @@ public class Startup public async Task Invoke(dynamic input) { ManagementObject[] processes = null; - object propValue; - ProcessModule mainModule; - ProcInfo aProcInfo; ArrayList result; ManagementObjectCollection moc = new ManagementClass(new ManagementPath("Win32_Process")).GetInstances(); @@ -50,68 +47,77 @@ public class Startup processes, p => Convert.ToInt32(p.GetPropertyValue("ProcessId")) == input ); if (aProcess != null) { - result.Add(makeProcInfo(aProcess)); + makeAndApprendProcInfo(aProcess, result); } } else { ManagementObject[] someProcesses = Array.FindAll( processes, p => p.GetPropertyValue("Name").ToString() == input ); foreach (ManagementObject p in someProcesses) { - result.Add(makeProcInfo(p)); + makeAndApprendProcInfo(p, result); } } } // No process specified; get all processes and their info. else { foreach (ManagementObject p in processes) { - result.Add(makeProcInfo(p)); + makeAndApprendProcInfo(p, result); } } return result.ToArray(); } - public static ProcInfo makeProcInfo(ManagementObject process) { + public static void makeAndApprendProcInfo(ManagementObject processMO, ArrayList procInfos) { object propValue; + Process theProcess; + int pid = Convert.ToInt32(processMO.GetPropertyValue("ProcessId")); + // It's possible that the actual process has exited since acquiring its + // ManagementObject representation (the argument passed to this + // function). If so, trying to get its process id (pid) will result in + // an exception indicating the process is no longer running. See: + // https://msdn.microsoft.com/en-us/library/76fkb36k(v=vs.110).aspx#Anchor_1 + try { + theProcess = Process.GetProcessById(pid); + ProcInfo procInfo = new ProcInfo(); + procInfo.command = processMO.GetPropertyValue("Name").ToString(); + procInfo.pid = pid; - ProcInfo procInfo = new ProcInfo(); - procInfo.command = process.GetPropertyValue("Name").ToString(); - procInfo.pid = Convert.ToInt32(process.GetPropertyValue("ProcessId")); - - propValue = process.GetPropertyValue("ExecutablePath"); - if (propValue != null) { - procInfo.fullPath = propValue.ToString(); - } - propValue = process.GetPropertyValue("CommandLine"); - if (propValue == null) { - propValue = ""; - } - procInfo.commandLine = propValue.ToString(); - - // Should be able to get the state of the process using the - // "ExecutionState" property, but that is not implemented; see: - // http://maestriatech.com/wmi.php - // - // Use the Process's main thread's state (the first one). - // Also, map Windows thread states to Linux-y process states. - Process theProcess = Process.GetProcessById(procInfo.pid); - switch (theProcess.Threads[0].ThreadState) { - case ThreadState.Running: - case ThreadState.Wait: - procInfo.state = "Running"; - break; + propValue = processMO.GetPropertyValue("ExecutablePath"); + if (propValue != null) { + procInfo.fullPath = propValue.ToString(); + } + propValue = processMO.GetPropertyValue("CommandLine"); + if (propValue == null) { + propValue = ""; + } + procInfo.commandLine = propValue.ToString(); + // Should be able to get the state of the process using the + // "ExecutionState" property, but that is not implemented; see: + // http://maestriatech.com/wmi.php + // + // Use the Process's main thread's state (the first one). + // Also, map Windows thread states to Linux-y process states. + switch (theProcess.Threads[0].ThreadState) { + case ThreadState.Running: + case ThreadState.Wait: + procInfo.state = "Running"; + break; - case ThreadState.Initialized: - case ThreadState.Ready: - case ThreadState.Standby: - case ThreadState.Transition: - procInfo.state = "Sleeping"; - break; + case ThreadState.Initialized: + case ThreadState.Ready: + case ThreadState.Standby: + case ThreadState.Transition: + procInfo.state = "Sleeping"; + break; - case ThreadState.Terminated: - case ThreadState.Unknown: - procInfo.state = "Zombie"; - break; + case ThreadState.Terminated: + case ThreadState.Unknown: + procInfo.state = "Zombie"; + break; + } + procInfos.Add(procInfo); } - return procInfo; + // Process no longer running -- nothing to add. + catch (ArgumentException e) { } } } From c337b7fb504c4e1788368c2d3c8144986787a2df Mon Sep 17 00:00:00 2001 From: Joseph Scheuhammer Date: Thu, 9 Nov 2017 11:12:56 -0500 Subject: [PATCH 2/2] GPII-2668: Windows Process Reporter: C# "process not running" exception Modified to address Steve Grundell's comments. --- gpii/node_modules/processReporter/dotNetProcesses.csx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gpii/node_modules/processReporter/dotNetProcesses.csx b/gpii/node_modules/processReporter/dotNetProcesses.csx index d6a5162d0..6eeec6232 100644 --- a/gpii/node_modules/processReporter/dotNetProcesses.csx +++ b/gpii/node_modules/processReporter/dotNetProcesses.csx @@ -47,27 +47,27 @@ public class Startup processes, p => Convert.ToInt32(p.GetPropertyValue("ProcessId")) == input ); if (aProcess != null) { - makeAndApprendProcInfo(aProcess, result); + makeAndAppendProcInfo(aProcess, result); } } else { ManagementObject[] someProcesses = Array.FindAll( processes, p => p.GetPropertyValue("Name").ToString() == input ); foreach (ManagementObject p in someProcesses) { - makeAndApprendProcInfo(p, result); + makeAndAppendProcInfo(p, result); } } } // No process specified; get all processes and their info. else { foreach (ManagementObject p in processes) { - makeAndApprendProcInfo(p, result); + makeAndAppendProcInfo(p, result); } } return result.ToArray(); } - public static void makeAndApprendProcInfo(ManagementObject processMO, ArrayList procInfos) { + public static void makeAndAppendProcInfo(ManagementObject processMO, ArrayList procInfos) { object propValue; Process theProcess; int pid = Convert.ToInt32(processMO.GetPropertyValue("ProcessId"));