Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-87909: avoid a 0 bytes copy call to sendfile()
That triggers an EAGAIN error on a minority of kernels and filesystems.
  • Loading branch information
gpshead committed Jan 22, 2023
commit 6b07ef94d47d411ad4af06bc6b8868559324e705
8 changes: 6 additions & 2 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,20 @@ def _fastcopy_sendfile(fsrc, fdst):
# should not make any difference, also in case the file content
# changes while being copied.
try:
blocksize = max(os.fstat(infd).st_size, 2 ** 23) # min 8MiB
filesize = os.fstat(infd).st_size
blocksize = max(filesize, 2 ** 23) # min 8MiB
except OSError:
filesize = float("inf")
blocksize = 2 ** 27 # 128MiB
# On 32-bit architectures truncate to 1GiB to avoid OverflowError,
# see bpo-38319.
if sys.maxsize < 2 ** 32:
blocksize = min(blocksize, 2 ** 30)

offset = 0
while True:
# We explicitly limit ourselves to filesize to avoid odd behavior
# on some systems. https://github.com/python/cpython/issues/87909
while offset < filesize:
try:
sent = os.sendfile(outfd, infd, offset, blocksize)
except OSError as err:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Avoid an unusual case in :func:`shutil.copyfile` where the ``sendfile``
system call might return an error on some kernels and filesystems after all
data was transferred.