Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update ProcessExtensions to not throw when it cannot access perf coun…

…ters
  • Loading branch information...
commit 6182eb62de6b44b8b719c70fafb3c62b4380a8a0 1 parent a068c42
@pranavkm pranavkm authored
Showing with 28 additions and 11 deletions.
  1. +28 −11 Kudu.Core/Infrastructure/ProcessExtensions.cs
View
39 Kudu.Core/Infrastructure/ProcessExtensions.cs
@@ -94,11 +94,17 @@ private static void SafeKillProcess(Process process, ITracer tracer)
continue;
}
- int parentId = FindPidFromIndexedProcessName(indexedProcessName);
+ int? parentId = FindPidFromIndexedProcessName(indexedProcessName);
+ if (!parentId.HasValue)
+ {
+ // We encountered an unauthorized access exception when trying to use perf counters when the account Kudu AppDomain is executing in doesn't have sufficient privileges.
+ // Skip when this happens.
+ continue;
+ }
List<int> children = null;
- if (!tree.TryGetValue(parentId, out children))
+ if (!tree.TryGetValue(parentId.Value, out children))
{
- tree[parentId] = children = new List<int>();
+ tree[parentId.Value] = children = new List<int>();
}
children.Add(proc.Key);
@@ -114,24 +120,35 @@ private static string FindIndexedProcessName(int pid, string processName)
for (var index = 0; index < processesByName.Length; index++)
{
processIndexedName = index == 0 ? processName : processName + "#" + index;
- using (var processId = new PerformanceCounter("Process", "ID Process", processIndexedName))
+ int? processId = SafeGetPerfCounter("Process", "ID Process", processIndexedName);
+ if (processId.HasValue && processId.Value == pid)
{
- if ((int)processId.NextValue() == pid)
- {
- return processIndexedName;
- }
+ return processIndexedName;
}
}
return processIndexedName;
}
- private static int FindPidFromIndexedProcessName(string indexedProcessName)
+ private static int? FindPidFromIndexedProcessName(string indexedProcessName)
+ {
+ return SafeGetPerfCounter("Process", "Creating Process ID", indexedProcessName);
+ }
+
+ private static int? SafeGetPerfCounter(string category, string counterName, string key)
{
- using (var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName))
+ try
{
- return (int)parentId.NextValue();
+ using (var counter = new PerformanceCounter(category, counterName, key, readOnly: true))
+ {
+ return (int)counter.NextValue();
+ }
+ }
+ catch (UnauthorizedAccessException)
+ {
+
}
+ return null;
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.