Repository navigation
Closed
Description
Hello,
version of go : 1.20.5
version of lib sftp : v1.13.6
version of sftp server: OpenSSH_8.9p1 Ubuntu-3ubuntu0.6
I sometime experiment a bug when my code close 2 times the same sftp.File ref in concurrent situation
here the code to reproduce :
main.go
package main
import (
"bytes"
"fmt"
"net"
"os"
"sync"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func main() {
connSSH, errSSH := ssh.Dial("tcp", net.JoinHostPort("localhost", "22"), &ssh.ClientConfig{
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
User: "USER",
Auth: []ssh.AuthMethod{
ssh.Password("PASSWORD"),
},
})
if errSSH != nil {
panic(errSSH)
}
client, errSftp := sftp.NewClient(connSSH, sftp.UseConcurrentReads(true), sftp.UseConcurrentWrites(true), sftp.UseFstat(true))
if errSftp != nil {
panic((errSftp))
}
if errSftp != nil {
panic(errSftp.Error())
}
input := make(chan int, 1000)
for i := 1; i < 1000; i++ {
input <- i
}
wait := &sync.WaitGroup{}
for i := 1; i <= 1000; i++ {
wait.Add(1)
go func(wait *sync.WaitGroup) {
defer wait.Done()
for {
idx, ok := <-input
if !ok {
return
}
filename := fmt.Sprintf("/tmp/file-%d.dat", idx)
err := copy(client, filename, "small content")
if err != nil {
fmt.Println(err.Error())
}
}
}(wait)
}
close(input)
wait.Wait()
}
func copy(client *sftp.Client, filename string, content string) error {
file, errCreate := client.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
if errCreate != nil {
return errCreate
}
_, errCopy := file.ReadFrom(bytes.NewBufferString(content))
if errCopy != nil {
fmt.Printf("💣💣💣💣💣 COPY: %s", err.Error())
//clean it
client.Remove(filename)
return errCopy
}
defer file.Close()
//force double close
file.Close()
return nil
}
go.mod
module golang_sftp_fd_null
go 1.20
require (
github.com/pkg/sftp v1.13.6
golang.org/x/crypto v0.9.0
)
require github.com/stretchr/testify v1.8.1 // indirect
require (
github.com/kr/fs v0.1.0 // indirect
golang.org/x/sys v0.8.0 // indirect
)
When i launched this code, sometime i got 1 or 2 SSH_FX_FAILURE on the ReadFrom method .
output of my code :
💣💣💣💣💣 COPY: sftp: "Failure" (SSH_FX_FAILURE)
On server side, I can see that type of logs:
Jan 11 15:27:04 BVK-book sftp-server[185806]: debug1: request 2162: write "(null)" (handle -1) off 0 len 14
Jan 11 15:27:04 BVK-book sftp-server[185806]: debug3: request 2162: sent status 4
Jan 11 15:27:04 BVK-book sftp-server[185806]: sent status Failure
Now, if I replace
defer file.Close()
//force double close
file.Close()
with
alreadyClosed:=false
defer func(){
if !alreadyClosed{
file.Close()
}
}
//force double close
file.Close()
I cant reproduce the error.
In the Close method documentation, it is mention that Close closes the File, rendering it unusable for I/O..
Is-it really the case ?
Does a PR that add a flag check "alreadyClose" in the Close method before send a close request to sftp server is a great idea ?
thank you.
Metadata
Metadata
Assignees
Labels
No labels
Activity
puellanivis commentedon Jan 11, 2024
It is a well-known situation that calling
Closemore than once on something may result in erroneous behavior. Consider: https://go.dev/play/p/K9uveTKRNusWhich returns:
Also, note that attempting to closing an
os.Filetwice will return an errorfile already closed.Now, all of that said, we could support this a little better, by locking the
sftp.Filemutex during aClosecall and then marking it as already closed, then returningfs.ErrClosedwhen we expect that double close has occurred. Rather than trying to send a known brokenSSH_FXP_CLOSEto the server for it to respond back with anSSH_FX_FAILURE.P.S.: Note that your code was erroneous even if using
os.File, but the error produced would be simply dropped on the floor, as you’re not actually trapping it in thedefer.lindmin commentedon Jan 15, 2024
Hello,
I knew double close is not a really clean code, I was fool by other module implementation of Close where Close can be called safely multiple time.
I create the issue it because the ReadFrom error happend randomly, and it take me some time to identify that was the second close() on a already closed file reference that impacting another file reference copy process, it seems to me very sneaky.
With your solution, it will be more explicit and preventive 👍
puellanivis commentedon Jan 15, 2024
This may not be strictly true. Since you’re throwing away any errors that are happening, you cannot know whether these multiple closes are actually safe or not.
Hm, this is still kind of weird though, because the
SSH_FXP_CLOSEshouldn’t be impacting any other open file handles. 🤔And even with
UseConcurrentWrites(true)the length of data being written, isn’t enough to trigger concurrent transfers.Actually:
This is the sftp server related what the request it got was. Which appears to be:
And that’s just all sorts of wrong. There shouldn’t really be a way that we can even send that.
puellanivis commentedon Jan 15, 2024
I think what might help is more of the server logs preceding one of these errors. 🤔 While the logs are, I’m sure, relatively free of sensitive information, you should probably attach it as a file, rather than embed it. There’s almost certainly a lot of noise going on in there.
lindmin commentedon Jan 16, 2024
here the log of one run with 2 copy errors :
double_close_bug.log
puellanivis commentedon Jan 17, 2024
Interesting, found two corresponding
closerequests via no bytes being written, all others havewritten 14:From here, we can then extract the entries for
file-696.dat, and mix in where thestatus Failureoccurs:Wait a second,
handle handle 4? But that’s not right. We already used that handle. 😮 oh no. What about theopenfor 948?handle handle 5. Again, an attempt to reuse an existing handle. In fact, these got shunted all the way to the back of the line. Almost all of theopens happen first, before much if anywrites happen. But these two “bugs” in theopenrequests both happen deep into thewrites. Looking at the lowest number handles:Hm. The ones reusing a handle occur immediately after the close for that handle.
Something is very sketchy here, and it’s not immediately apparent that it would be our code.
puellanivis commentedon Jan 17, 2024
Congratulations! You have managed to stumble across a race condition in openssh!
There is actually absolutely nothing wrong with our code. It’s operating correctly, and openssh is the one which is misbehaving.
I’ll be isolating it, preparing an issue report to their repo, then will link to the issue here. (And maybe a PR if I can work out how to address it at the same time.)
lindmin commentedon Jan 17, 2024
Oh ok, thank you for your analysis on that ! 👍
puellanivis commentedon Jan 18, 2024
Hm… ordering them by line numbers, I see something different. No bug or race condition in
openssh(well, small bug, but only in the logging, but since it’s not multithreaded, this was not making sense.)Instead, it’s (obviously) a race condition in the code which is multithreaded:
Thread A: open
/tmp/file-5.dat; Response:handle 4Thread A: write
handle 4with 14 bytes; Response: OKThread A: close
handle 4; Response: OKThread B: open
/tmp/file-40.dat; Response:handle 4(reused)Thread A: close
handle 4; Response: OKThread B: write
/tmp/file-40.dat; Response: FailureThread B: close
handle 4; Response: File not foundThread B: close
handle 4; Response: File not foundSo, this is a bug in
sftp, and we’ll need to prevent the potential of a reused file handler being closed by a delayed second close on another thread of requests.puellanivis commentedon Jan 19, 2024
If you can try out the new PR, and verify that you don’t seem to hit the error anymore. I know we cannot for sure say it doesn’t happen anymore, but some amount of confidence would be great.
christhomas commentedon Oct 15, 2026
We did a small code audit of the open issues, and we think this one is already addressed.
#574 was written for this issue and released in v1.13.7.
File.Closenow clears the handle while holding the file's lock, so a secondClosereturnsos.ErrClosed("file already closed") instead of sending another SSH_FXP_CLOSE for a handle the server may already have given to a different file:sftp/client.go
Lines 1115 to 1133 in 16147c5
We checked it against current master: create a file, close it twice, and the second
Closereturnsos.ErrClosedwithout sending anything to the server. So we think this can be closed.puellanivis commentedon Oct 16, 2026
If the issue returns, someone can always reopen, or open a new ticket.