Skip to content

A timed-out run throws on Windows PowerShell 5.1 when the caller's error preference is Stop #86

Open

Description

@fadwen

Summary

On Windows PowerShell 5.1, a script run that times out can end with a terminating error instead of a result whose run state is TimedOut. Invoke-IslProcess ends the timed-out process tree with & taskkill.exe /PID <id> /T /F 2>&1 | Out-Null. When taskkill writes to its error stream, which it does for a process that is already gone, Windows PowerShell 5.1 turns the redirected line into an error record, and with $ErrorActionPreference = 'Stop' in a scope the function sees that record is terminating. PowerShell 7 does not do this.

The preference is Stop in every pwsh and powershell step of GitHub Actions and in any script that follows ERR-01 and is started with -File. Windows PowerShell 5.1 is the host the harness exists for.

Findings

Where What
Private/Invoke-IslProcess.ps1:241 The scheduled-task path (-Context System, and -Credential). After Stop-ScheduledTask it lists every process whose command line carries the run id and calls taskkill for each. Stop-ScheduledTask has usually ended some of them already, and /T ends the children of the first, so a later id in the list is often gone: taskkill answers ERROR: The process "<id>" not found. with exit code 128.
Private/Invoke-IslProcess.ps1:101 The direct path has the same statement. The process is normally still there when it times out, so taskkill succeeds, but anything it writes to its error stream has the same effect.
both The exit code of taskkill is never read (ERR-23), so a process tree that could not be ended is not reported either.

Evidence

  • quality-gates.yml run 45145190738, job Windows PowerShell 5.1, on the branch of test: decide the skip conditions of four tests during discovery #85, which is the first change that lets the SYSTEM-context integration tests run: SYSTEM context.times out and cleans up the task failed with RemoteException: ERROR: The process "2020" not found. at Invoke-IslProcess.ps1:241. The same test passed in run 45140123789 on the same branch one commit earlier, and on a lab device where the preference was Continue, so the failure depends on timing and on the preference.

  • On Windows 11, in a script started with -File:

    Host $ErrorActionPreference & taskkill.exe /PID 999999 /T /F 2>&1 | Out-Null
    Windows PowerShell 5.1.26100 Stop terminating RemoteException: ERROR: The process "999999" not found.
    Windows PowerShell 5.1.26100 Continue no error, exit code 128
    PowerShell 7.6.10 Stop no error, exit code 128
    PowerShell 7.6.10 Continue no error, exit code 128

Proposed change

  • End a process tree in one private function that starts taskkill.exe as a process with its streams redirected by .NET, so that nothing it writes becomes a PowerShell error record on either version.
  • Treat a process that is already gone as the outcome the cleanup wants, and warn only when the process is still running afterwards.
  • Test the function on Windows PowerShell 5.1 with the preference set to Stop and an id that does not exist, which is the failing case made deterministic.

Risk

Low. The cleanup runs only after a timeout. The change has to be run on a lab device as SYSTEM, where the scheduled-task path runs, and in CI under Windows PowerShell 5.1 with #85 in place.

Discovered and written up with Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions