Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Tasks on MacOS agents not completing but marked as succeeded. #5079

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
e42776b
added tracing
sahilbhattMS Dec 12, 2024
19da14d
added more tracing
sahilbhattMS Dec 13, 2024
46f65fc
fixed exception handling
sahilbhattMS Dec 13, 2024
1876a77
fixed compilation errors
sahilbhattMS Dec 13, 2024
f832c54
fixes
sahilbhattMS Dec 13, 2024
d5dae1d
added tracing related to threads
sahilbhattMS Dec 23, 2024
1435f39
thread tracing
sahilbhattMS Dec 26, 2024
96cd757
test change in resourcemanager exec context
sahilbhattMS Dec 26, 2024
a6d789d
publish telemetry disabled check
sahilbhattMS Dec 27, 2024
327284d
streamreader access check
sahilbhattMS Dec 27, 2024
da1fed4
compare exchange
sahilbhattMS Dec 27, 2024
74a3716
turning off memory and disk info monitor
sahilbhattMS Dec 30, 2024
73432db
monitors - cpu off, memory on
sahilbhattMS Dec 30, 2024
324a403
cpu and memory info both enabled
sahilbhattMS Dec 30, 2024
5f95d4a
cancellationtoken update
sahilbhattMS Dec 30, 2024
b1ac11d
loop iteration trace
sahilbhattMS Dec 30, 2024
694f83e
added taskid
sahilbhattMS Dec 30, 2024
121acae
resource monitor locks
sahilbhattMS Dec 30, 2024
6c742d1
interlocked compare exchange
sahilbhattMS Dec 30, 2024
7fb519f
resource monitor lock and private task call
sahilbhattMS Dec 30, 2024
8b0d7ba
syntax fix
sahilbhattMS Dec 30, 2024
57f8150
job extension and workercommandmanager tracing
sahilbhattMS Dec 31, 2024
7d4c8cd
cancellation token tracing
sahilbhattMS Dec 31, 2024
63ff3cd
commented resourcediagnostics from steps runner, tracing for context.…
sahilbhattMS Dec 31, 2024
8350652
commenting out resource debug monitor
sahilbhattMS Dec 31, 2024
9f5b55c
minor fix
sahilbhattMS Dec 31, 2024
db7be63
added process ids to tracings in invoker; added continueaftercancelpr…
sahilbhattMS Dec 31, 2024
4ae1886
execute async overload
sahilbhattMS Dec 31, 2024
a2af9e1
continue after cancel process tree false
sahilbhattMS Dec 31, 2024
4b2a8cc
removed args
sahilbhattMS Dec 31, 2024
aacf185
added process filename
sahilbhattMS Jan 2, 2025
f037968
added filename
sahilbhattMS Jan 2, 2025
bf05f0f
Changes from tarun
lajijijij Jan 2, 2025
8356c24
Merge branch 'users/sahilbhatt/debug_monitor_tracing' of https://gith…
lajijijij Jan 2, 2025
5d157c9
undo filename change inside redirect if-blocks
sahilbhattMS Jan 2, 2025
f837847
adding lock to not invoke th eprocess again and again
lajijijij Jan 2, 2025
3a0a744
re-enable debug resource monitor in jobrunner
sahilbhattMS Jan 3, 2025
d6c3f58
remove try-catch-finally block
sahilbhattMS Jan 3, 2025
4e35f5d
disposing _proc stdinput and stderror
sahilbhattMS Jan 3, 2025
f217c6d
proc stdout dispose
sahilbhattMS Jan 3, 2025
ea3e6f5
removing tracings - restoring few files to original state
sahilbhattMS Jan 6, 2025
025af0f
removing tracings - restoring few files to original state
sahilbhattMS Jan 6, 2025
62eb8f0
removing tracings - restoring few files to original state
sahilbhattMS Jan 6, 2025
192ed66
removed tracings from process invoker and resource metrics manager
sahilbhattMS Jan 6, 2025
d6797a3
remove temporary debug statements
sahilbhattMS Jan 6, 2025
abaacf0
remove temporary debug statements
sahilbhattMS Jan 6, 2025
d69d4a2
formatting changes
sahilbhattMS Jan 6, 2025
64036d2
formatting changes
sahilbhattMS Jan 7, 2025
016540c
try-catch in dispose
sahilbhattMS Jan 8, 2025
519c0ac
null check for proc.stdout and proc.stderr, isprocrunning for diskinf…
sahilbhattMS Jan 9, 2025
bed69b5
formatting change
sahilbhattMS Jan 9, 2025
c6e1003
steps runner set context
sahilbhattMS Jan 9, 2025
ca7c534
Merge branch 'master' into users/sahilbhatt/debug_monitor_tracing
sahilbhattMS Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Agent.Sdk/ProcessInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ protected virtual void Dispose(bool disposing)
{
if (_proc != null)
{
//Dispose the standard input/output/error stream. Refer: https://github.com/dotnet/runtime/issues/58872
_proc.StandardInput.Dispose();
sahilbhattMS marked this conversation as resolved.
Show resolved Hide resolved
_proc.StandardOutput.Dispose();
_proc.StandardError.Dispose();
sahilbhattMS marked this conversation as resolved.
Show resolved Hide resolved

_proc.Dispose();
_proc = null;
}
Expand Down
171 changes: 106 additions & 65 deletions src/Agent.Worker/ResourceMetricsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public sealed class ResourceMetricsManager : AgentService, IResourceMetricsManag
#region MetricStructs
private struct CpuInfo
{
public bool IsProcRunning;
public DateTime Updated;
public double Usage;
}
Expand All @@ -64,6 +65,7 @@ private struct DiskInfo

public struct MemoryInfo
{
public bool IsProcRunning;
public DateTime Updated;
public long TotalMemoryMB;
public long UsedMemoryMB;
Expand Down Expand Up @@ -181,38 +183,57 @@ await Task.Run(() =>
}
if (PlatformUtil.RunningOnMacOS)
{
using var processInvoker = HostContext.CreateService<IProcessInvoker>();

List<string> outputs = new List<string>();
processInvoker.OutputDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
lock (_cpuInfoLock)
{
outputs.Add(message.Data);
};
if (_cpuInfo.IsProcRunning)
{
return;
}
_cpuInfo.IsProcRunning = true;
}

processInvoker.ErrorDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
try
{
Trace.Error($"Error on receiving CPU info: {message.Data}");
};
using var processInvoker = HostContext.CreateService<IProcessInvoker>();

List<string> outputs = new List<string>();
processInvoker.OutputDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
{
outputs.Add(message.Data);
};

var filePath = "/bin/bash";
var arguments = "-c \"top -l 2 -o cpu | grep ^CPU\"";
await processInvoker.ExecuteAsync(
workingDirectory: string.Empty,
fileName: filePath,
arguments: arguments,
environment: null,
requireExitCodeZero: true,
outputEncoding: null,
killProcessOnCancel: true,
cancellationToken: cancellationToken);

// Use second sample for more accurate calculation
var cpuInfoIdle = double.Parse(outputs[1].Split(' ', (char)StringSplitOptions.RemoveEmptyEntries)[6].Trim('%'));
processInvoker.ErrorDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
{
Trace.Error($"Error on receiving CPU info: {message.Data}");
};

var filePath = "/bin/bash";
var arguments = "-c \"top -l 2 -o cpu | grep ^CPU\"";

await processInvoker.ExecuteAsync(
workingDirectory: string.Empty,
fileName: filePath,
arguments: arguments,
environment: null,
requireExitCodeZero: true,
outputEncoding: null,
killProcessOnCancel: true,
cancellationToken: cancellationToken);
// Use second sample for more accurate calculation
var cpuInfoIdle = double.Parse(outputs[1].Split(' ', (char)StringSplitOptions.RemoveEmptyEntries)[6].Trim('%'));

lock (_cpuInfoLock)
lock (_cpuInfoLock)
{
_cpuInfo.Updated = DateTime.Now;
_cpuInfo.Usage = 100 - cpuInfoIdle;
}
}
finally
{
_cpuInfo.Updated = DateTime.Now;
_cpuInfo.Usage = 100 - cpuInfoIdle;
lock (_cpuInfoLock)
{
_cpuInfo.IsProcRunning = false;
}
}
}
}
Expand Down Expand Up @@ -287,51 +308,71 @@ await Task.Run(() =>

if (PlatformUtil.RunningOnMacOS)
{
// vm_stat allows to get the most detailed information about memory usage on MacOS
// but unfortunately it returns values in pages and has no built-in arguments for custom output
// so we need to parse and cast the output manually

using var processInvoker = HostContext.CreateService<IProcessInvoker>();

List<string> outputs = new List<string>();
processInvoker.OutputDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
lock (_memoryInfoLock)
{
outputs.Add(message.Data);
};
if (_memoryInfo.IsProcRunning)
{
return;
}
_memoryInfo.IsProcRunning = true;
}

processInvoker.ErrorDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
try
{
Trace.Error($"Error on receiving memory info: {message.Data}");
};
// vm_stat allows to get the most detailed information about memory usage on MacOS
// but unfortunately it returns values in pages and has no built-in arguments for custom output
// so we need to parse and cast the output manually

var filePath = "vm_stat";
await processInvoker.ExecuteAsync(
workingDirectory: string.Empty,
fileName: filePath,
arguments: string.Empty,
environment: null,
requireExitCodeZero: true,
outputEncoding: null,
killProcessOnCancel: true,
cancellationToken: cancellationToken);

var pageSize = int.Parse(outputs[0].Split(" ", StringSplitOptions.RemoveEmptyEntries)[7]);

var pagesFree = long.Parse(outputs[1].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesActive = long.Parse(outputs[2].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesInactive = long.Parse(outputs[3].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesSpeculative = long.Parse(outputs[4].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesWiredDown = long.Parse(outputs[6].Split(" ", StringSplitOptions.RemoveEmptyEntries)[3].Trim('.'));
var pagesOccupied = long.Parse(outputs[16].Split(" ", StringSplitOptions.RemoveEmptyEntries)[4].Trim('.'));

var freeMemory = (pagesFree + pagesInactive) * pageSize;
var usedMemory = (pagesActive + pagesSpeculative + pagesWiredDown + pagesOccupied) * pageSize;
using var processInvoker = HostContext.CreateService<IProcessInvoker>();

lock (_memoryInfoLock)
List<string> outputs = new List<string>();
processInvoker.OutputDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
{
outputs.Add(message.Data);
};

processInvoker.ErrorDataReceived += delegate (object sender, ProcessDataReceivedEventArgs message)
{
Trace.Error($"Error on receiving memory info: {message.Data}");
};

var filePath = "vm_stat";

await processInvoker.ExecuteAsync(
workingDirectory: string.Empty,
fileName: filePath,
arguments: string.Empty,
environment: null,
requireExitCodeZero: true,
outputEncoding: null,
killProcessOnCancel: true,
cancellationToken: cancellationToken);

var pageSize = int.Parse(outputs[0].Split(" ", StringSplitOptions.RemoveEmptyEntries)[7]);

var pagesFree = long.Parse(outputs[1].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesActive = long.Parse(outputs[2].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesInactive = long.Parse(outputs[3].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesSpeculative = long.Parse(outputs[4].Split(" ", StringSplitOptions.RemoveEmptyEntries)[2].Trim('.'));
var pagesWiredDown = long.Parse(outputs[6].Split(" ", StringSplitOptions.RemoveEmptyEntries)[3].Trim('.'));
var pagesOccupied = long.Parse(outputs[16].Split(" ", StringSplitOptions.RemoveEmptyEntries)[4].Trim('.'));

var freeMemory = (pagesFree + pagesInactive) * pageSize;
var usedMemory = (pagesActive + pagesSpeculative + pagesWiredDown + pagesOccupied) * pageSize;

lock (_memoryInfoLock)
{
_memoryInfo.Updated = DateTime.Now;
_memoryInfo.TotalMemoryMB = (freeMemory + usedMemory) / 1048576;
_memoryInfo.UsedMemoryMB = usedMemory / 1048576;
}
}
finally
{
_memoryInfo.Updated = DateTime.Now;
_memoryInfo.TotalMemoryMB = (freeMemory + usedMemory) / 1048576;
_memoryInfo.UsedMemoryMB = usedMemory / 1048576;
lock (_memoryInfoLock)
{
_memoryInfo.IsProcRunning = false;
sahilbhattMS marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Agent.Worker/StepsRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public async Task RunAsync(IExecutionContext jobContext, IList<IStep> steps)
}

// Change the current job context to the step context.
var resourceDiagnosticManager = HostContext.GetService<IResourceMetricsManager>();
resourceDiagnosticManager.SetContext(step.ExecutionContext);
// var resourceDiagnosticManager = HostContext.GetService<IResourceMetricsManager>();
// resourceDiagnosticManager.SetContext(step.ExecutionContext);

sahilbhattMS marked this conversation as resolved.
Show resolved Hide resolved
// Variable expansion.
step.ExecutionContext.SetStepTarget(step.Target);
Expand Down
Loading