SysToolsLib icon indicating copy to clipboard operation
SysToolsLib copied to clipboard

Getting stuck in Remove-PSThread in the finally block

Open BlackbirdSR71Est opened this issue 5 years ago • 1 comments

Hi,

Thank you for creating this Powershell Windows Service template.

I ran into an issue with the template in the part where Service part of the code fails with some exception and the logic falls into the "finally" block of the code. As part of the cleanup, "Get-PSThread | Remove-PSThread" is being performed. The Remove-PSThread will call Receive-PSThread to release the resources. The problem is that EndInvoke() method being used in the function will wait for the job to be finished and in this case, we are waiting for a message from the SCM (Receive-PipeMessage) and it may never come. I have tried to replace the EndInvoke() call with the Stop() call which is supposed to kill the job but I am still getting stuck over the call. In short, I am trying to

Start-PipeHandlerThread -pipeName "ControlTest" # Start the thread Get-PSThread | Remove-PSThread # Shutting it down before any message has been received

The solution I have used to get around in getting stuck in the "finally" block is to inject a message to the named pipe before the cleanup by using Send-PipeMessage.

BlackbirdSR71Est avatar Oct 28 '20 23:10 BlackbirdSR71Est

Hi,

thanks for the feedback. If you're confident that your fix resolves the issue you mentioned, please do a pull request for the original script with just that change. I'll review it and, if it also looks good to me, I'll merge it in.

JFLarvoire avatar Oct 31 '20 18:10 JFLarvoire