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 agent lint errors #4117

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public void InstallService(string serviceName, string serviceDisplayName, string
serviceDisplayName,
ServiceRights.AllAccess,
SERVICE_WIN32_OWN_PROCESS,
ServiceBootFlag.AutoStart,
ServiceStartType.AutoStart,
ServiceError.Normal,
agentServiceExecutable,
null,
Expand Down Expand Up @@ -1332,9 +1332,9 @@ public enum ServiceError
Critical = 0x00000003
}

public enum ServiceBootFlag
public enum ServiceStartType
{
Start = 0x00000000,
BootStart = 0x00000000,
SystemStart = 0x00000001,
AutoStart = 0x00000002,
DemandStart = 0x00000003,
Expand Down Expand Up @@ -1425,7 +1425,7 @@ private static extern IntPtr CreateService(
string lpDisplayName,
ServiceRights dwDesiredAccess,
int dwServiceType,
ServiceBootFlag dwStartType,
ServiceStartType dwStartType,
ServiceError dwErrorControl,
string lpBinaryPathName,
string lpLoadOrderGroup,
Expand Down
3 changes: 0 additions & 3 deletions src/Agent.Sdk/Util/ExceptionsUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using Microsoft.VisualStudio.Services.Agent;
using Microsoft.VisualStudio.Services.Agent.Util;

namespace Agent.Sdk.Util
Expand Down
3 changes: 0 additions & 3 deletions src/Agent.Sdk/Util/ILoggedSecretMasker.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Microsoft.TeamFoundation.DistributedTask.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Agent.Sdk.Util
{
Expand Down
3 changes: 0 additions & 3 deletions src/Agent.Sdk/Util/LoggedSecretMasker.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Microsoft.TeamFoundation.DistributedTask.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Agent.Sdk.Util
{
Expand Down
2 changes: 0 additions & 2 deletions src/Agent.Sdk/Util/MaskingUtil.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Microsoft.VisualStudio.Services.ServiceEndpoints.WebApi;
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.VisualStudio.Services.Agent.Util
{
Expand Down
7 changes: 0 additions & 7 deletions src/Agent.Sdk/Util/PlatformUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using Agent.Sdk.Knob;
using Agent.Sdk.Util;
using BuildXL.Cache.MemoizationStore.Interfaces.Caches;
using BuildXL.Utilities;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.VisualStudio.Services.Agent.Util;
using Microsoft.Win32;
using Newtonsoft.Json;
Expand Down
10 changes: 6 additions & 4 deletions src/Agent.Service/Windows/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ static int Main(String[] args)
return 1;
}

EventLog applicationLog = new EventLog("Application");
if (applicationLog.OverflowAction == OverflowAction.DoNotOverwrite)
using (EventLog applicationLog = new EventLog("Application"))
{
Console.WriteLine("[WARNING] The retention policy for Application event log is set to \"Do not overwrite events\".");
Console.WriteLine("[WARNING] Make sure manually clear logs as needed, otherwise AgentService will stop writing output to event log.");
if (applicationLog.OverflowAction == OverflowAction.DoNotOverwrite)
{
Console.WriteLine("[WARNING] The retention policy for Application event log is set to \"Do not overwrite events\".");
Console.WriteLine("[WARNING] Make sure manually clear logs as needed, otherwise AgentService will stop writing output to event log.");
}
}

try
Expand Down
5 changes: 2 additions & 3 deletions src/Agent.Worker/CodeCoverage/CodeCoverageCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ private async Task PublishCodeCoverageAsync(
}
catch (SocketException ex)
{
#pragma warning disable CA2000 // Dispose objects before losing scope
ExceptionsUtil.HandleSocketException(ex, WorkerUtilities.GetVssConnection(executionContext).Uri.ToString(), executionContext.Warning);
#pragma warning restore CA2000 // Dispose objects before losing scope
using var vssConnection = WorkerUtilities.GetVssConnection(executionContext);
ExceptionsUtil.HandleSocketException(ex, vssConnection.Uri.ToString(), executionContext.Warning);
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Agent.Worker/Handlers/StepHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async Task<int> ExecuteAsync(string workingDirectory,
outputEncoding = Encoding.UTF8;
}

var redirectStandardIn = new InputQueue<string>();
using var redirectStandardIn = new InputQueue<string>();
var payloadJson = JsonUtility.ToString(payload);
redirectStandardIn.Enqueue(payloadJson);
HostContext.GetTrace(nameof(ContainerStepHost)).Info($"Payload: {payloadJson}");
Expand Down
15 changes: 13 additions & 2 deletions src/Agent.Worker/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,

IExecutionContext jobContext = null;
CancellationTokenRegistration? agentShutdownRegistration = null;
VssConnection taskConnection = null;
VssConnection legacyTaskConnection = null;

try
{
// Create the job execution context.
Expand Down Expand Up @@ -225,7 +228,9 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
if (taskServerUri != null)
{
Trace.Info($"Creating task server with {taskServerUri}");
await taskServer.ConnectAsync(VssUtil.CreateConnection(taskServerUri, taskServerCredential, trace: Trace));

taskConnection = VssUtil.CreateConnection(taskServerUri, taskServerCredential, Trace);
await taskServer.ConnectAsync(taskConnection);
}

// for back compat TFS 2015 RTM/QU1, we may need to switch the task server url to agent config url
Expand All @@ -236,8 +241,10 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
Trace.Info($"Can't determine task download url from JobMessage or the endpoint doesn't exist.");
var configStore = HostContext.GetService<IConfigurationStore>();
taskServerUri = new Uri(configStore.GetSettings().ServerUrl);

Trace.Info($"Recreate task server with configuration server url: {taskServerUri}");
await taskServer.ConnectAsync(VssUtil.CreateConnection(taskServerUri, taskServerCredential, trace: Trace));
legacyTaskConnection = VssUtil.CreateConnection(taskServerUri, taskServerCredential, trace: Trace);
await taskServer.ConnectAsync(legacyTaskConnection);
}
}

Expand Down Expand Up @@ -380,6 +387,10 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
agentShutdownRegistration = null;
}

legacyTaskConnection?.Dispose();
taskConnection?.Dispose();
jobConnection?.Dispose();

await ShutdownQueue(throwOnFailure: false);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Agent.Worker/WorkerCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public bool TryProcessCommand(IExecutionContext context, string input)
}
catch (SocketException ex)
{
#pragma warning disable CA2000 // Dispose objects before losing scope
ExceptionsUtil.HandleSocketException(ex, WorkerUtilities.GetVssConnection(context).Uri.ToString(), context.Error);
#pragma warning restore CA2000 // Dispose objects before losing scope
using var vssConnection = WorkerUtilities.GetVssConnection(context);

ExceptionsUtil.HandleSocketException(ex, vssConnection.Uri.ToString(), context.Error);
context.CommandResult = TaskResult.Failed;
}
catch (Exception ex)
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.VisualStudio.Services.Agent/HostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class HostContext : EventListener, IObserver<DiagnosticListener>, IObserv
private static int[] _vssHttpCredentialEventIds = new int[] { 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 27, 29 };
private readonly ConcurrentDictionary<Type, object> _serviceInstances = new ConcurrentDictionary<Type, object>();
protected readonly ConcurrentDictionary<Type, Type> ServiceTypes = new ConcurrentDictionary<Type, Type>();
SecretMasker _basicSecretMasker = new SecretMasker();
private readonly ILoggedSecretMasker _secretMasker;
private readonly ProductInfoHeaderValue _userAgent = new ProductInfoHeaderValue($"VstsAgentCore-{BuildConstants.AgentPackage.PackageName}", BuildConstants.AgentPackage.Version);
private CancellationTokenSource _agentShutdownTokenSource = new CancellationTokenSource();
Expand All @@ -89,7 +90,8 @@ public class HostContext : EventListener, IObserver<DiagnosticListener>, IObserv
public ProductInfoHeaderValue UserAgent => _userAgent;
public HostContext(HostType hostType, string logFile = null)
{
_secretMasker = new LoggedSecretMasker(new SecretMasker());
_secretMasker = new LoggedSecretMasker(_basicSecretMasker);

// Validate args.
if (hostType == HostType.Undefined)
{
Expand Down Expand Up @@ -593,6 +595,8 @@ protected virtual void Dispose(bool disposing)
_trace = null;
_httpTrace?.Dispose();
_httpTrace = null;
_basicSecretMasker?.Dispose();
_basicSecretMasker = null;

_agentShutdownTokenSource?.Dispose();
_agentShutdownTokenSource = null;
Expand Down