Skip to content

gh-87909: avoid a 0 bytes copy call to sendfile() #101231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
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.
Loading