Skip to content

Commit

Permalink
Compatible handling of io timeout in extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 21, 2022
1 parent 07612ea commit 2f8b0df
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions ext/openssl/extconf.rb
Expand Up @@ -27,6 +27,7 @@
end

have_func("rb_io_maybe_wait") # Ruby 3.1
have_func("rb_io_timeout") # Ruby 3.2

Logging::message "=== Checking for system dependent stuff... ===\n"
have_library("nsl", "t_open")
Expand Down
14 changes: 12 additions & 2 deletions ext/openssl/ossl_ssl.c
Expand Up @@ -1641,11 +1641,21 @@ no_exception_p(VALUE opts)
return 0;
}

inline static
VALUE io_timeout()
{
#ifdef HAVE_RB_IO_TIMEOUT
return Qundef;
#else
return Qnil;
#endif
}

static void
io_wait_writable(rb_io_t *fptr)
{
#ifdef HAVE_RB_IO_MAYBE_WAIT
rb_io_maybe_wait_writable(errno, fptr->self, fptr->timeout);
rb_io_maybe_wait_writable(errno, fptr->self, io_timeout());
#else
rb_io_wait_writable(fptr->fd);
#endif
Expand All @@ -1655,7 +1665,7 @@ static void
io_wait_readable(rb_io_t *fptr)
{
#ifdef HAVE_RB_IO_MAYBE_WAIT
rb_io_maybe_wait_readable(errno, fptr->self, fptr->timeout);
rb_io_maybe_wait_readable(errno, fptr->self, io_timeout());
#else
rb_io_wait_readable(fptr->fd);
#endif
Expand Down
4 changes: 2 additions & 2 deletions ext/socket/init.c
Expand Up @@ -189,7 +189,7 @@ rsock_s_recvfrom(VALUE socket, int argc, VALUE *argv, enum sock_recv_type from)

if (slen >= 0) break;

if (!rb_io_maybe_wait_readable(errno, socket, fptr->timeout))
if (!rb_io_maybe_wait_readable(errno, socket, Qundef))
rb_sys_fail("recvfrom(2)");
}

Expand Down Expand Up @@ -705,7 +705,7 @@ rsock_s_accept(VALUE klass, VALUE io, struct sockaddr *sockaddr, socklen_t *len)
retry = 1;
goto retry;
default:
if (!rb_io_maybe_wait_readable(error, io, fptr->timeout)) break;
if (!rb_io_maybe_wait_readable(error, io, Qundef)) break;
retry = 0;
goto retry;
}
Expand Down
3 changes: 3 additions & 0 deletions include/ruby/io.h
Expand Up @@ -877,6 +877,9 @@ VALUE rb_io_set_timeout(VALUE io, VALUE timeout);
* here is a Ruby level integer, which is an OR-ed value of `IO::READABLE`,
* `IO::WRITable`, and `IO::PRIORITY`.
*
* If timeout is `Qundef`, it will use the default timeout as given by
* `rb_io_timeout(io)`.
*
* @param[in] io An IO object to wait.
* @param[in] events See above.
* @param[in] timeout Time, or numeric seconds since UNIX epoch.
Expand Down
5 changes: 4 additions & 1 deletion io.c
Expand Up @@ -830,7 +830,6 @@ rb_io_set_write_io(VALUE io, VALUE w)
return write_io ? write_io : Qnil;
}


/*
* call-seq:
* timeout -> duration or nil
Expand Down Expand Up @@ -1416,6 +1415,10 @@ rb_io_wait(VALUE io, VALUE events, VALUE timeout)
struct timeval tv_storage;
struct timeval *tv = NULL;

if (timeout == Qundef) {
timeout = fptr->timeout;
}

if (timeout != Qnil) {
tv_storage = rb_time_interval(timeout);
tv = &tv_storage;
Expand Down

0 comments on commit 2f8b0df

Please sign in to comment.