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

Wait-Logging Hanging Scheduled Task #124

Open
krocd opened this issue Apr 7, 2022 · 3 comments
Open

Wait-Logging Hanging Scheduled Task #124

krocd opened this issue Apr 7, 2022 · 3 comments

Comments

@krocd
Copy link

krocd commented Apr 7, 2022

I'm trying to debug why a Windows Scheduled Task is hanging at Wait-Logging, even though all other operations have completed and I see all of the expected log entries flushed to the log file.

The logging target is a file.

The design is:

  • Custom PS script module containing wrapper functions for Add-LoggingTarget, Write-Log, and Wait-Logging. These wrappers are used to set/force certain defaults for our environment and ensure consistent use of logging based on our standards.

  • Script uses custom script module functions to start logging (wrapper for Add-LoggingTarget), write log messages (wrapper for Write-Log), and to stop logging (wrapper for Wait-Logging).

  • When the script is executed by Windows Task Scheduler, execution hangs at the wrapper function called to stop logging (Wait-Logging). I see all expected messages in the log file, however it seems that the call to Wait-Logging is simply hanging until the Task Scheduler times out the task and kills it.

Is Wait-Logging or the LoggingEventQueue affected by the CallerScope? The documentation isn't really clear on exactly where the caller scope is referenced and what all it affects.

@krocd
Copy link
Author

krocd commented Apr 8, 2022

I was wrong, it's not Wait-Logging that is blocking the process exit. It's something else.

I was able to reproduce it outside of Task Scheduler by simply executing the same script at the command line (recall that the script imports a custom module, that imports the Logging module and wraps its functions):

%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NonInteractive -File "<Path to script file>"

The command prompt simply hangs waiting on the spawned powershell.exe process to end; which it never does.

I even tried adding a Remove-Module -Name Logging to the custom module function that "stops" logging, after calling Wait-Logging, which it looks like is already done by your OnRemoval event handler. However, this also had no impact on the blocking.

@krocd
Copy link
Author

krocd commented Apr 8, 2022

The issue seems to be in the OnRemoval event handler. For some reason, the call to Get-Module Logging is returning null, which causes the if ($Module) { condition to evaluate to false, and Stop-LoggingManager never gets called.

@ecarmen16
Copy link

I've seen similar and I chalked it up to where on the filesystem I was having the SYSTEM principal try to do Import-Module. I had different results with it in the System32\windowsPowershell\1.0\modules folder vs when used elsewhere too. Hope you figure it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants