Skip to content

Commit

Permalink
Prevent ServiceControllerTests from dispose when already disposed (do…
Browse files Browse the repository at this point in the history
…tnet/corefx#24042)

* Disable ServiceProcessTest that has been failing on CI and official builds

* Prevent dispose from been called when already disposed

* Add logging to repro if RemoveService is been called twice on the same ServiceController


Commit migrated from dotnet/corefx@c017ae3
  • Loading branch information
safern authored Sep 18, 2017
1 parent 5738bdf commit 7925e08
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
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;
}
}
}
}

0 comments on commit 7925e08

Please sign in to comment.