From da358db24519b7c9987f94d7959ed958974b6ad4 Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Tue, 28 Sep 2021 08:43:00 -0700 Subject: [PATCH] Fix race condition in ServiceProcess tests (#59676) --- .../TestService.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestService.cs b/src/libraries/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestService.cs index 5f0cad1e794c6..05a7176c36031 100644 --- a/src/libraries/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestService.cs +++ b/src/libraries/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestService.cs @@ -36,7 +36,7 @@ public TestService(string serviceName, Exception throwException = null) this._serverStream = new NamedPipeServerStream(serviceName); _waitClientConnect = this._serverStream.WaitForConnectionAsync(); _waitClientConnect = _waitClientConnect.ContinueWith(_ => DebugTrace("TestService " + ServiceName + ": Connected")); - _waitClientConnect.ContinueWith(t => WriteStreamAsync(PipeMessageByteCode.Connected)); + _waitClientConnect = _waitClientConnect.ContinueWith(t => WriteStreamAsync(PipeMessageByteCode.Connected, waitForConnect: false)); DebugTrace("TestService " + ServiceName + ": Ctor completed"); } @@ -99,19 +99,19 @@ protected override void OnStop() { DebugTrace("TestService " + ServiceName + ": OnStop"); base.OnStop(); - WriteStreamAsync(PipeMessageByteCode.Stop).Wait(); + // We may be stopping because the test has completed and we're cleaning up the test service so there's no client at all, so don't waitForConnect. + // Tests that verify "Stop" itself should ensure the client connection has completed before calling stop, by waiting on some other message from the pipe first. + WriteStreamAsync(PipeMessageByteCode.Stop, waitForConnect:false).Wait(); } - public async Task WriteStreamAsync(PipeMessageByteCode code, int command = 0) + public async Task WriteStreamAsync(PipeMessageByteCode code, int command = 0, bool waitForConnect = true) { DebugTrace("TestService " + ServiceName + ": WriteStreamAsync writing " + code.ToString()); var toWrite = (code == PipeMessageByteCode.OnCustomCommand) ? new byte[] { (byte)command } : new byte[] { (byte)code }; // Wait for the client connection before writing to the pipe. - // Exception: if it's a Stop message, it may be because the test has completed and we're cleaning up the test service so there's no client at all. - // Tests that verify "Stop" itself should ensure the client connection has completed before calling stop, by waiting on some other message from the pipe first. - if (code != PipeMessageByteCode.Stop) + if (waitForConnect) { await _waitClientConnect; }