| 1 |
# (c) 2025 Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
using namespace Microsoft.Win32.SafeHandles |
| 5 |
using namespace System.Collections |
| 6 |
using namespace System.IO |
| 7 |
using namespace System.Management.Automation |
| 8 |
using namespace System.Text |
| 9 |
using namespace System.Threading |
| 10 |
|
| 11 |
[CmdletBinding()] |
| 12 |
param ( |
| 13 |
[Parameter(Mandatory)] |
| 14 |
[string] |
| 15 |
$ResultPath, |
| 16 |
|
| 17 |
[Parameter(Mandatory)] |
| 18 |
[int] |
| 19 |
$Timeout, |
| 20 |
|
| 21 |
[Parameter(Mandatory)] |
| 22 |
[Int64] |
| 23 |
$WaitHandleId |
| 24 |
) |
| 25 |
|
| 26 |
if (-not (Test-Path -LiteralPath $ResultPath)) { |
| 27 |
throw "async result file at '$ResultPath' does not exist" |
| 28 |
} |
| 29 |
$rawResult = Get-Content -LiteralPath $ResultPath | ConvertFrom-Json |
| 30 |
|
| 31 |
# We ensure the outer PSObject is a hashtable to make it easier to add/merge |
| 32 |
# additional keys. |
| 33 |
$result = @{} |
| 34 |
foreach ($prop in $rawResult.PSObject.Properties) { |
| 35 |
$result[$prop.Name] = $prop.Value |
| 36 |
} |
| 37 |
|
| 38 |
# The intermediate script is used so that things are set up like it normally |
| 39 |
# is. The new Runspace is used to ensure we can stop it once the async time is |
| 40 |
# exceeded. |
| 41 |
$execInfo = Get-AnsibleExecWrapper -ManifestAsParam -IncludeScriptBlock |
| 42 |
$ps = [PowerShell]::Create() |
| 43 |
$null = $ps.AddScript(@' |
| 44 |
[CmdletBinding()] |
| 45 |
param([ScriptBlock]$ScriptBlock, $Param) |
| 46 |
|
| 47 |
& $ScriptBlock.Ast.GetScriptBlock() @Param |
| 48 |
'@).AddParameters( |
| 49 |
@{ |
| 50 |
ScriptBlock = $execInfo.ScriptInfo.ScriptBlock |
| 51 |
Param = $execInfo.Parameters |
| 52 |
}) |
| 53 |
|
| 54 |
# It is important we run with the invocation settings so that it has access |
| 55 |
# to the same PSHost. The pipeline input also needs to be marked as complete |
| 56 |
# so the exec_wrapper isn't waiting for input indefinitely. |
| 57 |
$pipelineInput = [PSDataCollection[object]]::new() |
| 58 |
$pipelineInput.Complete() |
| 59 |
$invocationSettings = [PSInvocationSettings]@{ |
| 60 |
Host = $host |
| 61 |
} |
| 62 |
|
| 63 |
# Signals async_wrapper that we are ready to start the job and to stop waiting |
| 64 |
$waitHandle = [SafeWaitHandle]::new([IntPtr]$WaitHandleId, $true) |
| 65 |
$waitEvent = [ManualResetEvent]::new($false) |
| 66 |
$waitEvent.SafeWaitHandle = $waitHandle |
| 67 |
$null = $waitEvent.Set() |
| 68 |
|
| 69 |
$jobOutput = $null |
| 70 |
$jobError = $null |
| 71 |
try { |
| 72 |
$jobAsyncResult = $ps.BeginInvoke($pipelineInput, $invocationSettings, $null, $null) |
| 73 |
$jobAsyncResult.AsyncWaitHandle.WaitOne($Timeout * 1000) > $null |
| 74 |
$result.finished = $true |
| 75 |
|
| 76 |
if ($jobAsyncResult.IsCompleted) { |
| 77 |
$jobOutput = @($ps.EndInvoke($jobAsyncResult) | Out-String) -join "`n" |
| 78 |
$jobError = $ps.Streams.Error |
| 79 |
|
| 80 |
# write success/output/error to result object |
| 81 |
$moduleResultJson = $jobOutput |
| 82 |
$startJsonChar = $moduleResultJson.IndexOf([char]'{') |
| 83 |
if ($startJsonChar -eq -1) { |
| 84 |
throw "No start of json char found in module result" |
| 85 |
} |
| 86 |
$moduleResultJson = $moduleResultJson.Substring($startJsonChar) |
| 87 |
|
| 88 |
$endJsonChar = $moduleResultJson.LastIndexOf([char]'}') |
| 89 |
if ($endJsonChar -eq -1) { |
| 90 |
throw "No end of json char found in module result" |
| 91 |
} |
| 92 |
|
| 93 |
$trailingJunk = $moduleResultJson.Substring($endJsonChar + 1).Trim() |
| 94 |
$moduleResultJson = $moduleResultJson.Substring(0, $endJsonChar + 1) |
| 95 |
$moduleResult = $moduleResultJson | ConvertFrom-Json |
| 96 |
# TODO: check for conflicting keys |
| 97 |
foreach ($prop in $moduleResult.PSObject.Properties) { |
| 98 |
$result[$prop.Name] = $prop.Value |
| 99 |
} |
| 100 |
|
| 101 |
if ($trailingJunk) { |
| 102 |
if (-not $result.warnings) { |
| 103 |
$result.warnings = @() |
| 104 |
} |
| 105 |
$result.warnings += "Module invocation had junk after the JSON data: $trailingJunk" |
| 106 |
} |
| 107 |
} |
| 108 |
else { |
| 109 |
# We can't call Stop() as pwsh won't respond if it is busy calling a .NET |
| 110 |
# method. The process end will shut everything down instead. |
| 111 |
$ps.BeginStop($null, $null) > $null |
| 112 |
|
| 113 |
throw "timed out waiting for module completion" |
| 114 |
} |
| 115 |
} |
| 116 |
catch { |
| 117 |
$exception = @( |
| 118 |
"$_" |
| 119 |
"$($_.InvocationInfo.PositionMessage)" |
| 120 |
"+ CategoryInfo : $($_.CategoryInfo)" |
| 121 |
"+ FullyQualifiedErrorId : $($_.FullyQualifiedErrorId)" |
| 122 |
"" |
| 123 |
"ScriptStackTrace:" |
| 124 |
"$($_.ScriptStackTrace)" |
| 125 |
|
| 126 |
if ($_.Exception.StackTrace) { |
| 127 |
"$($_.Exception.StackTrace)" |
| 128 |
} |
| 129 |
) -join ([Environment]::NewLine) |
| 130 |
|
| 131 |
$result.exception = $exception |
| 132 |
$result.failed = $true |
| 133 |
$result.msg = "failure during async watchdog: $_" |
| 134 |
# return output back, if available, to Ansible to help with debugging errors |
| 135 |
$result.stdout = $jobOutput |
| 136 |
$result.stderr = $jobError | Out-String |
| 137 |
} |
| 138 |
finally { |
| 139 |
$resultJson = ConvertTo-Json -InputObject $result -Depth 99 -Compress |
| 140 |
Set-Content -LiteralPath $ResultPath -Value $resultJson -Encoding UTF8 |
| 141 |
} |