Get-AzureStorageContainer stalls when called from event
Cmdlet(s)
Get-AzureStorageContainer
PowerShell Version
4.0
Module Version
Azure.Storage version 2.3.0
OS Version
6.3.9600.16406
Description
Output stalls displaying task status when calling Get-AzureStorageContainer from a WPF GUI event.
Script/Steps for Reproduction
Execute Get-StorageContainer from a WPF GUI event
# vim: sw=4 ts=4 fdc=4 fdm=marker
#
# Create GUI object {{{
If ([threading.thread]::CurrentThread.GetApartmentState() -ne "STA") {
Write-Host -ForegroundColor "RED" "You must run this script in Single Threaded Apartment mode (powershell.exe -STA)."
Exit 1
}
[void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
[XML]$XAML='
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test window" Height="103" Width="276" WindowStartupLocation="CenterOwner" Topmost="True" ResizeMode="NoResize" WindowStyle="ToolWindow">
<Grid>
<TextBlock x:Name="lblPrompt" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Click button to run problem code as an event" VerticalAlignment="Top" Width="244" Height="37"/>
<Button x:Name="btnClick" Content="ClickMe" HorizontalAlignment="Left" Margin="93,38,0,0" VerticalAlignment="Top" Width="75" IsDefault="True"/>
</Grid>
</Window>
'
$xmlReader=(New-Object System.Xml.XmlNodeReader ($XAML))
$Window=[Windows.Markup.XamlReader]::Load($xmlReader)
$btnClick=$Window.FindName("btnClick")
$btnClick.Add_Click({Test-ProblemCode "button click event"})
# Create GUI object }}}
Write-Host "`n`n`n`n`n`n" # Move down console to avoid progress overlay
$User=whoami /upn
While($TRUE){
Try{
$Cred=Get-Credential -Message "Enter credentials to connect to Azure" -User $User
If([string]$Cred.Username -eq ""){
Write-Host -ForegroundColor YELLOW -BackgroundColor BLACK "Cancelled at your request"
Break
}
Write-Host "Attempting Azure login..."
$RV=Login-AzureRmAccount -Credential $Cred -ErrorAction Stop
Write-Host "Azure login OK"
Break
} Catch {
Write-Host -ForegroundColor RED -BackgroundColor BLACK ("Failed to login to Azure with account '{0}', try again or cancel to exit" -f $Cred.Username)
$User=$Cred.Username
}
}
If([string]$Cred.Username -eq ""){ exit 1 }
# Get-AzureStorageContainer test code
Function Test-ProblemCode($s)
{
Write-Host ("`nTesting problem code from {0}..." -f $s)
# Iterate subscriptions
Get-AzureRmSubscription | Foreach-Object {
Write-Host (" - Subscription: {0}" -f $_.SubscriptionID)
$RV=Select-AzureRMSubscription -SubscriptionID $_.SubscriptionID
# Iterate Storage Accounts
Get-AzureRmStorageAccount | Select-Object -First 2 -Wait | Foreach-Object {
Write-Host (" - - Storage account '{0}', resource group '{1}'" -f $_.StorageAccountName,$_.ResourceGroupName)
# Iterate containers *** At this point the process pipeline stalls ***
$_ | Get-AzureStorageContainer | Select-Object -First 2 -Wait | Foreach-Object {
Write-Host (" - - - Container '{0}'" -f $_.Name)
}
}
}
}
# Initial test from main
Test-ProblemCode "main script"
Write-Host -ForegroundColor GREEN "`nNow click the button in the window to run the same code in an event"
# Now test from event
$Window.ShowDialog() | Out-Null

@EmmaZhu Hey Emma, would you mind taking a look at this issue?
This issue can also repro on my machine, the script hang in "Get-AzureRmStorageAccount" on my machine when run Test-ProblemCode() in an event.
@markcowl, The issue seems not a storage specified issue, but can also repro with other cmdlets like:
- Get-AzureRmResourceGroup
- Get-AzureRmVM
- Get-AzureRmWebApp
Would you please help to look at the issue and see how can we investigate and fix it in the Azure Powershell framework?
@blueww as we have discussed last week, the fix has to be in for Get-AzureStorageContainer as the tasks are being awaited incorrectly under DispatcherSynchronizationContext. This hang is not in the common code for Azure PowerShell.
Already have a local fix and it works in the testing. Will update the thread when I get fix released.
Has the fix already been released? Or is there an ETA when this will happen?
I seem to be running in the same problem with the new-azurestoragecontainer command, and don't know how to proceed with my script :-(
Is there perhaps some kind of workaround?
I plan to add the fix in the next release (Jan, 2017). Besides Get-AzureStorageContainer, which else cmdlets will you called from event? We would like to better understand your scenario to give better support.
Up to now I found it happens with the following cmdlets: New-AzureStorageContainer Get-AzureStorageBlob Start-AzureStorageBlobCopy
I call these cmdlets from an event (input of new container name in the gui), and want create a new container and copy blobs to that new container
I will try to fix all the cmdlets that will hang in next release (Jan) thanks!
After continuing with my script I seem to have run in the same (kind of) problem as with the cmdlets mentioned earlier.
This time I created a function that just powers on a VM:
Function StartTestenv { Start-AzureRmVM -ResourceGroupName $RG -Name $VM }
this function is started when pressing a button in my gui, the starting of the VM is being done as I can see it being started in the Azure portal, but the Start-Azure-RmVM command never seems to finish. Whenever i run this start command from a powershell console the command finishes a couple of seconds after i run the command.
@hyonholee, Would you please help to look at the hang in Start-AzureRmVM. I have a PR to fix the hang in storage cmdlets : https://github.com/Azure/azure-powershell/pull/3299
Hey do you have an exact ETA for delivering this fix? This bug is hindering my work. Is there a workaround until you get the fix released?
The fix is still pending in PR https://github.com/Azure/azure-powershell/pull/3299. Since per @twitchax : Azure PowerShell is not thread-safe, and we should wait for a proper time to address concurrency in Azure PowerShell, instead of applying a band-aid to the issue. And @twitchax suggest building a custom MSI of Azure PowerShell 3.2.0 which addresses the issue. ( The build is not supported, since it was ad hoc for you, and there was no rigorous testing of thread-safety. )
Do you think a custom MSI (not official) is acceptable for you?
For me this would be acceptable, this way I can continue with building my script/GUI.
Thanks @blueww!
@keeshuizer, as @blueww mentioned, we understand that thread-safety is a concern, but we would prefer to do it right and make all of Azure PowerShell thread-safe. Can you email me at [email protected]? I can get an ad hoc build for you to address your issue in the meantime.
The ad hoc fix can be found here, if you're interested: https://github.com/twitchax/azure-powershell/tree/ps_3.2.0_concurrency
@twitchax Did you get my email? For some reason I had some issues with sending email at that time..
@keeshuizer, yes, I did. Sorry for the late reply.
I talked offline with @shahabhijeet, and it seems that, while there are concurrency issues in PowerShell, the root cause of this problem is not the typical concurrency issues that we see. I am recommending that we take the fix for this issue, and I have approved #3299.
Good news. Thank-you team for your continued support.
The fix for storage cmdlets is already merged in. Will close the issue when the new PowerShell release come out.
Please open new issues if find other none storage cmdlets hang in event .
@blueww I'm using an ad-hoc build which included your fixes (that i got from twitchax), but I just ran into a problem with another cmdlet, the cmdlet itself isn't directly storage related, although it makes use of storage stuff i guess..
The cmdlet is 'New-AzureRmVM'.
I'm creating a new VM using an existing vhd file, when i run the commands in a normal PowerShell session the New-AzureRmVM exits after a minute or so with the following:
PS N:> New-AzureRmVM -ResourceGroupName $SelectedTestEnv -Location $location -VM $vm -Verbose VERBOSE: Performing the operation "New" on target "app-keten01".
RequestId IsSuccessStatusCode StatusCode ReasonPhrase
True OK OK
When i run the exact same code from my PowerShell GUI the creation of the VM is initiated (and finishes according to the Azure portal), but the command never finishes, it just hangs after the 'Performing the operation 'New' line.
Do you want me to open a new issue about this cmdlet?
@keeshuizer , would you please open a new issue for the hang issue of New-AzureRmVM? We take this issue to fix the storage cmdlets. And if you open a new issue, it will be assign to proper team to take care.
Thanks!