You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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…
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.
@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? 🤔
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
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.
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.
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.
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?
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
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.
🤔 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.
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.
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.
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 👺
😱 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… 😭
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.
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. 😂
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
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.
... 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.
Activity
puellanivis commentedon Mar 29, 2021
Queries: Is it returning an
io.EOFerror, or a nil error? How are you transferring the file?Client.Open()then aio.Copy?mrwonko commentedon Mar 29, 2021
Yes,
Client.Open()followed byio.Copy(), which returns a nil error, implying that the reader itself returnedio.EOF.puellanivis commentedon Mar 29, 2021
Hm… this is proving pretty hard to pin down what might be going wrong with
WriteTo. I see one path that could result in anerr == nilcase without anio.EOFbut 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 anio.EOFfrom anything other than anSSH_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…
mrwonko commentedon Mar 29, 2021
Sounds right, other examples like 1343488, 1638400 and 786432 also fulfil that.
puellanivis commentedon Mar 29, 2021
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
nilerror without an EOF, and like, it very clearly shouldn’t be anilerror… 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.
drakkan commentedon Mar 30, 2021
@puellanivis thank you. So if I want try to replicate this issue it should happen only for unexpected transfer errors, right?
puellanivis commentedon Mar 30, 2021
@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? 🤔
drakkan commentedon Apr 2, 2021
I tryed to replicate this issue with no luck using something like this:
@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
what do you think about? Should we add a context or such? thank you
mrwonko commentedon Apr 7, 2021
I will give #425 a try next week and let you know if it helps.
mrwonko commentedon Apr 12, 2021
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.
mrwonko commentedon Apr 13, 2021
There have been about a dozen failed downloads at this point, #425 doesn't seem to fix our issue.
drakkan commentedon Apr 13, 2021
@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
puellanivis commentedon Apr 13, 2021
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.EOFand so it assumes the read is complete. Since the client can really only receive either data, or anio.EOFthat means that setup will only ever write a 32k. 🤔 This still seems so weird though.puellanivis commentedon Apr 13, 2021
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
sendPacketdoes not ever return anio.EOFerror, and thereadChunkAtrefuses to recognize anerr == io.EOFfrom sendPacket as well.Since proper EOFs in the SFTP library should only ever come via an
SSH_FX_EOFstatus packet, and never raw from thesendPacket.drakkan commentedon Apr 13, 2021
@puellanivis thank for your patch
@mrwonko can you please a give a try to #429?
43 remaining items
gnvk commentedon Jun 2, 2025
The issue happened again and this is all I see in the logs:
and then the
Wait()error:We retry five times, it failed all 5 times with the exact same logs.
I hope there's nothing sensitive in this.
puellanivis commentedon Jun 2, 2025
OK, so, the packet there is:
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?
gnvk commentedon Jun 3, 2025
The files that we're trying to download are definitely not empty. First, we do a
client.Walk()and we log everyPath()andStat().Size(). The files are always a couple hundred megabytes.It's a third-party server, we only have read access to it. Using this method it says
and
puellanivis commentedon Jun 3, 2025
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.
puellanivis commentedon Jun 11, 2025
🤔 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.
jerbob92 commentedon Nov 26, 2025
For what it's worth, I also have this issue where
io.ReadAllwould result in incomplete files, whileio.Copyworks fine. In my case it's withCoreFTP-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.3is what is returned when opening the SSH connection, but I'm not sure if thats the actual CoreFTP version, given this topic.puellanivis commentedon Dec 3, 2025
I’m curious if you’re brave enough to try out the
dev-v2branch, 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.jerbob92 commentedon Dec 3, 2025
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 👺
puellanivis commentedon Dec 4, 2025
😱 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… 😭
jerbob92 commentedon Dec 4, 2025
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.
puellanivis commentedon Dec 8, 2025
I’ve already had to make some special changes for a SFTP client, which insisted on parsing the
longnamedespite 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. 😂
Rex-Reynolds commentedon Jun 3, 2026
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.ReadAllconverts 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
puellanivis commentedon Jun 3, 2026
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_READpackets, but insteadSSH_FXP_DATApackets.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
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
ForceSerializationfeature intov2if 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#L2099return an error when the server reuses an open handle
Detect duplicate handles and support single-file clients