Skip to content

Commit d13108c

Browse files
committed
Catch process tracker errors if already exited
1 parent cdb1893 commit d13108c

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

StabilityMatrix.Core/Helper/ProcessTracker.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,30 @@ public static partial class ProcessTracker
2020
{
2121
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2222

23-
private static readonly Lazy<JobObject?> ProcessTrackerJobLazy =
24-
new(() =>
23+
private static readonly Lazy<JobObject?> ProcessTrackerJobLazy = new(() =>
24+
{
25+
if (!JobObject.IsAvailableOnCurrentPlatform)
2526
{
26-
if (!JobObject.IsAvailableOnCurrentPlatform)
27-
{
28-
return null;
29-
}
27+
return null;
28+
}
3029

31-
// The job name is optional (and can be null) but it helps with diagnostics.
32-
// If it's not null, it has to be unique. Use SysInternals' Handle command-line
33-
// utility: handle -a ChildProcessTracker
34-
var jobName = $"SM_ProcessTracker_{Environment.ProcessId}";
30+
// The job name is optional (and can be null) but it helps with diagnostics.
31+
// If it's not null, it has to be unique. Use SysInternals' Handle command-line
32+
// utility: handle -a ChildProcessTracker
33+
var jobName = $"SM_ProcessTracker_{Environment.ProcessId}";
3534

36-
Logger.Debug("Creating Job Object {Job}", jobName);
35+
Logger.Debug("Creating Job Object {Job}", jobName);
3736

38-
try
39-
{
40-
return new JobObject(jobName);
41-
}
42-
catch (Exception e)
43-
{
44-
Logger.Error(e, "Failed to create Job Object, ProcessTracker will be unavailable");
45-
return null;
46-
}
47-
});
37+
try
38+
{
39+
return new JobObject(jobName);
40+
}
41+
catch (Exception e)
42+
{
43+
Logger.Error(e, "Failed to create Job Object, ProcessTracker will be unavailable");
44+
return null;
45+
}
46+
});
4847

4948
private static JobObject? ProcessTrackerJob => ProcessTrackerJobLazy.Value;
5049

@@ -76,15 +75,15 @@ public static void AddProcess(Process process)
7675
return;
7776
}
7877

79-
Logger.Debug(
80-
"Adding Process {Process} [{Id}] to Job Object {Job}",
81-
process.ProcessName,
82-
process.Id,
83-
job.Name
84-
);
85-
8678
try
8779
{
80+
Logger.Debug(
81+
"Adding Process {Process} [{Id}] to Job Object {Job}",
82+
process.ProcessName,
83+
process.Id,
84+
job.Name
85+
);
86+
8887
job.AssignProcess(process);
8988
}
9089
catch (Exception)
@@ -207,7 +206,7 @@ public JobObject(string name)
207206
// This is the key flag. When our process is killed, Windows will automatically
208207
// close the job handle, and when that happens, we want the child processes to
209208
// be killed, too.
210-
LimitFlags = JOBOBJECTLIMIT.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
209+
LimitFlags = JOBOBJECTLIMIT.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
211210
};
212211

213212
var length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
@@ -304,7 +303,7 @@ internal enum JobObjectInfoType
304303
EndOfJobTimeInformation = 6,
305304
ExtendedLimitInformation = 9,
306305
SecurityLimitInformation = 5,
307-
GroupInformation = 11
306+
GroupInformation = 11,
308307
}
309308

310309
[StructLayout(LayoutKind.Sequential)]
@@ -326,7 +325,7 @@ internal struct JOBOBJECT_BASIC_LIMIT_INFORMATION
326325
// ReSharper disable once IdentifierTypo
327326
internal enum JOBOBJECTLIMIT : uint
328327
{
329-
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000
328+
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000,
330329
}
331330

332331
[StructLayout(LayoutKind.Sequential)]

0 commit comments

Comments
 (0)