Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ Test-Assert ((Get-HarnessShape $shapeJobs) -eq 'ForceStoppedCount,CrashedCount,A
$shapeFindings = Test-PkWorkerSafety -WorkerScript { param($Id) $null = [Func[int]]{ 1 } }
$shapeFinding = $shapeFindings[0]
Test-Assert ((Get-HarnessShape $shapeFinding) -eq 'Rule,Where,Line,Text') ("worker-safety findings are objects (" + (Get-HarnessShape $shapeFinding) + ")")
Test-Assert ((Get-HarnessShape (Initialize-ParallelKit)) -eq 'AbortHandler,ChildProcessJob,ThreadPool') "Initialize-ParallelKit returns an object"
Test-Assert ((Get-HarnessShape (Initialize-ParallelKit)) -eq 'AbortHandler,ChildProcessJob,ThreadPool,Version,StartMode') "Initialize-ParallelKit returns an object"

$fields = @('Command', 'ExitCode', 'Stdout', 'Stderr', 'StreamTimedOut', 'StreamError', 'StartError', 'Aborted', 'TimedOut')
$batch = Invoke-PkProcessBatch -MaxConcurrent 1 -Commands @(
Expand Down Expand Up @@ -4304,6 +4304,56 @@ foreach ($s in $tkStarted) { $null = Complete-PkProcess $s -Dispose }
Test-Assert ($tkKilled -and $tkPings.Count -eq 3 -and $tkLeft.Count -eq 0) ("with one PID per taskkill call, every program's children are still killed (" + $tkPings.Count + " children seen, " + $tkLeft.Count + " left running)")
Write-Host ""

Write-Host "=== AW: when a program can't be created in the Job Object ===" -ForegroundColor Cyan
# A start whose setup fails (pipes, the attribute list, the job attribute)
# falls back to Process.Start and the Job Object just after, with a warning
# shown once, since that costs the cleanup of a child the program starts
# straight away. The harness's own switch to that path stays quiet.
Reset-Harness
$fbReset = [PkChildProcessJob].GetMethod('TestResetFallbackWarning')
if ($null -ne $fbReset) { $null = $fbReset.Invoke($null, @()) }
$fbOk = @(); $fbError = $null
[PkChildProcessJob]::TestFailSetup = $true
try {
foreach ($i in 1..2) {
$s = Start-PkProcess -FilePath $cmdExe -Arguments '/d /c ping -n 2 127.0.0.1 >nul& exit /b 3'
$fbInJob = [PkHarnessJobProbe]::InJob($s['Proc'].Id, $jobHandle)
$r = Complete-PkProcess $s -Dispose
$fbOk += ($r.ExitCode -eq 3 -and $fbInJob -ne $false)
}
} catch { $fbError = [string]$_ } finally { [PkChildProcessJob]::TestFailSetup = $false }
Test-Assert ($null -eq $fbError -and ($fbOk -join ',') -eq 'True,True') ("a start whose setup fails falls back to Process.Start, still in the Job Object (results: " + ($fbOk -join ',') + ", error: " + $fbError + ")")
$fbWarnings = @($script:messages | Where-Object { $_.Level -eq 'Warn' -and $_.Message -like '*Process.Start*' })
Test-Assert ($fbWarnings.Count -eq 1 -and $fbWarnings[0].Message -like '*straight away*') ("and warns once, saying what it costs (" + $fbWarnings.Count + " warning(s))")
Reset-Harness
if ($null -ne $fbReset) { $null = $fbReset.Invoke($null, @()) }
[PkChildProcessJob]::TestUseProcessStart = $true
try { $null = Complete-PkProcess (Start-PkProcess -FilePath $cmdExe -Arguments '/d /c exit 0') -Dispose } finally { [PkChildProcessJob]::TestUseProcessStart = $false }
Test-Assert (@($script:messages | Where-Object { $_.Level -eq 'Warn' }).Count -eq 0) "the harness's switch to Process.Start doesn't warn"

# A program whose output readers can't be built isn't left running untracked:
# the start fails and the program is ended.
[PkChildProcessJob]::TestFailReaders = $true
$rdError = $null
try { $null = Start-PkProcess -FilePath $pingExe -Arguments '-n 59 127.0.0.1' } catch { $rdError = [string]$_ } finally { [PkChildProcessJob]::TestFailReaders = $false }
$rdLeft = @(Get-CimInstance Win32_Process -Filter "Name='PING.EXE'" | Where-Object { [string]$_.CommandLine -like '* -n 59 *' })
foreach ($l in $rdLeft) { Stop-Process -Id $l.ProcessId -Force -ErrorAction SilentlyContinue }
Test-Assert ($rdError -like '*could not start*' -and $rdLeft.Count -eq 0) ("a program whose output readers fail to build is ended and the start reported as failed (" + $rdLeft.Count + " left running, error: " + $rdError + ")")

# Initialize-ParallelKit reports the kit's version and how it starts programs.
$ipHeader = [regex]::Match([System.IO.File]::ReadAllText((Join-Path $PSScriptRoot 'ParallelKit.ps1')), '(?m)^# Version (\d+\.\d+)').Groups[1].Value
$ipError = $null; $ip = $null; $ipAdded = $null
try {
$ip = Initialize-ParallelKit
[PkChildProcessJob]::TestUseProcessStart = $true
try { $ipAdded = (Initialize-ParallelKit).StartMode } finally { [PkChildProcessJob]::TestUseProcessStart = $false }
} catch { $ipError = [string]$_ }
$ipVersion = $null; $ipMode = $null
if ($null -ne $ip -and $null -ne $ip.PSObject.Properties['Version']) { $ipVersion = [string]$ip.Version; $ipMode = $ip.StartMode }
Test-Assert ($ipHeader -ne '' -and $ipVersion -eq $ipHeader) ("Initialize-ParallelKit's Version matches the header's Version line (" + $ipVersion + " vs " + $ipHeader + ")")
Test-Assert ($ipMode -eq 'CreatedInJob' -and $ipAdded -eq 'AddedToJob') ("and its StartMode says how programs start: CreatedInJob normally, AddedToJob without the start lock (" + $ipMode + ", " + $ipAdded + ", error: " + $ipError + ")")
Write-Host ""

Set-PkMessageHandler -Handler $null
Reset-PkAbortState

Expand Down
154 changes: 112 additions & 42 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# PowerShell 7.
#
# Version 3.2 - https://github.com/HalRiveria/PowerShell-ParallelKit
# (Initialize-ParallelKit reports the version too: change both together.)
# Copyright (c) 2026 HalRiveria. MIT License: keep this notice with any copy;
# the full text is in LICENSE in the repository.
#
Expand Down Expand Up @@ -163,7 +164,7 @@ public static class PkState

// Bump this, and the check after the type definitions, whenever any C#
// in this file changes: a session keeps the types it compiled first.
public const int TypesVersion = 5;
public const int TypesVersion = 6;

// Locked: Register-/Unregister-PkCancellationSource are callable from
// worker runspaces, and List<T> is not safe for concurrent writers.
Expand Down Expand Up @@ -523,6 +524,29 @@ public static class PkChildProcessJob

// Harness only: makes TryStart return null, to test the Process.Start path.
public static bool TestUseProcessStart = false;
// Harness only: make a start's setup fail, or building its output readers.
public static bool TestFailSetup = false;
public static bool TestFailReaders = false;

// How programs start here, for Initialize-ParallelKit: "CreatedInJob"
// (TryStart), "AddedToJob" (Process.Start, then the job: no usable start
// lock, or the harness switch), or "NoJob" (the job couldn't be set up,
// so nothing is cleaned up on a kill). A single start can still fall back
// from CreatedInJob when its setup fails; that warns.
public static string StartMode
{
get
{
if (_job == IntPtr.Zero) { return "NoJob"; }
if (TestUseProcessStart || GetStartLock() == null) { return "AddedToJob"; }
return "CreatedInJob";
}
}

// The fallback warning is shown once per process: true for the first caller.
private static int _fallbackWarned = 0;
public static bool ClaimFallbackWarning() { return Interlocked.Exchange(ref _fallbackWarned, 1) == 0; }
public static void TestResetFallbackWarning() { Interlocked.Exchange(ref _fallbackWarned, 0); }

private static StringBuilder BuildCommandLine(string fileName, string arguments)
{
Expand Down Expand Up @@ -553,14 +577,17 @@ public static class PkChildProcessJob
}

// Starts the program in the job, or returns null when it can't do that
// safely (no job, no usable start lock): the caller then uses
// Process.Start and Assign. Throws Win32Exception when Windows refuses,
// as Process.Start does. encoding null: the console's output code page.
public static PkStartedProcess TryStart(string fileName, string arguments, string workingDirectory, Encoding encoding)
// safely: the caller then uses Process.Start and Assign. fallbackReason
// says why, for the caller's one-time warning; it stays null where no
// warning is due (no job, which has its own; the harness switch). Throws
// Win32Exception when Windows refuses to create the program, as
// Process.Start does. encoding null: the console's output code page.
public static PkStartedProcess TryStart(string fileName, string arguments, string workingDirectory, Encoding encoding, out string fallbackReason)
{
fallbackReason = null;
if (TestUseProcessStart || _job == IntPtr.Zero) { return null; }
StartLock startLock = GetStartLock();
if (startLock == null) { return null; }
if (startLock == null) { fallbackReason = ".NET's process-start lock isn't where the kit expects it on this .NET version"; return null; }
if (encoding == null)
{
try { encoding = Encoding.GetEncoding((int)GetConsoleOutputCP()); }
Expand All @@ -570,43 +597,57 @@ public static class PkChildProcessJob

IntPtr outRead = IntPtr.Zero, outWrite = IntPtr.Zero, errRead = IntPtr.Zero, errWrite = IntPtr.Zero;
IntPtr attrList = IntPtr.Zero, handleArr = IntPtr.Zero, jobArr = IntPtr.Zero;
bool attrReady = false, created = false;
bool attrReady = false, created = false, setupFailed = false;
IntPtr stdin = IntPtr.Zero;
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

startLock.Enter();
try
{
try
{
CreateOutputPipe(out outRead, out outWrite);
CreateOutputPipe(out errRead, out errWrite);

// stdin as Process.Start passes it: PowerShell's own. Listed
// only if inheritable, the only case the child could use it.
IntPtr stdin = GetStdHandle(STD_INPUT_HANDLE);
uint hflags;
bool stdinInherit = stdin != IntPtr.Zero && stdin != new IntPtr(-1)
&& GetHandleInformation(stdin, out hflags) && (hflags & HANDLE_FLAG_INHERIT) != 0;
int handleCount = stdinInherit ? 3 : 2;
handleArr = Marshal.AllocHGlobal(IntPtr.Size * handleCount);
Marshal.WriteIntPtr(handleArr, 0, outWrite);
Marshal.WriteIntPtr(handleArr, IntPtr.Size, errWrite);
if (stdinInherit) { Marshal.WriteIntPtr(handleArr, 2 * IntPtr.Size, stdin); }
jobArr = Marshal.AllocHGlobal(IntPtr.Size);
Marshal.WriteIntPtr(jobArr, _job);

IntPtr size = IntPtr.Zero;
InitializeProcThreadAttributeList(IntPtr.Zero, 2, 0, ref size); // fails by design: returns the size
attrList = Marshal.AllocHGlobal(size);
if (!InitializeProcThreadAttributeList(attrList, 2, 0, ref size)) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
attrReady = true;
if (!UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handleArr,
(IntPtr)(IntPtr.Size * handleCount), IntPtr.Zero, IntPtr.Zero)
|| !UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, jobArr,
(IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))
// Setup: a failure here falls back to Process.Start and Assign
// (3.1's way) instead of failing the start. Only CreateProcess
// itself, below, reports that the program couldn't start.
try
{
throw new Win32Exception(Marshal.GetLastWin32Error());
CreateOutputPipe(out outRead, out outWrite);
CreateOutputPipe(out errRead, out errWrite);

// stdin as Process.Start passes it: PowerShell's own. Listed
// only if inheritable, the only case the child could use it.
stdin = GetStdHandle(STD_INPUT_HANDLE);
uint hflags;
bool stdinInherit = stdin != IntPtr.Zero && stdin != new IntPtr(-1)
&& GetHandleInformation(stdin, out hflags) && (hflags & HANDLE_FLAG_INHERIT) != 0;
int handleCount = stdinInherit ? 3 : 2;
handleArr = Marshal.AllocHGlobal(IntPtr.Size * handleCount);
Marshal.WriteIntPtr(handleArr, 0, outWrite);
Marshal.WriteIntPtr(handleArr, IntPtr.Size, errWrite);
if (stdinInherit) { Marshal.WriteIntPtr(handleArr, 2 * IntPtr.Size, stdin); }
jobArr = Marshal.AllocHGlobal(IntPtr.Size);
Marshal.WriteIntPtr(jobArr, _job);

IntPtr size = IntPtr.Zero;
InitializeProcThreadAttributeList(IntPtr.Zero, 2, 0, ref size); // fails by design: returns the size
attrList = Marshal.AllocHGlobal(size);
if (!InitializeProcThreadAttributeList(attrList, 2, 0, ref size)) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
attrReady = true;
if (!UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handleArr,
(IntPtr)(IntPtr.Size * handleCount), IntPtr.Zero, IntPtr.Zero)
|| !UpdateProcThreadAttribute(attrList, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, jobArr,
(IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (TestFailSetup) { throw new Win32Exception(5); }
}
catch (Win32Exception e)
{
setupFailed = true;
fallbackReason = "setting up a start inside the Job Object failed (" + e.Message + ")";
}
if (setupFailed) { return null; } // the finally blocks below clean up and release the lock

STARTUPINFOEX si = new STARTUPINFOEX();
si.StartupInfo.cb = Marshal.SizeOf(typeof(STARTUPINFOEX));
Expand Down Expand Up @@ -645,17 +686,32 @@ public static class PkChildProcessJob
}
finally { startLock.Exit(); }

// Everything up to the resume is guarded, the output readers included:
// if any step fails, the program (still suspended) is ended and every
// handle released, so nothing is left running untracked.
Process proc = null;
SafeFileHandle outSafe = null, errSafe = null;
StreamReader stdoutReader = null, stderrReader = null;
try
{
proc = Process.GetProcessById(pi.dwProcessId);
IntPtr opened = proc.Handle; // taken while it's suspended, so ExitCode stays readable however fast it exits
outSafe = new SafeFileHandle(outRead, true);
errSafe = new SafeFileHandle(errRead, true);
if (TestFailReaders) { throw new IOException("Test: building the output readers failed."); }
stdoutReader = new StreamReader(new FileStream(outSafe, FileAccess.Read, 4096, false), encoding, true, 4096);
stderrReader = new StreamReader(new FileStream(errSafe, FileAccess.Read, 4096, false), encoding, true, 4096);
if (ResumeThread(pi.hThread) == 0xFFFFFFFF) { throw new Win32Exception(Marshal.GetLastWin32Error()); }
}
catch
{
TerminateProcess(pi.hProcess, 1);
CloseHandle(outRead); CloseHandle(errRead);
// A reader owns its handle; a SafeFileHandle not yet in a reader
// owns its own; before either, the raw handle is closed directly.
if (stdoutReader != null) { stdoutReader.Dispose(); }
if (outSafe != null) { outSafe.Dispose(); } else { CloseHandle(outRead); }
if (stderrReader != null) { stderrReader.Dispose(); }
if (errSafe != null) { errSafe.Dispose(); } else { CloseHandle(errRead); }
CloseHandle(pi.hThread); CloseHandle(pi.hProcess);
if (proc != null) { proc.Dispose(); }
throw;
Expand All @@ -665,8 +721,8 @@ public static class PkChildProcessJob

PkStartedProcess r = new PkStartedProcess();
r.Process = proc;
r.Stdout = new StreamReader(new FileStream(new SafeFileHandle(outRead, true), FileAccess.Read, 4096, false), encoding, true, 4096);
r.Stderr = new StreamReader(new FileStream(new SafeFileHandle(errRead, true), FileAccess.Read, 4096, false), encoding, true, 4096);
r.Stdout = stdoutReader;
r.Stderr = stderrReader;
return r;
}
}
Expand All @@ -676,7 +732,7 @@ public static class PkChildProcessJob
# A session that loaded an older copy keeps its compiled types. Say so,
# rather than fail later with a missing-method error. Keep this number in
# sync with [PkState]::TypesVersion.
if ((& { try { [PkState]::TypesVersion } catch { 0 } }) -ne 5) {
if ((& { try { [PkState]::TypesVersion } catch { 0 } }) -ne 6) {
throw "ParallelKit: this PowerShell session already loaded an older version of the kit, and its compiled parts can't be swapped out. Close this window and start a new one."
}

Expand Down Expand Up @@ -855,13 +911,20 @@ function Initialize-ParallelKit {
# 0 (the default) sizes it for up to about 48.
# .OUTPUTS
# An object with AbortHandler, ChildProcessJob and ThreadPool, each $true if that
# part worked.
# part worked; Version, the kit's version; and StartMode, how the kit starts
# programs here: CreatedInJob (inside the Job Object, so anything they start
# is in it too), AddedToJob (started, then added to it just after), or NoJob
# (no cleanup if PowerShell is killed). A program whose start can't be set up
# in the job falls back to AddedToJob, with a warning.
[CmdletBinding()]
param([int]$MaxConcurrentProcesses = 0)
$handlerOk = Register-PkAbortHandler
$jobOk = Initialize-PkChildProcessJob
$poolOk = Initialize-PkThreadPool -MaxConcurrentProcesses $MaxConcurrentProcesses
return [pscustomobject][ordered]@{ AbortHandler = $handlerOk; ChildProcessJob = $jobOk; ThreadPool = $poolOk }
# The header's Version line: the version PR changes both, and the harness
# checks they match.
$version = [version]'3.2'
return [pscustomobject][ordered]@{ AbortHandler = $handlerOk; ChildProcessJob = $jobOk; ThreadPool = $poolOk; Version = $version; StartMode = [PkChildProcessJob]::StartMode }
}

function Register-PkCancellationSource {
Expand Down Expand Up @@ -1346,8 +1409,15 @@ function Start-PkProcessCore {
# In the job from its first instruction where possible; otherwise
# Process.Start, and the job just after (a child it starts at once
# can miss it then).
$started = [PkChildProcessJob]::TryStart($psi.FileName, $psi.Arguments, $psi.WorkingDirectory, $encoding)
if ($null -eq $started) { $proc = [System.Diagnostics.Process]::Start($psi) }
$fallbackReason = $null
$started = [PkChildProcessJob]::TryStart($psi.FileName, $psi.Arguments, $psi.WorkingDirectory, $encoding, [ref]$fallbackReason)
if ($null -eq $started) {
# Once per process: the cost is the same for every program after.
if ($null -ne $fallbackReason -and [PkChildProcessJob]::ClaimFallbackWarning()) {
Write-PkMessage ("[WARNING] Starting programs with Process.Start and adding them to the Job Object just after, because " + $fallbackReason + ". A child a program starts straight away (cmd /c tool.exe) can escape the cleanup if PowerShell is killed outright. Shown once.") -Level Warn
}
$proc = [System.Diagnostics.Process]::Start($psi)
}
} catch {
$tried = ""
if ($psi.FileName -ne $FilePath) { $tried = " (" + $psi.FileName + ")" }
Expand Down
Loading
Loading