"Publish-AzWebApp: One or more errors occurred. (A task was canceled.)" to regions far away from me

Emil Valbak Hermansen 0 Reputation points
2023-04-05T14:40:06.21+00:00

Environment:
PS version running via c# code: 7.1.3
Az module versions tried: v6.4.0 and v9.6.0
OS: Win 10 Pro

I have a .NET 6 project running PowerShell scripts via the Microsoft.PowerShell.SDK package. In my script, I'm attempting to deploy 2 web apps, and publish the contents via zip files. This works as expected when publishing to regions close to me (eg. West Europe and North Europe), but fails when trying to do it to regions further away from me (West US, East US, Southeast Asia). For the failing regions, it is always the second publish which fails. The zip file size of the failing publish is 447MB. The only difference between the two Publish-AzWebApp commands is the target App Service and zip file.

Publishing the zip file (28,8MB) from the first Publish-AzWebApp command to both publish commands works fine, so I suspect it is some sort of timeout issue when trying to publish the 447MB zip file?

In a StackOverflow post the fix was to enable client affinity, but I already have that enabled on newly created web apps (https://stackoverflow.com/questions/57855306/publish-azwebapp-throwing-an-aggregate-exception-need-to-understand-why-how-to)

I've also attempted to increase the default timeout for Invoke-WebRequest to 600 seconds to see if it helped. It still fails before that:

$PSDefaultParameterValues.Add("*:TimeoutSec", 600)

The error I get via powershell is "Publish-AzWebApp: One or more errors occurred. (A task was canceled.)". From the app service log stream I get the following error and stackTrace:

Error occurred, type: error, text: The client disconnected., stackTrace:
at System.Web.Hosting.IIS7WorkerRequest.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.Web.HttpBufferlessInputStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
at System.IO.Stream.<>c.
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,140 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,004 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,351 Reputation points Microsoft Employee
    2023-04-12T22:29:04.3+00:00

    It seems that the issue is related to the size of the zip file and the distance between your location and the target region. The error message indicates that the client disconnected, which could be caused by a timeout issue. One possible solution is to split the large zip file into smaller chunks and upload them separately. Another solution is to use a different deployment method, such as FTP or Git. You can also try increasing the timeout value for the Publish-AzWebApp command by adding the -TimeoutSec parameter with a higher value, such as 1200 (20 minutes). You can split the large zip file into smaller chunks using the Split-File cmdlet in PowerShell. Here's an example:

    $sourceFile = "C:\path\to\large\file.zip"
    $chunkSize = 100MB
    $destinationFolder = "C:\path\to\output\folder"
    
    $index = 0
    $buffer = New-Object byte[] $chunkSize
    
    $stream = New-Object System.IO.FileStream($sourceFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
    
    while (($read = $stream.Read($buffer, 0, $chunkSize)) -gt 0) {
        $chunkFile = Join-Path $destinationFolder ("chunk{0}.zip" -f $index++)
        $fs = New-Object System.IO.FileStream($chunkFile, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
        $fs.Write($buffer, 0, $read)
        $fs.Close()
    }
    
    $stream.Close()
    
    

    This script reads the large zip file in chunks of 100MB and saves each chunk as a separate zip file in the specified output folder. You can adjust the chunk size and output folder as needed. Once you have split the large zip file into smaller chunks, you can upload them separately using the Publish-AzWebApp command.

    0 comments No comments