Skip to content

Commit 0206905

Browse files
committed
Add support for IO#timeout.
1 parent 8aa3849 commit 0206905

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

ext/openssl/ossl_ssl.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,9 @@ static void
17291729
io_wait_writable(VALUE io)
17301730
{
17311731
#ifdef HAVE_RB_IO_MAYBE_WAIT
1732-
rb_io_maybe_wait_writable(errno, io, RUBY_IO_TIMEOUT_DEFAULT);
1732+
if (!rb_io_maybe_wait_writable(errno, io, RUBY_IO_TIMEOUT_DEFAULT)) {
1733+
rb_raise(rb_eIOTimeoutError, "Timed out while waiting to become writable!");
1734+
}
17331735
#else
17341736
rb_io_t *fptr;
17351737
GetOpenFile(io, fptr);
@@ -1741,7 +1743,9 @@ static void
17411743
io_wait_readable(VALUE io)
17421744
{
17431745
#ifdef HAVE_RB_IO_MAYBE_WAIT
1744-
rb_io_maybe_wait_readable(errno, io, RUBY_IO_TIMEOUT_DEFAULT);
1746+
if (!rb_io_maybe_wait_readable(errno, io, RUBY_IO_TIMEOUT_DEFAULT)) {
1747+
rb_raise(rb_eIOTimeoutError, "Timed out while waiting to become readable!");
1748+
}
17451749
#else
17461750
rb_io_t *fptr;
17471751
GetOpenFile(io, fptr);

lib/openssl/ssl.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,16 @@ def wait_readable(*args)
299299
def wait_writable(*args)
300300
to_io.wait_writable(*args)
301301
end
302+
303+
if IO.method_defined?(:timeout)
304+
def timeout
305+
to_io.timeout
306+
end
307+
308+
def timeout=(value)
309+
to_io.timeout=(value)
310+
end
311+
end
302312
end
303313

304314
def verify_certificate_identity(cert, hostname)

test/openssl/test_ssl.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ def test_sysread_and_syswrite
193193
}
194194
end
195195

196+
def test_read_with_timeout
197+
omit "does not support timeout" unless IO.method_defined?(:timeout)
198+
199+
start_server do |port|
200+
server_connect(port) do |ssl|
201+
ssl.timeout = 0.001
202+
assert_raise(IO::TimeoutError) {ssl.read(1)}
203+
end
204+
end
205+
end
206+
196207
def test_getbyte
197208
start_server { |port|
198209
server_connect(port) { |ssl|

0 commit comments

Comments
 (0)