IO#timeout=
VALUE
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);
fptr->timeout = timeout;
return self;
}
Sets the internal timeout to the specified duration or nil. The timeout applies to all blocking operations where possible.
When the operation performs longer than the timeout set, IO::TimeoutError is raised.
This affects the following methods (but is not limited to): gets, puts, read, write, wait_readable and wait_writable. This also affects blocking socket operations like Socket#accept and Socket#connect.
Some operations like File#open and IO#close are not affected by the timeout. A timeout during a write operation may leave the IO in an inconsistent state, e.g. data was partially written. Generally speaking, a timeout is a last ditch effort to prevent an application from hanging on slow I/O operations, such as those that occur during a slowloris attack.
Ruby version changes
-
3.2
-
Introduce
IO#timeout=andIO#timeoutwhich can causeIO::TimeoutErrorto be raised if a blocking operation exceeds the specified timeout. [Feature #18630]ExampleSTDIN.timeout = 1 STDIN.read # => Blocking operation timed out! (IO::TimeoutError)
-