1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Try nil as default for 'default timeout'. (#6509)

This commit is contained in:
Samuel Williams 2022-10-08 14:02:34 +13:00 committed by GitHub
parent 5b3079a8a5
commit 844a9dff88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2022-10-08 10:02:59 +09:00
Merged-By: ioquatix <samuel@codeotaku.com>
6 changed files with 24 additions and 19 deletions

View file

@ -1285,7 +1285,7 @@ bsock_sendmsg_internal(VALUE sock, VALUE data, VALUE vflags,
if (ss == -1) { if (ss == -1) {
int e; int e;
if (!nonblock && rb_io_maybe_wait_writable(errno, fptr->self, fptr->timeout)) { if (!nonblock && rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
rb_io_check_closed(fptr); rb_io_check_closed(fptr);
goto retry; goto retry;
} }
@ -1557,7 +1557,7 @@ bsock_recvmsg_internal(VALUE sock,
if (ss == -1) { if (ss == -1) {
int e; int e;
if (!nonblock && rb_io_maybe_wait_readable(errno, fptr->self, fptr->timeout)) { if (!nonblock && rb_io_maybe_wait_readable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
rb_io_check_closed(fptr); rb_io_check_closed(fptr);
goto retry; goto retry;
} }

View file

@ -601,7 +601,7 @@ rsock_bsock_send(int argc, VALUE *argv, VALUE socket)
if (n >= 0) return SSIZET2NUM(n); if (n >= 0) return SSIZET2NUM(n);
if (rb_io_maybe_wait_writable(errno, socket, fptr->timeout)) { if (rb_io_maybe_wait_writable(errno, socket, RUBY_IO_TIMEOUT_DEFAULT)) {
continue; continue;
} }

View file

@ -189,7 +189,7 @@ rsock_s_recvfrom(VALUE socket, int argc, VALUE *argv, enum sock_recv_type from)
if (slen >= 0) break; if (slen >= 0) break;
if (!rb_io_maybe_wait_readable(errno, socket, Qundef)) if (!rb_io_maybe_wait_readable(errno, socket, RUBY_IO_TIMEOUT_DEFAULT))
rb_sys_fail("recvfrom(2)"); rb_sys_fail("recvfrom(2)");
} }
@ -705,7 +705,7 @@ rsock_s_accept(VALUE klass, VALUE io, struct sockaddr *sockaddr, socklen_t *len)
retry = 1; retry = 1;
goto retry; goto retry;
default: default:
if (!rb_io_maybe_wait_readable(error, io, Qundef)) break; if (!rb_io_maybe_wait_readable(error, io, RUBY_IO_TIMEOUT_DEFAULT)) break;
retry = 0; retry = 0;
goto retry; goto retry;
} }

View file

@ -170,7 +170,7 @@ udp_send_internal(VALUE v)
if (n >= 0) return RB_SSIZE2NUM(n); if (n >= 0) return RB_SSIZE2NUM(n);
if (rb_io_maybe_wait_writable(errno, fptr->self, fptr->timeout)) { if (rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
goto retry; goto retry;
} }
} }

View file

@ -59,7 +59,7 @@
#define RUBY_IO_WAIT_METHODS #define RUBY_IO_WAIT_METHODS
// Used as the default timeout argument to `rb_io_wait` to use the `IO#timeout` value. // Used as the default timeout argument to `rb_io_wait` to use the `IO#timeout` value.
#define RUBY_IO_TIMEOUT_DEFAULT Qundef #define RUBY_IO_TIMEOUT_DEFAULT Qnil
RBIMPL_SYMBOL_EXPORT_BEGIN() RBIMPL_SYMBOL_EXPORT_BEGIN()
@ -881,14 +881,14 @@ VALUE rb_io_set_timeout(VALUE io, VALUE timeout);
* here is a Ruby level integer, which is an OR-ed value of `IO::READABLE`, * here is a Ruby level integer, which is an OR-ed value of `IO::READABLE`,
* `IO::WRITable`, and `IO::PRIORITY`. * `IO::WRITable`, and `IO::PRIORITY`.
* *
* If timeout is `Qundef`, it will use the default timeout as given by * If timeout is `Qnil`, it will use the default timeout as given by
* `rb_io_timeout(io)`. * `rb_io_timeout(io)`.
* *
* @param[in] io An IO object to wait. * @param[in] io An IO object to wait.
* @param[in] events See above. * @param[in] events See above.
* @param[in] timeout Time, or numeric seconds since UNIX epoch. * @param[in] timeout Time, or numeric seconds since UNIX epoch.
* If Qnil, wait forever. If Qundef, use the * If Qnil, use the default timeout. If Qfalse
* default timeout. * or Qundef, wait forever.
* @exception rb_eIOError `io` is not open. * @exception rb_eIOError `io` is not open.
* @exception rb_eRangeError `timeout` is out of range. * @exception rb_eRangeError `timeout` is out of range.
* @exception rb_eSystemCallError `select(2)` failed for some reason. * @exception rb_eSystemCallError `select(2)` failed for some reason.

23
io.c
View file

@ -494,7 +494,7 @@ rb_cloexec_fcntl_dupfd(int fd, int minfd)
#if defined(_WIN32) #if defined(_WIN32)
#define WAIT_FD_IN_WIN32(fptr) \ #define WAIT_FD_IN_WIN32(fptr) \
(rb_w32_io_cancelable_p((fptr)->fd) ? Qnil : rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), fptr->timeout)) (rb_w32_io_cancelable_p((fptr)->fd) ? Qnil : rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), RUBY_IO_TIMEOUT_DEFAULT))
#else #else
#define WAIT_FD_IN_WIN32(fptr) #define WAIT_FD_IN_WIN32(fptr)
#endif #endif
@ -866,6 +866,11 @@ rb_io_timeout(VALUE self)
VALUE VALUE
rb_io_set_timeout(VALUE self, VALUE timeout) rb_io_set_timeout(VALUE self, VALUE timeout)
{ {
// Validate it:
if (RTEST(timeout)) {
rb_time_interval(timeout);
}
rb_io_t *fptr = rb_io_get_fptr(self); rb_io_t *fptr = rb_io_get_fptr(self);
fptr->timeout = timeout; fptr->timeout = timeout;
@ -1044,7 +1049,7 @@ void
rb_io_read_check(rb_io_t *fptr) rb_io_read_check(rb_io_t *fptr)
{ {
if (!READ_DATA_PENDING(fptr)) { if (!READ_DATA_PENDING(fptr)) {
rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), fptr->timeout); rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), RUBY_IO_TIMEOUT_DEFAULT);
} }
return; return;
} }
@ -1395,7 +1400,7 @@ io_fflush(rb_io_t *fptr)
return 0; return 0;
while (fptr->wbuf.len > 0 && io_flush_buffer(fptr) != 0) { while (fptr->wbuf.len > 0 && io_flush_buffer(fptr) != 0) {
if (!rb_io_maybe_wait_writable(errno, fptr->self, fptr->timeout)) if (!rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT))
return -1; return -1;
rb_io_check_closed(fptr); rb_io_check_closed(fptr);
@ -1419,7 +1424,7 @@ rb_io_wait(VALUE io, VALUE events, VALUE timeout)
struct timeval tv_storage; struct timeval tv_storage;
struct timeval *tv = NULL; struct timeval *tv = NULL;
if (timeout == Qundef) { if (timeout == Qnil || timeout == Qundef) {
timeout = fptr->timeout; timeout = fptr->timeout;
} }
@ -1793,7 +1798,7 @@ io_binwrite_string(VALUE arg)
remaining -= result; remaining -= result;
} }
// Wait for it to become writable: // Wait for it to become writable:
else if (rb_io_maybe_wait_writable(errno, p->fptr->self, p->fptr->timeout)) { else if (rb_io_maybe_wait_writable(errno, p->fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
rb_io_check_closed(p->fptr); rb_io_check_closed(p->fptr);
} }
else { else {
@ -2060,7 +2065,7 @@ io_binwritev_internal(VALUE arg)
iov->iov_base = (char *)iov->iov_base + result; iov->iov_base = (char *)iov->iov_base + result;
iov->iov_len -= result; iov->iov_len -= result;
} }
else if (rb_io_maybe_wait_writable(errno, fptr->self, fptr->timeout)) { else if (rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
rb_io_check_closed(fptr); rb_io_check_closed(fptr);
} }
else { else {
@ -2567,7 +2572,7 @@ rb_io_rewind(VALUE io)
static int static int
fptr_wait_readable(rb_io_t *fptr) fptr_wait_readable(rb_io_t *fptr)
{ {
int result = rb_io_maybe_wait_readable(errno, fptr->self, fptr->timeout); int result = rb_io_maybe_wait_readable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT);
if (result) if (result)
rb_io_check_closed(fptr); rb_io_check_closed(fptr);
@ -5291,7 +5296,7 @@ finish_writeconv(rb_io_t *fptr, int noalloc)
ds += result; ds += result;
if ((size_t)result == remaining) break; if ((size_t)result == remaining) break;
} }
else if (rb_io_maybe_wait_writable(errno, fptr->self, fptr->timeout)) { else if (rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
if (fptr->fd < 0) if (fptr->fd < 0)
return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr(closed_stream)); return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr(closed_stream));
} }
@ -12284,7 +12289,7 @@ fiber_scheduler_wait_for(void * _arguments)
{ {
struct fiber_scheduler_wait_for_arguments *arguments = (struct fiber_scheduler_wait_for_arguments *)_arguments; struct fiber_scheduler_wait_for_arguments *arguments = (struct fiber_scheduler_wait_for_arguments *)_arguments;
arguments->result = rb_fiber_scheduler_io_wait(arguments->scheduler, arguments->fptr->self, INT2NUM(arguments->events), arguments->fptr->timeout); arguments->result = rb_fiber_scheduler_io_wait(arguments->scheduler, arguments->fptr->self, INT2NUM(arguments->events), RUBY_IO_TIMEOUT_DEFAULT);
return NULL; return NULL;
} }