Skip to content

gh-80669: Fix os.sendfile() resending data after EINTR on macOS, FreeBSD and DragonFly - #162315

Open
maurycy wants to merge 3 commits into
python:mainfrom
maurycy:sendfile-eintr
Open

maurycy wants to merge 3 commits into
python:mainfrom
maurycy:sendfile-eintr

Conversation

@maurycy

@maurycy maurycy commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

On macOS, FreeBSD and DragonFly BSD, sendfile() can return EINTR after sending part of the requested data. Retrying from the original offset sends those bytes twice.

After checking for an exception from the signal handler, os.sendfile() now returns the number of bytes sent so far. It retries only if no bytes were sent. On macOS, the retry also restores the requested byte count because passing zero tells the kernel to send the rest of the file.

With a full socket buffer on macOS, a 4 KiB request sent the entire 1 MiB file before this fix. The same call now sends 4 KiB.

As per PEP 475, os.sendfile() still handles EINTR without raising InterruptedError. If no bytes were sent, it retries. Otherwise, it returns the number of bytes sent, as it already does for EAGAIN and EBUSY.

Fixes #80669

@maurycy

maurycy commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

cc @giampaolo @MojoVampire @vstinner

@maurycy maurycy changed the title gh-80669: Fix os.sendfile() resending data after EINTR gh-80669: Fix os.sendfile() resending data after EINTR on macOS, FreeBSD and DragonFly Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

os.sendfile() on BSD, macOS don't return bytes sent on EINTR

1 participant