Skip to content

fix(harness): end a timed-out run without taskkill's errors - #87

Open
fadwen wants to merge 12 commits into
mainfrom
fix/process-tree-cleanup
Open

fadwen wants to merge 12 commits into
mainfrom
fix/process-tree-cleanup

Conversation

@fadwen

@fadwen fadwen commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

On Windows PowerShell 5.1, a run that timed out could end with a terminating error instead of a result whose run state is TimedOut. Invoke-IslProcess ended the process tree with & taskkill.exe /PID <id> /T /F 2>&1 | Out-Null. Windows PowerShell 5.1 turns a line that a native command writes to a redirected error stream into an error record, and with $ErrorActionPreference = 'Stop' in a scope the function sees, as in a CI step or a script started with -File, that record ends the command. taskkill writes such a line for a process that is already gone, which is common in the scheduled-task path. Closes #86.

This pull request is stacked on #84 (fix/strict-mode-root-module) and merges after it. #85 is now stacked on this one, because #85 is the change that lets the SYSTEM-context tests run in CI, and its timeout test is the one that met this defect.

Changes

  • New private function Stop-IslProcessTree. It starts taskkill.exe as a process whose streams .NET redirects, so nothing taskkill writes reaches PowerShell as an error, on either version. A process that is already gone (exit code 128) is the state the cleanup wants and is not reported. When taskkill fails for another reason and the process is still running, a warning says so; the exit code was never read before (ERR-23).
  • Invoke-IslProcess calls it in both places that ended a tree: after a direct run times out, and for each process of a scheduled-task run that timed out.
  • Tests/Unit/Private/Stop-IslProcessTree.Tests.ps1 runs it against a real tree, cmd.exe running ping.exe: the tree ends, -WhatIf ends nothing, a process that is already gone neither fails nor warns under an error preference of Stop, a timed-out Invoke-IslProcess returns its result under that preference, and it ends within its timeout when the caller's $WhatIfPreference is on.
  • CHANGELOG.md has the entry under Fixed.

Verification

  • The mechanism, in a script started with -File on Windows 11: with $ErrorActionPreference = 'Stop', & taskkill.exe /PID 999999 /T /F 2>&1 | Out-Null ends with a terminating RemoteException: ERROR: The process "999999" not found. on Windows PowerShell 5.1.26100 and with no error and exit code 128 on PowerShell 7.6.10. With the preference Continue neither version raises an error.
  • Stop-IslProcessTree.Tests.ps1, Invoke-IslProcess.Tests.ps1 and Module.Contract.Tests.ps1 on PowerShell 7.6.10 and on Windows PowerShell 5.1: 47 passed, 0 failed on each, and no ping.exe was left running. After the change for the WhatIf preference, the two process test files on both versions: 27 passed, 0 failed; the new test took 29 seconds and failed without that change.
  • Invoke-ScriptAnalyzer at Error and Warning on the three changed script files: no findings. Test-StandardsCompliance.ps1 of ai-powershell-standards 2.0.0 on the two new files: no findings.
  • Every suite at the top of the stack, which has this change and test: decide the skip conditions of four tests during discovery #85, through ./Tests/Invoke-Tests.ps1: on PowerShell 7.6.10 with -CodeCoverage 1,069 passed, 3 failed, 7 skipped, coverage 95.31 percent; on Windows PowerShell 5.1 1,033 passed, 3 failed, 43 skipped. The three failures are the ones that fail on that computer without this change and pass in CI (Repair-IntuneScript corrupts an ANSI script on a host with a UTF-8 system code page #75 twice, In-box module table does not list UOPProviders, present on Windows 11 26H2 #76).
  • On a lab device, as NT AUTHORITY\SYSTEM under Windows PowerShell 5.1.26100.9818 with $ErrorActionPreference = 'Stop' in the global scope, the SYSTEM-context suite five times over. Without this change: 10 passed, 5 failed, the timeout test every time, with ERROR: The process "1584" not found. in two rounds and ERROR: The process with PID 6640 (child process of PID 8524) could not be terminated. in three. With it: 15 passed, 0 failed, and no IntuneScriptLab-* scheduled task left behind. The unit tests of the two process functions in the same session: 26 passed.
  • quality-gates.yml, dispatched on this branch, run 45148793084: all five jobs passed. The SYSTEM-context tests do not run on this branch; they do on test: decide the skip conditions of four tests during discovery #85, where the Windows PowerShell 5.1 job failed on the timeout test in run 45145190738 without this change and the workflow passed three times out of three with it (runs 45148797974, 45148802814 and 45148807467).

Notes

  • The warning path, a process that taskkill could not end, has no test: staging one needs a process the session may not end, and the only dependable one is a system process.
  • Stop-IslProcessTree declares SupportsShouldProcess (FUN-30). The two callers pass -Confirm:$false and -WhatIf:$false, because the cleanup must neither wait for an answer nor be skipped by a preference: a function of the module sees a global $WhatIfPreference, and in the direct path the run waits for the process after the cleanup.

The three tests of the SYSTEM-context integration suite and the
file-version test of Test-IntuneWin32Rule were skipped on every run,
in an elevated session and on a computer that has notepad.exe as well.
Their -Skip condition read a script-scoped variable that the file set
in BeforeAll. Pester evaluates -Skip during discovery, before any
BeforeAll runs, so the variable was unset, the condition was true, and
a skipped test still leaves the run Passed.

Both variables are now set in BeforeDiscovery, as the other suites
with a skip condition do. The SYSTEM-context tests are the only ones
that launch a script through a scheduled task as NT AUTHORITY\SYSTEM,
so that path is tested again wherever the session is elevated.

Refs #83
On Windows PowerShell 5.1, a run that timed out could end with a
terminating error instead of a result whose run state is TimedOut.
Invoke-IslProcess ended the process tree with

    & taskkill.exe /PID <id> /T /F 2>&1 | Out-Null

and 5.1 turns a line that a native command writes to a redirected
error stream into an error record. With $ErrorActionPreference = 'Stop'
in a scope the function sees, as in a CI step or a script started with
-File, that record is terminating. taskkill writes such a line for a
process that is already gone, which is common in the scheduled-task
path: stopping the task has ended some of the processes, and ending
one ends its children.

The new private Stop-IslProcessTree starts taskkill.exe as a process
whose streams .NET redirects, so nothing it writes reaches PowerShell
as an error on either version. A process that is already gone is the
state the cleanup wants and is not reported. The exit code is read
now: when taskkill fails for another reason and the process is still
running, a warning says so.

Refs #86

@fadwen fadwen left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve after one should-fix.

Verified

  • Closes #86, by reading the code. taskkill now runs as a .NET Process with both streams redirected and read asynchronously, so nothing it writes becomes an error record on Windows PowerShell 5.1.
  • Exit codes: 0 and 128 are treated as success. Any other code warns only if the process is still alive (ERR-23).
  • Call sites: both places in Invoke-IslProcess now call the new function.
  • taskkill path: taken from [Environment]::SystemDirectory, not from PATH.

Unverified (needs Windows): the 5.1 reproduction, the 5-round lab run as SYSTEM, and the CI runs 45148793084, 45148797974, 45148802814 and 45148807467. This PR has no checks because its base isn't main.

Should-fix: Private/Invoke-IslProcess.ps1 lines 101 and 243, the cleanup can be skipped by $WhatIfPreference and then hang

  • Both calls pass -Confirm:$false but not -WhatIf:$false. A module function sees a global $WhatIfPreference.
  • Reproduced: with $global:WhatIfPreference = $true, Stop-IslProcessTree -ProcessId 999999 -Confirm:$false prints "What if: Performing the operation "End"…" and runs no taskkill.
  • In the direct path, the next line is $process.WaitForExit() with no timeout. A timed-out script that never exits therefore hangs the run for good, where the old & taskkill always ran.
  • Fix: add -WhatIf:$false to both calls, the same way MOD-14 keeps non-publish steps out of -WhatIf. Add a test that sets $WhatIfPreference = $true around a timed-out Invoke-IslProcess and checks that it returns TimedOut.

Nits

  • Should cleanup be ShouldProcess-gated at all? The cleanup is not a user-facing change, and both callers opt out of it. Dropping SupportsShouldProcess would remove this whole class of problem. If FUN-30 is read as covering it, keep it as it is plus the fix above.
  • catch { $PSCmdlet.ThrowTerminatingError($_) }: if taskkill.exe can't be started, the timed-out run ends with that error instead of its TimedOut result. Writing a warning there would keep the run's result, which is what this PR is about.

Two comments in quality-gates.yml said that the hosted runner's token
is not elevated and that the SYSTEM-context tests therefore skip in
CI. They never ran because their skip condition was evaluated before
its variable was set, not because of the runner: with the condition
decided during discovery, the tests job and the Windows PowerShell 5.1
job each run the three tests, and the file takes 14 seconds where it
took 62 milliseconds.

The comments now say that the runner is elevated and that those jobs
register and remove IntuneScriptLab-* scheduled tasks as SYSTEM on it.

Refs #83
Stop-IslProcessTree declares SupportsShouldProcess, and the two calls
in Invoke-IslProcess passed -Confirm:$false only. A function of the
module sees a global $WhatIfPreference, so with it set the cleanup
printed what it would do and ended nothing. In the direct path the
next statement waits for the process without a time limit: a script
that never exits would have held the run for good, where the taskkill
call this function replaced always ran.

Both calls pass -WhatIf:$false as well. The cleanup after a timeout
is not the caller's to preview. The new test sets the preference
around a timed-out run of a 30-second process: it took 29 seconds
before this change and ends within the timeout now.

Refs #86
Base automatically changed from fix/strict-mode-root-module to main November 23, 2026 10:12
@fadwen

fadwen commented Nov 23, 2026

Copy link
Copy Markdown
Owner Author

The should-fix is addressed in 5664fdc. The two nits are answered below; neither is taken.

Cleanup skipped under $WhatIfPreference: fixed.

  • Reproduced with the test the review asked for: with the preference set around a timed-out Invoke-IslProcess of a 30-second process, the run took 29 seconds, because the cleanup printed what it would do and the run then waited for the process.
  • Both calls in Invoke-IslProcess now pass -WhatIf:$false as well as -Confirm:$false.
  • The test is in Stop-IslProcessTree.Tests.ps1. It passes on PowerShell 7.6.10 and on Windows PowerShell 5.1 and fails without the change. It uses a process that ends by itself after half a minute, so a regression fails the test and cannot hang the suite.
  • Not run: the scheduled-task path under a WhatIf preference. That path registers a task first, and whether it gets as far as the cleanup under that preference was not checked.

Should the cleanup be gated by ShouldProcess at all: kept. FUN-30 is a MUST for a function that changes state, and a departure from it is the owner's to record (DOC-22), not something to settle in a review reply. With -WhatIf:$false at both call sites the gate can no longer skip the cleanup.

catch { $PSCmdlet.ThrowTerminatingError($_) } when taskkill.exe cannot be started: kept.

  • In the direct path the caller waits for the process after the cleanup, without a time limit. If the function only warned, a script that never exits would hold the run for good. The error ends the run, as the call-operator form did when taskkill.exe could not be found.
  • Keeping the TimedOut result in that case needs a second way to end the process. That is a larger change than this one, for a failure that needs taskkill.exe to be missing from System32.

The checks that could not be made in the sandbox

  • CI after the commit: run 45153303224 passed all five jobs on this branch, and run 45153308602 did on test: decide the skip conditions of four tests during discovery #85, where the SYSTEM-context tests run.
  • The lab run in the description, five rounds as SYSTEM under an error preference of Stop, was made before this commit, which changes only the two call sites.

@fadwen fadwen left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review after 5664fdc and the merge of main. Verdict: approve. The should-fix is resolved, and I accept your reasons for keeping both nits as they are.

Verified

  • Diff against main: only this PR's change is left (Stop-IslProcessTree, the two call sites, the tests and the CHANGELOG).
  • Both call sites: each passes -Confirm:$false -WhatIf:$false, with a comment explaining why.
  • The new test: it sets $WhatIfPreference = $true around a timed-out Invoke-IslProcess and asserts TimedOut within 15 s. Its target is a process that ends by itself, so a regression fails the test instead of hanging the suite.
  • CI: run 45154229071 on this PR passed all five jobs, including Windows PowerShell 5.1.
  • The #84 follow-up merged in here (35fd3e7): Get-IslPropertyValue now reads a generic Dictionary[string,object]. The case I reproduced returns 1, and Get-IslPropertyValue.Tests.ps1 passes 16/16 in my sandbox.

Optional follow-up (not blocking): as you say, the scheduled-task path under a WhatIf preference wasn't checked. Register-ScheduledTask and Start-ScheduledTask honour $WhatIfPreference themselves, so under it no task would be registered, and the run would only wait out its timeout. It's worth a look, or a -WhatIf:$false on those calls, in a later change.

test: decide the skip conditions of four tests during discovery
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

Successfully merging this pull request may close these issues.

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

1 participant