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

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?

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

Related

Ruby C extension returns report_on_exception is true

I was trying to import server_info api in the ibm_db extension. It worked well in windows but in Linux, it returns tread sleep error.
#<Thread:0x000055c9febc5ca0 sleep> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
stack level too deep (SystemStackError)
^C*** stack smashing detected ***: <unknown> terminated
code:-
require 'ibm_db'
conn = IBM_DB.connect('DATABASE=;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=;PWD=','','')
puts 'this is ibm_db'
#Thread.report_on_exception = false
puts 'this is begin'
puts IBM_DB
serverinfo = IBM_DB::server_info( conn )
puts serverinfo.DBMS_NAME[0,100]
puts IBM_DB.close(conn)
why the same code returns the error in Linux but woe=rks well in windows.
When I run the code in debug mode it does not produce any error only in normal mode it produces an error.
Thanks,
Akhil
Well, two approach
use sleep for 5-10 second after db connection
check db connection like: p "connected" if ActiveRecord::Base.connected?
In your case, need to check in ibm_db gem how to check if connection established or not
conn=ibm_db.connect("DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password",'','')
connState = ibm_db.active(conn)
print(connState)
if it says "TRUE" go ahead else pause/sleep whatever your requirement.

Thread deadlock on simple operations in pry

[Note: I "fixed" this problem by creating and using a new gemset. I'm still curious why the problem occurred but it is no longer blocking me.]
[I am aware that there is a similar issue at Deadlock in Ruby join(), but I have tried the timeout parameter suggested there and it does not help. I suspect there is a pry-specific problem not covered there.]
I am getting the error below when running the code below, but only when executed within a pry session. This code has not been changed and has been working fine for quite a while, and I have no idea why it's an issue just now. I am using pry version 0.11.3 on Ruby 2.5.1. Also, this code works fine when pasted into pry; it's not working in my wifi-wand application that launches pry in the context of one of its objects (gem install wifi-wand to install, https://github.com/keithrbennett/wifiwand is the project page).
domains = %w(google.com baidu.com)
puts "Calling dig on domains #{domains}..." if #verbose_mode
threads = domains.map do |domain|
Thread.new do
output = `dig +short #{domain}`
output.length > 0
end
end
threads.each(&:join)
[1] pry(#<WifiWand::CommandLineInterface>)> ci
Calling dig on domains ["google.com", "baidu.com"]...
fatal: No live threads left. Deadlock?
3 threads, 3 sleeps current:0x00007fbd13d0c5a0 main thread:0x00007fbd13d0c5a0
* #<Thread:0x00007fbd14069c20 sleep_forever>
rb_thread_t:0x00007fbd13d0c5a0 native:0x00007fff89c2b380 int:0
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:89:in `join'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:89:in `each'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:89:in `block in connected_to_internet?'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/models/base_model.rb:126:in `connected_to_internet?'
/Users/kbennett/work/wifi-wand/lib/wifi-wand/command_line_interface.rb:264:in `cmd_ci'
Strangely, the problem was fixed by uninstalling and reinstalling the awesome_print gem. I have no idea why.

Process.detach cause sinatra-1.4.7 auto exit

here is my code:
class App < Sinatra::Base
get "/" do
pid =fork do
end
Process.detach(pid)
end
end
App.start!
when i curl localhost:4567, the server auto exit and the output is:
127.0.0.1 - - [13/Aug/2016:23:45:18 CST] "GET / HTTP/1.1" 200 0
- -> /
[2016-08-13 23:45:18] INFO WEBrick::HTTPServer#start done.
== Sinatra has ended his set (crowd applauds)
my environment is:
Linux Mint 17.3 Rosa
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
Why does Sinatra quit after just one request? I want it to keep running until I deliberately kill it.
It's not Process.detach that causes Sinatra to quit, but the fact that the forked process ends and runs its at_exit hooks, which shut down the server (though I'm not quite sure how that can leak from the child process to the parent).
There's a couple of simple ways to prevent this.
I prefer this set-and-forget solution:
configure do
disable :traps
end
The downside is that Ctrl-C causes a not-so-graceful exit.
Alternatively, add this to the beginning of any fork block:
settings.running_server = nil
The downside is that you need to remember to add this to all forked code.
You might also find someone suggesting at_exit { Process.exit! } in all fork blocks, but I think it's less reliable because other at_exit handlers could interfere with it.
Unfortunately, I don't see any configuration option in Sinatra that would fix forking and still allow graceful handling of Ctrl-C.

chef: How do I increase the CommandTimout for Mixlib::ShellOut in a ruby block?

I'm trying to perform a database data load in a chef recipe on an Ubuntu 14.04 system with a Chef 11.10.4 based client. I'm doing the data load in a ruby block and here are the relevant portions of the error in the log:
Mixlib::ShellOut::CommandTimeout
Command timed out after 600s:
Command exceeded allowed execution time, killed by TERM signal.
[2015-10-06T01:24:00+00:00] ERROR: ruby_block[Load Dataset] (chef-virtuoso::load line 178) had an error:
Mixlib::ShellOut::CommandTimeout: Command timed out after 600s:
Anyone know how to increase the default CommandTimeout to 3600 for a shell_out! in a ruby block?
Here is the ruby block from the recipe that is failing:
::Chef::Recipe.send(:include, Chef::Mixin::ShellOut)
ruby_block "Load Dataset" do
block do
shell_out!(DATA_LOAD_COMMAND)
end
end
According to mixlib-shellout code and chef mixin shell_out code you should be able to use the timeout option like this:
ruby_block "Load Dataset" do
block do
shell_out!(DATA_LOAD_COMMAND, :timeout => 3600)
end
end
You don't have to send the Class into Recipe, it's already part of the DSL.

retry in Ruby can not be performed

for i in 1..5
retry if i > 2
puts "Value of local variable is #{i}"
end
When I ran the code above, I got an error message saying Invalid retry
The version of Ruby I was using is 1.9.3. Would anyone know what went wrong with the code?
From Ruby 1.9 onwards retry needs to be in a rescue clause and will only work when in a rescue clause

Resources