Skip to content

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

Description

@giampaolo
BPO 36488
Nosy @giampaolo, @MojoVampire
PRs
  • bpo-36488: os.sendfile() on BSD and macOS doesn't return bytes sent on EINTR #12807
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-03-30.16:47:38.407>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = "os.sendfile() on BSD, macOS don't return bytes sent on EINTR"
    updated_at = <Date 2019-04-12.16:11:35.311>
    user = 'https://github.com/giampaolo'

    bugs.python.org fields:

    activity = <Date 2019-04-12.16:11:35.311>
    actor = 'giampaolo.rodola'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-03-30.16:47:38.407>
    creator = 'giampaolo.rodola'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36488
    keywords = ['patch']
    message_count = 4.0
    messages = ['339214', '339292', '339511', '339578']
    nosy_count = 3.0
    nosy_names = ['giampaolo.rodola', 'rosslagerwall', 'josh.r']
    pr_nums = ['12807']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue36488'
    versions = ['Python 3.7', 'Python 3.8']

    Linked PRs

    Activity

    1. giampaolo commented on Mar 30, 2019

      @giampaolo
      ContributorAuthor

      From "man sendfile" on both OSX and FreeBSD:

      <<[EINTR] A signal interrupted sendfile() before it could be completed. If specified, the number of bytes successfully sent will be returned in *len.>>

      Right now we catch EINTR and simply retry. Instead we should only retry is no bytes were sent, else we should return those bytes, similarly to what we do on EAGAIN and EBUSY:

      cpython/Modules/posixmodule.c

      Lines 8907 to 8917 in 2438cdf

      _Py_BEGIN_SUPPRESS_IPH
      do {
      Py_BEGIN_ALLOW_THREADS
      #ifdef __APPLE__
      ret = sendfile(in, out, offset, &sbytes, &sf, flags);
      #else
      ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
      #endif
      Py_END_ALLOW_THREADS
      } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
      _Py_END_SUPPRESS_IPH

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Mar 30, 2019
    3. changed the title [-]BSD/OSX: os.sendfile() does not return bytes sent on EINTR[/-] [+]os.sendfile() on BSD OSs and macOS does not return bytes sent on EINTR[/+] on Mar 30, 2019
    4. changed the title [-]os.sendfile() on BSD OSs and macOS does not return bytes sent on EINTR[/-] [+]os.sendfile() on BSD and macOS does not return bytes sent on EINTR[/+] on Mar 30, 2019
    5. MojoVampire commented on Apr 1, 2019

      MojoVampiremannequin
      Mannequin

      Wasn't the point of PEP-475 that all EINTR returns would be explicitly handled by retrying rather than forcing the user to handle it? Seems like the correct solution is still to retry, but on OSX/FreeBSD we'd need to update the offset and count arguments to account for the data that has already been sent.

    6. changed the title [-]os.sendfile() on BSD and macOS does not return bytes sent on EINTR[/-] [+]os.sendfile() on BSD, macOS don't return bytes sent on EINTR[/+] on Apr 5, 2019
    7. giampaolo commented on Apr 5, 2019

      @giampaolo
      ContributorAuthor

      sendfile() on BSD/OSX is complicated by the headers/trailers args. You'll have to take that into account in the retry logic, adding unnecessary complexity. Since sendfile() may already return fewer bytes than requested (e.g. non-blocking sockets or big files) it's just easier to return the bytes sent thus far (if any). I can work on a patch once I find some time.

      Wasn't the point of PEP-475 that all EINTR returns would be explicitly handled by retrying rather than forcing the user to handle it?

      From PEP-475: <<[...] to relieve application code from the burden of doing so>>

    8. MojoVampire commented on Apr 7, 2019

      MojoVampiremannequin
      Mannequin

      Right. So this is a hard problem for anyone to solve, and therefore os.sendfile should be the one solving it, not the caller of sendfile, right?

    9. transferred this issue fromon Apr 10, 2022
    10. moved this from Todo to In Progress in OS issues 💻on Nov 21, 2026
    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

      3.7 (EOL)end of life3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions