Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block

I get the following error when I try to use the method "read_nonblock" from the "socket" library

IO::EAGAINWaitReadable: Resource temporarily unavailable - read would block

But when I try it through the IRB on the terminal it works fine

How can I make it read the buffer?

like image 872
tisokoro Avatar asked Jun 05 '15 14:06

tisokoro


1 Answers

I get the following error when I try to use the method "read_nonblock" from the "socket" library

It is expected behaviour when the data isn't ready in the buffer. Since the exception, IO::EAGAINWaitReadable is originated from ruby version 2.1.0, in older version you must trap IO::WaitReadable with additional port selection and retry. So do as it was adviced in the ruby documentation:

begin
   result = io.read_nonblock(maxlen)
rescue IO::WaitReadable
   IO.select([io])
   retry
end

For newer version os ruby you should trap IO::EAGAINWaitReadable also, but just with retrial reading for a timeout or infinitely. I haven't found out the example in the docs, but remember that it was without port selection:

begin
   result = io.read_nonblock(maxlen)
rescue IO::EAGAINWaitReadable
   retry
end

However some my investigations lead to that it is also better to do port selection on IO::EAGAINWaitReadable, so you'll can get:

begin
   result = io.read_nonblock(maxlen)
rescue IO::WaitReadable, IO::EAGAINWaitReadable
   IO.select([io])
   retry
end

To support the code with both versions of exception, just declare the definition of IO::EAGAINWaitReadable in lib/ core under if clause:

if ! ::IO.const_defined?(:EAGAINWaitReadable)
   class ::IO::EAGAINWaitReadable; end
end
like image 72
Малъ Скрылевъ Avatar answered Oct 11 '22 23:10

Малъ Скрылевъ