Skip to content

Commit

Permalink
Fix race condition in hostsetup.ps1.
Browse files Browse the repository at this point in the history
There was a race condition in setting up credential port forwarding in which the adapter has multiple addresses before the new ip is assigned. Allowing this race condition can cause the netsh interface portproxy setup to result in the network adapter in an unrecoverable bad state where the proxy is unable to listen on port 80 because the port is in use by a non-functional proxy rule.

This race condition happened rarely before, but started becoming a problem when changes were made to let windows test run faster (aws#1886), which let TestV3TaskEndpointDefaultNetworkMode, TestV3TaskEndpointTags and TestV3TaskEndpointDefaultNetworkMode have a high failure rate because they need the credential port forwarding.
  • Loading branch information
fenxiong committed Mar 2, 2019
1 parent e80ccb8 commit e2ea764
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion misc/windows-deploy/hostsetup.ps1
Expand Up @@ -35,8 +35,39 @@ $dockerSubnet = (docker network inspect nat | ConvertFrom-Json).IPAM.Config.Subn
$ip = (Get-NetRoute -InterfaceIndex $ifIndex -DestinationPrefix $dockerSubnet)
if(!($ip)) {

$IPAddrParams = @{
InterfaceIndex = $ifIndex;
IPAddress = $credentialAddress;
PrefixLength = 32;
}

# This command tells the APIPA interface that this IP exists.
New-NetIPAddress -InterfaceIndex $ifIndex -IPAddress $credentialAddress -PrefixLength 32
New-NetIPAddress @IPAddrParams

[int]$delay = 1
[int]$maxTries = 10
[bool]$available = $false
while (-not $available -and $maxTries -ge 0) {
try {
$IPAddr = Get-NetIPAddress @IPAddrParams -ErrorAction:Ignore
if ($IPAddr) {
if ($($IPAddr.AddressState) -eq "Preferred") {
$available = $true
break;
} else {
Start-Sleep -Seconds $delay
$maxTries--
}
}
} catch {
# Prevent race condition where the adapter has multiple addresses before our new ip is assigned
# Note: Allowing this race condition can cause the netsh interface portproxy setup to result
# in the network adapter in an unrecoverable bad state where the proxy is unable to
# listen on port 80 because the port is in use by a non-functional proxy rule.
Start-Sleep -Seconds $delay
$maxTries--
}
}

# Enable the default docker IP range to be routable by the APIPA interface.
New-NetRoute -DestinationPrefix $dockerSubnet -ifIndex $ifindex
Expand Down

0 comments on commit e2ea764

Please sign in to comment.