ruby-changes:71991
From: Samuel <ko1@a...>
Date: Sat, 28 May 2022 12:44:49 +0900 (JST)
Subject: [ruby-changes:71991] 1589ac6291 (master): Improve handling of zero length writes.
https://git.ruby-lang.org/ruby.git/commit/?id=1589ac6291 From 1589ac6291d6bf9caa8f237c6d143dd26526b579 Mon Sep 17 00:00:00 2001 From: Samuel Williams <samuel.williams@o...> Date: Mon, 9 May 2022 23:44:44 +1200 Subject: Improve handling of zero length writes. --- io.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/io.c b/io.c index 30fb42d932..21c5d21c65 100644 --- a/io.c +++ b/io.c @@ -1607,7 +1607,7 @@ io_binwrite_string_internal(rb_io_t *fptr, const char *ptr, long length) https://github.com/ruby/ruby/blob/trunk/io.c#L1607 fptr->wbuf.off += (int)result; fptr->wbuf.len -= (int)result; - return 0L; + result = 0; } return result; @@ -1664,21 +1664,19 @@ io_binwrite_string(VALUE arg) https://github.com/ruby/ruby/blob/trunk/io.c#L1664 // Write as much as possible: long result = (long)io_binwrite_string_internal(p->fptr, ptr, remaining); - // Finished: - if (result == remaining) { - break; - } + // It's possible that write can return 0 which implies we should wait for the file descriptor to be writable. + if (result == 0) errno = EAGAIN; - if (result >= 0) { + if (result > 0) { + if (result == remaining) break; ptr += result; remaining -= result; - errno = EAGAIN; } - // Wait for it to become writable: - if (rb_io_maybe_wait_writable(errno, p->fptr->self, Qnil)) { + else if (rb_io_maybe_wait_writable(errno, p->fptr->self, Qnil)) { rb_io_check_closed(p->fptr); - } else { + } + else { // The error was unrelated to waiting for it to become writable, so we fail: return -1; } @@ -1908,7 +1906,9 @@ io_binwritev_internal(VALUE arg) https://github.com/ruby/ruby/blob/trunk/io.c#L1906 while (remaining) { long result = rb_writev_internal(fptr, iov, iovcnt); - if (result >= 0) { + if (result == 0) errno = EAGAIN; + + if (result > 0) { offset += result; if (fptr->wbuf.ptr && fptr->wbuf.len) { if (offset < (size_t)fptr->wbuf.len) { @@ -5151,7 +5151,9 @@ finish_writeconv(rb_io_t *fptr, int noalloc) https://github.com/ruby/ruby/blob/trunk/io.c#L5151 size_t remaining = dp-ds; long result = rb_write_internal(fptr, ds, remaining); - if (result >= 0) { + if (result == 0) errno = EAGAIN; + + if (result > 0) { ds += result; if ((size_t)result == remaining) break; } -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/