Skip to content

os.sendfile() skips file data on macOS with headers and count=0 #162378

Open

Description

@maurycy

Bug report

On macOS, passing a nonempty header to os.sendfile() with count=0 sends just the header.

But the documented behavior is to send the file from offset to EOF:

On macOS and FreeBSD, a value of 0 for count specifies to send until the end of in_fd is reached.

https://docs.python.org/3/library/os.html#os.sendfile

import os
import socket
import tempfile

tx, rx = socket.socketpair()
with tempfile.TemporaryFile() as f, tx, rx, rx.makefile("rb") as reader:
    f.write(b"abc")
    f.flush()
    sent = os.sendfile(tx.fileno(), f.fileno(), 1, 0, headers=[b"H"])
    tx.shutdown(socket.SHUT_WR)
    print(sent, reader.read())
2026-11-21T11:52:30.990254000+0100 maurycy@gimel /tmp/r  % uv run --python 3.16 repro.py 
1 b'H'

it should be 3 b'Hbc'

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-macextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions