from shutil import copy from tempfile import mkstemp from os import sysconf, fdopen, truncate, close, unlink, sendfile page_size = sysconf('SC_PAGESIZE') # this gives a file size equal to the default block size used in shutil, but # any other multiple of page_size also triggers the bug for me file_size = page_size * 2048 from_fd, from_name = mkstemp(dir='.') to_fd, to_name = mkstemp(dir='.') ff = fdopen(from_fd) tf = fdopen(to_fd) truncate(from_fd, file_size) # Demonstrate that this is not a problem with sendfile, but with how we're # calling it. try: sent = 0 while (sent < file_size): sent += sendfile(to_fd, from_fd, 0, file_size) except Exception as e: print('Plain sendfile failed!') print(e) finally: close(from_fd) close(to_fd) # Demonstrate error in shutil try: copy(from_name, to_name) except Exception as e: print('Shutil Failed!') print(e) finally: unlink(from_name) unlink(to_name)