Skip to content

Invoke-webrequest unable to download large files - stream too long #15438

Closed

Description

Hi,

This is half a feature request/bug.

When downloading very large files (> 1GB), Invoke-Webrequest will:

  • usually crash with a stream too long exception,
  • consume huge amount of memory, going beyound 2.5GB or RAM used.

Which make this commandlet unsuited for something that would work flawlessly on wget.

Do you have any plans on improving this? How complex would this feature be?

Activity

  1. added
    Needs-Triage
    on May 22, 2021
  2. added
    WG-Engine-Performance
    on May 22, 2021
  3. iSazonov commented on May 22, 2021

    @iSazonov
    Collaborator

    What is GN?

  4. jborean93 commented on May 23, 2021

    @jborean93
    Collaborator

    Probably just a typo for gigabyte, but what PowerShell version are you on, IIRC older ones suffered from bad performance due to inefficiencies with the progress stream but that should be less of a problem today.

  5. vexx32 commented on May 23, 2021

    @vexx32
    Collaborator

    This sounds like more of an issue with the underlying implementation, it's hitting the built-in limits of whatever type of Stream it's using.

    It might be necessary to download files to a temp location so we can chunk the download data as it's received and avoid storing the entirety of a very large file into a single Stream object.

  6. daxian-dbw commented on May 25, 2021

    @daxian-dbw
    Member

    jdescelliers (@jdescelliers) What version of PowerShell do you observe this issue? I believe the large file download issues was fixed by Ilya (@iSazonov)'s PR #11095, which replaced MemoryStream with async streams. The fix was available in 7.1. Can you try on 7.1 and see if you observe the same?

  7. rjmholt commented on May 25, 2021

    @rjmholt
    Collaborator

    jdescelliers (@jdescelliers) it would be useful to know:

    • What platform and version of PowerShell you're experiencing this with ($PSVersionTable will tell you this)
    • What the exception and the stack trace of that exception are (Get-Error will tell you that in PS 7)

    I'm currently testing this out with PS 7.2-preview.5 on my Windows 10 machine by downloading a 10 GB file and will report back on what happens.

  8. added
    Waiting on Author
    on May 25, 2021
  9. rjmholt commented on May 26, 2021

    @rjmholt
    Collaborator

    So after running this yesterday, iwr ran for a few hours. I didn't compare it to, say, a browser, so I'm not sure if it was the site or the cmdlet.

    But it didn't throw for me.

  10. iSazonov commented on May 26, 2021

    @iSazonov
    Collaborator

    jdescelliers (@jdescelliers) Please share $PSVersionTable.

  11. rjmholt commented on May 26, 2021

    @rjmholt
    Collaborator

    And the output of Get-Error if there is an error

  12. jdescelliers commented on Jun 2, 2021

    @jdescelliers
    Author

    So a bit of context:

    • Windows terminal version: 1.8.1444.0
    • %PSVersionTable output:
    Name                           Value
    ----                           -----
    PSVersion                      7.1.3
    PSEdition                      Core
    GitCommitId                    7.1.3
    OS                             Microsoft Windows 10.0.19043
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
    • file downloaded: Ubuntu ISO - I needed a large file to test this, so the command is:
    Invoke-WebRequest -Uri https://releases.ubuntu.com/20.04.2.0/ubuntu-20.04.2.0-desktop-amd64.iso -Method Get
    • Error: Invoke-WebRequest: Stream was too long.
    • `Get-Error output:
    Exception             :
        Type       : System.IO.IOException
        TargetSite :
            Name          : Write
            DeclaringType : System.IO.MemoryStream
            MemberType    : Method
            Module        : System.Private.CoreLib.dll
        StackTrace :
       at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at Microsoft.PowerShell.Commands.WebResponseContentMemoryStream.Initialize()
       at Microsoft.PowerShell.Commands.WebResponseContentMemoryStream.ToArray()
       at Microsoft.PowerShell.Commands.WebResponseObject.InitializeContent()
       at Microsoft.PowerShell.Commands.WebResponseObjectFactory.GetResponseObject(HttpResponseMessage response, Stream
    responseStream, ExecutionContext executionContext)
       at Microsoft.PowerShell.Commands.InvokeWebRequestCommand.ProcessResponse(HttpResponseMessage response)
       at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
       at System.Management.Automation.CommandProcessor.ProcessRecord()
        Message    : Stream was too long.
        Source     : System.Private.CoreLib
        HResult    : -2146232800
    CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], IOException
    FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    InvocationInfo        :
        MyCommand        : Invoke-WebRequest
        ScriptLineNumber : 1
        OffsetInLine     : 1
        HistoryId        : 2
        Line             : Invoke-WebRequest -Uri https://releases.ubuntu.com/20.04.2.0/ubuntu-20.04.2.0-desktop-amd64.iso
    -Method Get
        PositionMessage  : At line:1 char:1
                           + Invoke-WebRequest -Uri https://releases.ubuntu.com/20.04.2.0/ubuntu-2 …
                           + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        InvocationName   : Invoke-WebRequest
        CommandOrigin    : Internal
    ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    • Memory usage of pwsh.exe when the exeption was raised: 2.1GB which isn't released as of writing this comment.
    • Theorical connection speed 1GB / Ethernet used.

    On the same URL, wget 1.20.3 uses roughtly 30MB of RAM for 30MB/s download speed.

  13. jborean93 commented on Jun 2, 2021

    @jborean93
    Collaborator

    You need to specify -OutFile to store the data into a file and bypass this problem. The default behaviour is to store the response in memory and that has limits as you've encountered.

  14. jdescelliers commented on Jun 5, 2021

    @jdescelliers
    Author

    Yes. It works when using -OutFile, there are still multiple issues that may need to be treated separately:

    • High memory usage: on a 2nd attempt, download peaked around 30MB/s and memory usage was steady at 96MB so more than 3 times the needed RAM: is it related to how .NET Core allocates memory in large chunks?
    • that memory is not released back to the OS when the download is complete,
    • I didn't see any mention in the documentation of Invoke-Webrequest that the response body would be stored in memory unless ``-OutFile` is provided,
    • shouldn't -Outfile be able to be "smarter" by using the filename that may be included in the response Content-Disposition: attachment header?
  15. iSazonov commented on Jun 6, 2021

    @iSazonov
    Collaborator
    • shouldn't -Outfile be able to be "smarter" by using the filename that may be included in the response Content-Disposition: attachment header?

    Perhaps we already have such issue.

    I didn't see any mention in the documentation of Invoke-Webrequest that the response body would be stored in memory unless ``-OutFile` is provided,

    No need explicitly document this because it is how pipeline works - we should accumulate data before sending it to pipeline.

    that memory is not released back to the OS when the download is complete,

    I guess .Net Core GC is too lazy.

    High memory usage

    Maybe we should see if we can do better.

  16. ghost closed this as completedon Jan 8, 2022
  17. added
    Resolution-Answered
    and removed
    Waiting on Author
    on Jan 10, 2022
  18. daxian-dbw commented on Jan 10, 2022

    @daxian-dbw
    Member

    The original question was answered. For discussion on further improvements, please search for existing related issues or create new issue if no related issue is found.

  19. orcinus commented on May 2, 2023

    @orcinus

    This is still a problem in 2023.
    PowerShell is a ridiculous joke at this point.

  20. xantari commented on Dec 26, 2023

    @xantari

    -OutFile still doesn't respect the content-disposition file name :(

  21. ku4eto commented on Sep 9, 2024

    @ku4eto

    This is still an issue in 7.4.5.
    If Invoke-WebRequest is part of a function, the downloaded content is still going into the memory before being outputed into a file.
    Example is with an alias of how Linux wget should act on Windows:

    function wget_bash
    {
    	param
        (
            [parameter(Mandatory=$true)]$Uri,
            [parameter(Mandatory=$false)]$OutFile="."
        )
    	Invoke-WebRequest -Uri $Uri -OutFile $OutFile -HttpVersion 2.0 -PassThru
    }
    New-Alias -Name "wget" -Value wget_bash -Force -Scope Global -Option AllScope -PassThru
    

    If you try to pull with wget <somefile>, it will pull it first into the memory, before saving it as a file. Thus, you will eventually run with bigger files into Stream was too long.

  22. rwalle commented on Jan 13, 2026

    @rwalle

    This is still an issue in 7.4.5. If Invoke-WebRequest is part of a function, the downloaded content is still going into the memory before being outputed into a file. Example is with an alias of how Linux wget should act on Windows:

    function wget_bash
    {
    	param
        (
            [parameter(Mandatory=$true)]$Uri,
            [parameter(Mandatory=$false)]$OutFile="."
        )
    	Invoke-WebRequest -Uri $Uri -OutFile $OutFile -HttpVersion 2.0 -PassThru
    }
    New-Alias -Name "wget" -Value wget_bash -Force -Scope Global -Option AllScope -PassThru
    

    If you try to pull with wget <somefile>, it will pull it first into the memory, before saving it as a file. Thus, you will eventually run with bigger files into Stream was too long.

    I just ran into this when fetching a 2GB file. My machine has plenty of free memory, much larger than 2GB, yet wget failed due to "stream was too long". This is very strange.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageResolution-AnsweredWG-CmdletsWG-Engine-Performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions