This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients christian.heimes, kevans, koobs, vstinner
Date 2020-06-25.10:13:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1593079995.43.0.452547607223.issue41013@roundup.psfhosted.org>
In-reply-to
Content
> So in my local repro, Python is looping forever on successful write() for reasons I'm not immediately sure of.

io.BufferedWriter.write() (well, especially its flush() method) calls write() until all data is written.

Extract of _bufferedwriter_flush_unlocked() code, Modules/_io/buffered.c:

    while (self->write_pos < self->write_end) {
        n = _bufferedwriter_raw_write(self,
            self->buffer + self->write_pos,
            Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
                             Py_off_t, Py_ssize_t));
        if (n == -1) {
            goto error;
        }
        else if (n == -2) {
            _set_BlockingIOError("write could not complete without blocking",
                                 0);
            goto error;
        }
        self->write_pos += n;
        self->raw_pos = self->write_pos;
        written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t);
        /* Partial writes can return successfully when interrupted by a
           signal (see write(2)).  We must run signal handlers before
           blocking another time, possibly indefinitely. */
        if (PyErr_CheckSignals() < 0)
            goto error;
    }

You are correct: if write() returns 0, write() is called again. If write() always returns 0, the loop never stops...

Maybe a BlockingIOError must be raised if write(buffer) returns 0 (and buffer is not empty).
History
Date User Action Args
2020-06-25 10:13:15vstinnersetrecipients: + vstinner, christian.heimes, koobs, kevans
2020-06-25 10:13:15vstinnersetmessageid: <1593079995.43.0.452547607223.issue41013@roundup.psfhosted.org>
2020-06-25 10:13:15vstinnerlinkissue41013 messages
2020-06-25 10:13:15vstinnercreate