Wednesday, September 16, 2015

Python ftplib storbinary hanging

Today I've spent several hours to find why python program is hangs sometimes.
Using gdb I've found that sometimes ftplib's storbinary hangs at the end of file transfer.

gdb has shown that it hangs at:
... in __libc_recv (fd=?, buf=?, n=8192, flags=-1) at ../sysdeps/unix/sysv/linux/x86_64/recv.c:33

I've added callback argument to storbinary to print last data block (by default blocksize is 8192):
def print_last(data):
    if data < 8192:
        print len(data)

ftp.storbinary('STOR %s' % fn, f, callback=print_last)
And after that I've found that program hangs at the end of file transfer.
I also found that EOFError happens after creating some number of ftp sessions (sockets). In my case after creating >46 (or 47) simultaneous ftp connection.

Solution was simple - to add timeout argument on ftp creation.
ftp = ftplib.FTP(, self.username, self.password, timeout=10)


  1. Dear Sir,
    I have the same problem and I am very interested to understand why a timeout can solve this problem . Can you explain why ?
    Many thanks in advance.

    1. As you can check ftplib.FTP uses socket. And when you creating ftplib.FTP without timeout argument, the socket has no timeout (as sockets are blocking by default). It means that program will block the execution until the server breaks connection or some data arrive.