#4393·gofr

FTP file store: concurrent or overlapping operations desync the single control connection and hang forever

Author: NitinKumar004Created Sep 27, 2026Updated Sep 27, 2026

Summary

The FTP file store keeps a single *ftp.ServerConn and uses it for every request with no serialization. jlaffaye/ftp documents that a ServerConn "only supports one in-flight data connection" and "is not safe to be called concurrently".

On top of that, Open() keeps the RETR data transfer open inside the returned file until the caller closes it. Any other FTP operation made in the meantime breaks the control channel: another request, or even the same handler calling Stat. After that, every later call on the file store blocks forever, because no command has a deadline.

Reproduction

With the in-process goftp server used in storage_adapter_test.go:

Sequential, in a single goroutine:

go
f, _ := fs.Open("a.txt")
_, err := fs.Stat("b.txt")
// err: failed to list directory "b.txt/": 227 Entering Passive Mode (127,0,0,1,...)
err = f.Close()
// err: failed to close reader: 150 Opening ASCII mode data connection for file list
fs.Stat("b.txt")
// never returns (the test hits the go test timeout)

Concurrent: 20 goroutines, each running fs.Open(name) + io.ReadAll + Close on a different file.

  • The test hangs until the go test timeout.
  • The server log shows 20 consecutive EPSV commands followed by a single LIST.
  • Every goroutine is stuck in IO wait or semacquire.

Root cause

  • pkg/gofr/datasource/file/ftp/storage_adapter.go: one shared conn *ftp.ServerConn (line 49), with no lock around commands.
  • NewReader / NewRangeReader return the live *ftp.Response, which must be closed before the next command on that connection.
  • CommonFileSystem.Open (pkg/gofr/datasource/file/common_fs.go) stores that reader in the returned CommonFile, so the transfer stays open until the user closes the file.
  • No dial or command deadline is set, so a desynced connection blocks forever instead of failing.

Impact

Any service using the FTP file store can hang every later file operation until it is restarted. It is enough to have more than one concurrent request, or a handler that holds a file open while doing another file operation.

Suggested fix

  • Serialize every command on the connection with a mutex held for the whole command.
  • For readers, either read the RETR body fully and close the response before returning, or hold the lock until the returned reader is closed.
  • Longer term, a small pool of ServerConns (one checked out per operation or open file) would allow real concurrency.
  • Use ftp.DialWithTimeout and per-command deadlines so a broken connection fails fast instead of hanging.

Related, but a different problem: #4367 covers the data race on publishing conn from the retry goroutine, which is fixed in #4376. That fix makes the pointer publish safe but does not serialize commands on the connection.