Powershell: Background process hangs until UI is triggered

Created on 9 Aug 2020  路  4Comments  路  Source: PowerShell/PowerShell


I'm seeing some weird behavior trying to pipe a command to AutoHotkey. It seems to hang in the background until a pwsh UI event causes it to complete.

Steps to reproduce

'WinMaximize, ahk_class ENMainFrame' | D:\path\to\AutoHotkey\AutoHotkey.exe *

Expected behavior

Completes within a few seconds and maximizes the target window. It leaves no AutoHotkey processes running:

PS D:\projects> ps auto*
PS D:\projects>

Actual behavior

The command returns quickly, but the target window is not maximized, and there remains a background process:

PS D:\projects> ps auto*

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     12     1.96       7.65       0.03   13804   1 AutoHotkey

This background process hangs without maximizing the window for 7+ minutes if the terminal is left alone. However, if I hold down the ENTER key, (displaying a series of new prompts), within a few seconds the targeted window will be maximized and the background process will terminate. Similarly, I can type .\ and then hold TAB for a few seconds (cycling through file name completions) with the same effect. Also, adding a & at the end fixes the problem:

'WinMaximize, ahk_class ENMainFrame' | D:\tools\AutoHotkey\AutoHotkey.exe * & 

Environment data

This behavior is present with:

  • pwsh (7.0.3) with ConEmu
  • pwsh (7.0.3) with traditional windows terminal ui
  • pwsh -NoProfile (7.0.3)

But not present with:

  • powershell (5.1.18362.752, after adjusting the default encoding)
  • cmd

$PSVersionTable :

Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question Resolution-Answered

All 4 comments

So AutoHotkey isn't a console application, so typically PowerShell doesn't wait for it's completion before returning. I think stdin is still being shared though, so AutoHotkey thinks it's still waiting for more script.

As a workaround, if you pipe the output of AutoHotkey to something it'll force it to wait until completion and properly close stdin. e.g.

'WinMaximize, ahk_class ENMainFrame' | D:\path\to\AutoHotkey\AutoHotkey.exe * | Write-Output

@SeeminglyScience Thanks, that workaround does work on my machine. I'm unsure of the next step. Do you think this is a AutoHotkey bug instead (maybe it's not acknowledging the EOF or something)? Should I submit a bug report to AHK? Or do you think pwsh is failing to close stdin properly in this case?

It's probably mostly PowerShell's fault. The only question is if it's typically by design and AHK is just an edge case. Either way I don't think there's anything on AHK's end to do.

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings