Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Prevent ServiceControllerTests from dispose when already disposed #24042

Merged
merged 3 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -21,6 +21,8 @@ public class ServiceBaseTests : IDisposable
private static readonly Lazy<bool> s_isElevated = new Lazy<bool>(() => AdminHelpers.IsProcessElevated());
protected static bool IsProcessElevated => s_isElevated.Value;

private bool _disposed;

public ServiceBaseTests()
{
_testService = new TestServiceProvider();
Expand Down Expand Up @@ -174,7 +176,11 @@ public void TestOnContinueBeforePause()

public void Dispose()
{
_testService.DeleteTestServices();
if (!_disposed)
{
_testService.DeleteTestServices();
_disposed = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ServiceControllerTests : IDisposable
protected static bool IsProcessElevated => s_isElevated.Value;

private const int ExpectedDependentServiceCount = 3;
private bool _disposed;

public ServiceControllerTests()
{
Expand Down Expand Up @@ -190,7 +191,11 @@ public void ServicesStartMode()

public void Dispose()
{
_testService.DeleteTestServices();
if (!_disposed)
{
_testService.DeleteTestServices();
_disposed = true;
}
}

private static ServiceController AssertHasDependent(ServiceController controller, string serviceName, string displayName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class TestServiceInstaller
{
public const string LocalServiceName = "NT AUTHORITY\\LocalService";

private string _removalStack;

public TestServiceInstaller()
{
}
Expand Down Expand Up @@ -128,6 +130,19 @@ public unsafe void Install()

public void RemoveService()
{
if (ServiceName == null)
throw new InvalidOperationException($"Already removed service at stack ${_removalStack}");

// Store the stack for logging in case we're called twice
try
{
throw new Exception();
}
catch (Exception e)
{
_removalStack = e.StackTrace;
}

// Stop the service
using (ServiceController svc = new ServiceController(ServiceName))
{
Expand Down Expand Up @@ -161,6 +176,8 @@ public void RemoveService()

Interop.Advapi32.CloseServiceHandle(serviceManagerHandle);
}

ServiceName = null;
}
}
}
}