Skip to content

Incomplete downloads #424

Open
Open

Description

@mrwonko

Since upgrading to 1.13 the library will often report EOF before a file opened with Client.Open() is downloaded completely. For example a 634618 byte file has stopped downloading after just 65536 bytes, and a 2367599 byte file after 2031616.

As a workaround I've added a call to File.Stat().Size(), which returns the correct file size, and compare that with the downloaded size to check for success.

Activity

  1. puellanivis commented on Mar 29, 2021

    @puellanivis
    Collaborator

    Queries: Is it returning an io.EOF error, or a nil error? How are you transferring the file? Client.Open() then a io.Copy?

  2. mrwonko commented on Mar 29, 2021

    @mrwonko
    Author

    Yes, Client.Open() followed by io.Copy(), which returns a nil error, implying that the reader itself returned io.EOF.

  3. puellanivis commented on Mar 29, 2021

    @puellanivis
    Collaborator

    Hm… this is proving pretty hard to pin down what might be going wrong with WriteTo. I see one path that could result in an err == nil case without an io.EOF but no one closes any of those channels anyways, so it seems unlikely to be that, and I don’t really see how we could be getting an io.EOF from anything other than an SSH_FXP_STATUS…

    Clearly some number of following bytes are not being committed, this can be clearly seen, because the two mistaken file size examples are at 32768 byte boundaries…

  4. mrwonko commented on Mar 29, 2021

    @mrwonko
    Author

    the two mistaken file size examples are at 32768 byte boundaries

    Sounds right, other examples like 1343488, 1638400 and 786432 also fulfil that.

  5. puellanivis commented on Mar 29, 2021

    @puellanivis
    Collaborator

    I’m thinking we might want to try a cherry pick that covers the unexpected channel close case. I’m entirely unsure how that could ever trigger, but it might be the problem, as that is the only other way we can get a nil error without an EOF, and like, it very clearly shouldn’t be a nil error… it was just like that as a hold-over from an earlier draft.

    This could also explain the observed behavior, some number of 32 KiB writes work, then we receive on a closed channel, and stop all transfers.

  6. drakkan commented on Mar 30, 2021

    @drakkan
    Collaborator

    @puellanivis thank you. So if I want try to replicate this issue it should happen only for unexpected transfer errors, right?

  7. puellanivis commented on Mar 30, 2021

    @puellanivis
    Collaborator

    @drakkan That’s the thing, this really shouldn’t happen for unexpected transfer errors either. Those should all percolate some non-EOF error up the line as well? 🤔

  8. drakkan commented on Apr 2, 2021

    @drakkan
    Collaborator

    I tryed to replicate this issue with no luck using something like this:

      for i := 0; i < 1000; i++ {
    		start := time.Now()
    		f, err := sftpClient.Open("file.zip")
    		if err != nil {
    			panic(err)
    		}
    		dst, err := os.Create("/tmp/file.zip")
    		if err != nil {
    			panic(err)
    		}
    		n, err := io.Copy(dst, f)
    		fmt.Printf("it %v written %v, err %v elapsed %v\n", i, n, err, time.Since(start))
    		if n != 61449068 {
    			fmt.Printf("errrrrrrrrrrrrrrrrrrrrrr\n\n\n\n")
    		}
    		f.Close()
    		dst.Close()
    	}
    

    @mrwonko, can you please confirm that #425 fixes this issue in your case?

    @puellanivis I tryed this over a VPN connection, to trigger the error I tryed to disconnect the VPN, in this case more than 15 minutes are needed before the connection lost error is triggered

    it 0 written 2654208, err connection lost elapsed 17m10.208717111s
    

    what do you think about? Should we add a context or such? thank you

  9. mrwonko commented on Apr 7, 2021

    @mrwonko
    Author

    I will give #425 a try next week and let you know if it helps.

  10. mrwonko commented on Apr 12, 2021

    @mrwonko
    Author

    I've deployed #425 on our preproduction system a couple of hours ago and there was already the first partial download, 98304 bytes instead of 240430. I'll keep it there until tomorrow to see if it happens again, but then I'll probably roll back, it doesn't seem to work.

  11. mrwonko commented on Apr 13, 2021

    @mrwonko
    Author

    There have been about a dozen failed downloads at this point, #425 doesn't seem to fix our issue.

  12. drakkan commented on Apr 13, 2021

    @drakkan
    Collaborator

    @mrwonko can you please provide a reproducer? I tryed the above code with no luck.

    I'm not very familiar with client side code but if I have an easy way to reproduce the issue I'll try to fix it, thank you

  13. puellanivis commented on Apr 13, 2021

    @puellanivis
    Collaborator

    The only think I can think of now is the possibility that there are writes and reads happening to the remote file at the same time. So, when the read tries to read a value at offset XYZ, it’s met with an io.EOF and so it assumes the read is complete. Since the client can really only receive either data, or an io.EOF that means that setup will only ever write a 32k. 🤔 This still seems so weird though.

  14. puellanivis commented on Apr 13, 2021

    @puellanivis
    Collaborator

    Going through with a fine-toothed comb again, I’m seeing numerous small issues here and there. I’ll be making a PR to address them, and ensure that sendPacket does not ever return an io.EOF error, and the readChunkAt refuses to recognize an err == io.EOF from sendPacket as well.

    Since proper EOFs in the SFTP library should only ever come via an SSH_FX_EOF status packet, and never raw from the sendPacket.

  15. drakkan commented on Apr 13, 2021

    @drakkan
    Collaborator

    @puellanivis thank for your patch

    @mrwonko can you please a give a try to #429?

  16. 43 remaining items

  17. gnvk commented on Jun 2, 2025

    @gnvk

    The issue happened again and this is all I see in the logs:

    recv packet error: 9 of 32777 bytes: 67000030db00008000
    

    and then the Wait() error:

    error reading packet body: 9 of 32777: (SSH_FXP_DATA) unexpected EOF
    

    We retry five times, it failed all 5 times with the exact same logs.

    I hope there's nothing sensitive in this.

  18. puellanivis commented on Jun 2, 2025

    @puellanivis
    Collaborator

    OK, so, the packet there is:

    Packet{
      Type: SSH_FXP_DATA,
      ID:   0x30db,
      Data: string{
        Len:  32768,
        Data: nil,
      },
    }
    

    So, nothing sensitive, because it’s entirely empty. 😂 The packet length comes out as 9+32768, so nothing about the metadata is particularly “wrong”, we’re just missing the entire data payload. 🤔

    What code is the SFTP server running? The openssh implementation? Or ours?

  19. gnvk commented on Jun 3, 2025

    @gnvk

    The files that we're trying to download are definitely not empty. First, we do a client.Walk() and we log every Path() and Stat().Size(). The files are always a couple hundred megabytes.

    What code is the SFTP server running? The openssh implementation? Or ours?

    It's a third-party server, we only have read access to it. Using this method it says

    $ telnet <server_address> 22
    ...
    SSH-2.0-AzureSSH_1.0.0
    

    and

    $ sftp <server_address>
    ...
    sftp> version
    SFTP protocol version 3
    
  20. puellanivis commented on Jun 3, 2025

    @puellanivis
    Collaborator

    The files that we're trying to download are definitely not empty.

    Oh, don’t worry, I believe you. The packet itself says there should be 32768 bytes following as content from the file. That this isn’t transmitted is a very weird situation.

  21. puellanivis commented on Jun 11, 2025

    @puellanivis
    Collaborator
    $ telnet <server_address> 22
    ...
    SSH-2.0-AzureSSH_1.0.0
    

    🤔 So, having taken a couple of days to investigation, research and such… I don’t think that we could be doing anything different. This disconnect is definitively coming from either Azure’s side or somewhere in the networking between.

    Since AzureSSH’s source code is closed source, I cannot even look into what’s triggering this. Because of the bug above, we also cannot get the process exit code. And it’s probable that we’re not getting any stderr messages about the failure because the disconnect happens first, before any stderr messages can be sent.

    Short of getting logs or records or information from Azure’s side about why they’re breaking the connection, I’m not sure there’s any way to “fix” anything.

  22. jerbob92 commented on Nov 26, 2025

    @jerbob92

    For what it's worth, I also have this issue where io.ReadAll would result in incomplete files, while io.Copy works fine. In my case it's with CoreFTP-0.3.3. When using with an OpenSSH SFTP server I didn't encounter this issue.

    Since I have a workaround it's not that big of a deal, but let me know if I can do anything to help debug it.

    Edit: CoreFTP-0.3.3 is what is returned when opening the SSH connection, but I'm not sure if thats the actual CoreFTP version, given this topic.

  23. puellanivis commented on Dec 3, 2025

    @puellanivis
    Collaborator

    I’m curious if you’re brave enough to try out the dev-v2 branch, and see if that resolves the issue? The whole Read/Write/ReadFrom/WriteFrom code was significantly simplified, and possibly easier to debug; if the symptoms persist, anyways.

  24. jerbob92 commented on Dec 3, 2025

    @jerbob92

    I’m curious if you’re brave enough to try out the dev-v2 branch, and see if that resolves the issue? The whole Read/Write/ReadFrom/WriteFrom code was significantly simplified, and possibly easier to debug; if the symptoms persist, anyways.

    So something odd just happened.

    I was going to try dev-v2 locally to see if it fixes it, but first tried with v1 to make sure that the issue is still there. I couldn't reproduce it locally on v1...

    Then I remembered our production image runs in Alpine, so next step was to try it in Alpine, still no luck, couldn't reproduce it.

    Then I remembered that I run on MacOS locally which is arm64, while prod runs on amd64, so then I made a amd64 docker build and ran it with --platform linux/amd64, still can't reproduce it.

    So then the only thing left was trying it again on the server itself... Can't reproduce it anymore. What the hell 🤯

    Edit: so I went back to the exact same code as it was before, so what happened is that I returned the connection to the pool before the file was closed, and then it would basically open a new file on the same connection, and then it would call file.Close() on the original subroutine, causing the incomplete file on the second use of the connection.

    Changing from io.ReadAll to io.Copy didn't actually fix it, I just happened to have changed the logic so that the file is closed before it returns the connection to the pool in the same change.

    This still sounds like a bug to me, because it would mean you can't work on multiple file at the same time?

    I also tried dev-v2 now, and it seems to have the same issue.

    Edit 2: what the hell ^2, this SFTP server returns the file handle "OpenFileHandle" for any file that you open, so if you open two files, you get back the same file handle, if you then close the first handle it will actually close the handle of the second file on the server, making it close the file, making the file transfer incomplete, because it uses the same handle 👺

  25. puellanivis commented on Dec 4, 2025

    @puellanivis
    Collaborator

    😱 alright, that sounds like something we should be guarding against. Like if an open/opendir receives a file handle that it already thinks is open, then we should return an error back to the caller, and let them deal with the fact that the open “failed”… except… the old file handle was probably overwritten on the server side as well anyways… 😭

  26. jerbob92 commented on Dec 4, 2025

    @jerbob92

    Yeah you are correct that the file handle is overwritten on the server side. It would be nice if this library guards against it, so that the user at least has some idea about what's going on. On the other side I wonder if this library should have to deal with quirks of some weird SFTP server, which potentially is also a very old version of it.

  27. puellanivis commented on Dec 8, 2025

    @puellanivis
    Collaborator

    I’ve already had to make some special changes for a SFTP client, which insisted on parsing the longname despite the spec urging clients not to parse it.

    Additionally though, this would be a good guard in the first place. It’s a misbehavior that we really should guard against, I just never expected anything like it. 😂

  28. Rex-Reynolds commented on Jun 3, 2026

    @Rex-Reynolds

    Same symptom on v1.13.6

    Server: GoAnywhere MFT 7.7.1 (via ssh -v banner; third-party managed endpoint).

    Symptom: ~228KB file consistently arrived ~24KiB short, err == nil. The concurrent-read prefetcher treats the server's READ responses as premature EOF, and io.ReadAll converts that to nil. Truncation point was consistent across runs.

    Workaround: sftp.UseConcurrentReads(false) — downloads then byte-identical to an out-of-band copy. We also added a bytes-received vs Lstat size check.

    May well be a server-side spec deviation rather than a client bug — posting for visibility as this was a non-trivial triage

  29. puellanivis commented on Jun 3, 2026

    @puellanivis
    Collaborator

    What do you mean the servers READ responses are interpreted as a premature EOF? Also, I’m confused the server should not be sending any SSH_FXP_READ packets, but instead SSH_FXP_DATA packets.

    I’m assuming it’s this code here? https://github.com/pkg/sftp/blob/master/client.go#L1307-L1309

    Note that https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-02#section-6.4 reads

    ... For normal disk
    files, it is guaranteed that this will read the specified number of
    bytes, or up to end of file. For e.g. device files this may return
    fewer bytes than requested.

    One of the hardest parts about handling concurrent reads is that if a server sends a short read, like… we already sent read packets ahead of that hole… so, there would then be a missing gap in that particular read if we just kept going… I mean, we could technically track any holes, and then send requests to fill them… 🤔 I think you’re right that it’s probably a server-side spec deviation.

    I might need to include a ForceSerialization feature into v2 if this is the case… it takes a bit of a safer route to concurrent read requests, but it still makes the same assumption about short reads: https://github.com/pkg/sftp/blob/dev-v2/client.go#L2099

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions