win-async: fix race condition in async run (#43691)

pull/41729/head
Jordan Borean 6 years ago committed by GitHub
parent 08e8d8c1d5
commit 22b921d47f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1124,6 +1124,15 @@ $ErrorActionPreference = "Stop"
# return asyncresult to controller # return asyncresult to controller
$exec_wrapper = { $exec_wrapper = {
# help to debug any errors in the exec_wrapper or async_watchdog by generating
# an error log in case of a terminating error
trap {
$log_path = "$($env:TEMP)\async-exec-wrapper-$(Get-Date -Format "yyyy-MM-ddTHH-mm-ss.ffffZ")-error.txt"
$error_msg = "Error while running the async exec wrapper`r`n$_`r`n$($_.ScriptStackTrace)"
Set-Content -Path $log_path -Value $error_msg
throw $_
}
&chcp.com 65001 > $null &chcp.com 65001 > $null
$DebugPreference = "Continue" $DebugPreference = "Continue"
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
@ -1201,9 +1210,6 @@ $exec_wrapper = {
Function Run($payload) { Function Run($payload) {
$remote_tmp = $payload["module_args"]["_ansible_remote_tmp"] $remote_tmp = $payload["module_args"]["_ansible_remote_tmp"]
$remote_tmp = [System.Environment]::ExpandEnvironmentVariables($remote_tmp) $remote_tmp = [System.Environment]::ExpandEnvironmentVariables($remote_tmp)
if ($null -eq $remote_tmp) {
$remote_tmp = $original_tmp
}
# calculate the result path so we can include it in the worker payload # calculate the result path so we can include it in the worker payload
$jid = $payload.async_jid $jid = $payload.async_jid
@ -1267,6 +1273,19 @@ Function Run($payload) {
} }
$watchdog_pid = $process.ProcessId $watchdog_pid = $process.ProcessId
# populate initial results before we send the async data to avoid result race
$result = @{
started = 1;
finished = 0;
results_file = $results_path;
ansible_job_id = $local_jid;
_ansible_suppress_tmpdir_delete = $true;
ansible_async_watchdog_pid = $watchdog_pid
}
$result_json = ConvertTo-Json $result
Set-Content $results_path -Value $result_json
# wait until the client connects, throw an error if the timeout is reached # wait until the client connects, throw an error if the timeout is reached
$wait_async = $pipe.BeginWaitForConnection($null, $null) $wait_async = $pipe.BeginWaitForConnection($null, $null)
$wait_async.AsyncWaitHandle.WaitOne(5000) > $null $wait_async.AsyncWaitHandle.WaitOne(5000) > $null
@ -1283,19 +1302,6 @@ Function Run($payload) {
$pipe.Close() $pipe.Close()
} }
# populate initial results before we resume the process to avoid result race
$result = @{
started=1;
finished=0;
results_file=$results_path;
ansible_job_id=$local_jid;
_ansible_suppress_tmpdir_delete=$true;
ansible_async_watchdog_pid=$watchdog_pid
}
$result_json = ConvertTo-Json $result
Set-Content $results_path -Value $result_json
return $result_json return $result_json
} }

@ -1,21 +1,8 @@
#!powershell #!powershell
# This file is part of Ansible
# # Copyright: (c) 2018, Ansible Project
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by #Requires -Module Ansible.ModuleUtils.Legacy
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON
# POWERSHELL_COMMON
$parsed_args = Parse-Args $args $parsed_args = Parse-Args $args

@ -4,7 +4,7 @@
- name: async fire and forget - name: async fire and forget
async_test: async_test:
sleep_delay_sec: 8 sleep_delay_sec: 10
async: 20 async: 20
poll: 0 poll: 0
register: asyncresult register: asyncresult
@ -23,7 +23,7 @@
- name: async poll immediate success - name: async poll immediate success
async_test: async_test:
sleep_delay_sec: 0 sleep_delay_sec: 0
async: 20 async: 10
poll: 1 poll: 1
register: asyncresult register: asyncresult
@ -61,7 +61,7 @@
- name: async poll retry - name: async poll retry
async_test: async_test:
sleep_delay_sec: 5 sleep_delay_sec: 5
async: 20 async: 10
poll: 1 poll: 1
register: asyncresult register: asyncresult
@ -97,8 +97,8 @@
- name: async poll timeout - name: async poll timeout
async_test: async_test:
sleep_delay_sec: 25 sleep_delay_sec: 5
async: 20 async: 3
poll: 1 poll: 1
register: asyncresult register: asyncresult
ignore_errors: true ignore_errors: true
@ -152,7 +152,7 @@
- name: echo some non ascii characters - name: echo some non ascii characters
win_command: cmd.exe /c echo über den Fußgängerübergang gehen win_command: cmd.exe /c echo über den Fußgängerübergang gehen
async: 20 async: 10
poll: 1 poll: 1
register: nonascii_output register: nonascii_output

Loading…
Cancel
Save