Ruby deadlock waiting on Process::Waiter with Open3.popen3 - ruby

I was using Open3.capture3 in my ruby code and started to notice deadlocks. I did some research and came across this great blog about how open3 can cause deadlocks. Taking this into account I came up with my own capture3 method (still using Open3.popen3) to replace one I was currently using (see below). However I have found that deadlocks still seem to be occurring and the responsible party is the blocking call on the wait_thread.value to get the Process::Status. Looking into Open3 source code I can see this wait_thread is just a Process::Waiter that you get from a Process.detach call. I was wondering what could be causing this deadlock? Is there some way I can check to see if this thread is deadlocked, kill it and return a custom Process::Status (I'd like to return a Process::Status as opposed to just an integer to stay inline with Open3.capture3)
def capture3(cmd)
out_buff = ''
err_buff = ''
chunk_size = 4096
# Progressive timeout array
sleeps = [[0.05]*20,[0.1]*5,[0.5]*3,1,2].flatten
_in, out, err, wait_thread = Open3.popen3(cmd)
_in.close
open_pipes = [out, err]
while !open_pipes.empty?
timeout = sleeps.shift || timeout
ready_pipes, *_ = IO.select(open_pipes, nil, nil, timeout)
ready_pipes.each do |pipe|
tmp = ''
begin
tmp << pipe.readpartial(chunk_size)
rescue EOFError
pipe.close
open_pipes.delete(pipe)
end
out_buff << tmp if pipe == out
err_buff << tmp if pipe == err
end
end
[out_buff, err_buff, wait_thread.value]
end
Note ruby version I'm using is 2.2.5 and running on Mac.
Edit:
The exact error is No live threads left. Deadlock? (fatal) pointing to line number of the wait_thread.value call. I'm calling the capture3 method in another function where I fork the process like so
def fork_process(cmd, options={})
pid = fork {
Process.setproctitle(options[:process_name]) unless options[:process_name].nil?
ENV.merge!(options[:environment])
stdout, stderr, status = capture3(cmd)
yield(stdout, stderr, status) if block_given?
}
Process.detach(pid)
end
Full stacktrace:
/home/myapp/utils.rb:769:in `value': No live threads left. Deadlock? (fatal)
from /home/myapp/utils.rb:769:in `capture3'
from /home/myapp/utils.rb:106:in `block in fork_process'
from /home/myapp/utils.rb:104:in `fork'
from /home/myapp/utils.rb:104:in `fork_process'
from /home/myapp/server.rb:396:in `block in <class:MyServer>'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `block in compile!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `[]'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (3 levels) in route!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:994:in `route_eval'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (2 levels) in route!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1015:in `block in process_route'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `catch'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `process_route'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:973:in `block in route!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `each'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `route!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1085:in `block in dispatch!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1082:in `dispatch!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `block in call!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `call!'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:895:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-1.6.5/lib/rack/nulllogger.rb:9:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/rack-1.6.5/lib/rack/head.rb:13:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/show_exceptions.rb:25:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:182:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:2013:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `block in call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1787:in `synchronize'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/puma-3.8.2/lib/puma/configuration.rb:224:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/puma-3.8.2/lib/puma/server.rb:600:in `handle_request'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/puma-3.8.2/lib/puma/server.rb:435:in `process_client'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/puma-3.8.2/lib/puma/server.rb:299:in `block in run'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/puma-3.8.2/lib/puma/thread_pool.rb:120:in `call'
from /Users/app/.rvm/gems/ruby-2.2.5/gems/puma-3.8.2/lib/puma/thread_pool.rb:120:in `block in spawn_thread'

Related

Delayed Job Delayed::PerformableMethod undefined method "delayed_task" for #<Class:0x0000000b4dcd20>

I am using delayed jobs and I think I have a problem with serialization. We are using ruby 2.2.2.
In my model I have a callback that fires on save and creates a delayed job for my model. like this:
after_create :update_queue
def update_queue
self.class.delay(run_at: 1.minutes.from_now, delayed_reference_id: self.id, delayed_reference_type: "Payment", queue: "qbo_payments").delayed_task(self.id)
end
def self.delayed_task
# Do some stuff
end
However I am getting the error:
undefined method `delayed_task' for #<Class:0x0000000f93f9b8>
~/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
~/shared/bundle/ruby/2.2.0/gems/attr_encrypted-3.0.3/lib/attr_encrypted.rb:295:in `method_missing'
~/shared/bundle/ruby/2.2.0/gems/attr_encrypted-3.0.3/lib/attr_encrypted/adapters/active_record.rb:129:in `method_missing_with_attr_encrypted'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/performable_method.rb:26:in `perform'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/backend/base.rb:84:in `block in invoke_job'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `block in initialize'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `execute'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:40:in `run_callbacks'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/backend/base.rb:81:in `invoke_job'
~/shared/bundle/ruby/2.2.0/gems/newrelic_rpm-3.16.0.318/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb:127:in `block in invoke_job'
~/shared/bundle/ruby/2.2.0/gems/newrelic_rpm-3.16.0.318/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:363:in `perform_action_with_newrelic_trace'
~/shared/bundle/ruby/2.2.0/gems/newrelic_rpm-3.16.0.318/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb:126:in `invoke_job'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:230:in `block (2 levels) in run'
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:89:in `block in timeout'
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `call'
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/timeout.rb:99:in `timeout'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:230:in `block in run'
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:229:in `run'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:306:in `block in reserve_and_run_one_job'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `block in initialize'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `execute'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:40:in `run_callbacks'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:306:in `reserve_and_run_one_job'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:213:in `block in work_off'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:212:in `times'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:212:in `work_off'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:175:in `block (4 levels) in start'
/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:174:in `block (3 levels) in start'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `block in initialize'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `execute'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:40:in `run_callbacks'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:173:in `block (2 levels) in start'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:172:in `loop'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:172:in `block in start'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/plugins/clear_locks.rb:7:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/plugins/clear_locks.rb:7:in `block (2 levels) in <class:ClearLocks>'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:79:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:79:in `block (2 levels) in add'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:61:in `block in initialize'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:79:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:79:in `block in add'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `call'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:66:in `execute'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/lifecycle.rb:40:in `run_callbacks'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/worker.rb:171:in `start'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/command.rb:132:in `run'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/command.rb:120:in `block in run_process'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/application.rb:265:in `call'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/application.rb:265:in `block in start_proc'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/daemonize.rb:84:in `call'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/daemonize.rb:84:in `call_as_daemon'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/application.rb:269:in `start_proc'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/application.rb:295:in `start'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/controller.rb:69:in `run'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons.rb:193:in `block in run_proc'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/cmdline.rb:88:in `call'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons/cmdline.rb:88:in `catch_exceptions'
~/shared/bundle/ruby/2.2.0/gems/daemons-1.2.3/lib/daemons.rb:192:in `run_proc'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/command.rb:118:in `run_process'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/command.rb:99:in `block in daemonize'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/command.rb:97:in `times'
~/shared/bundle/ruby/2.2.0/gems/delayed_job-4.1.2/lib/delayed/command.rb:97:in `daemonize'
script/delayed_job:5:in `<main>'"
What I have tried
After a bit of google research I found 2 fixes. The first and more widely accepted fix is to add:
require 'yaml'
YAML::ENGINE.yamler = 'syck'
#https://github.com/collectiveidea/delayed_job/issues/199
However, syck is no longer supported for ruby 2.2.2
The second solution, which only seemed to exacerbate the problem, was to add "bundle exec" to my delayed_job startup script like this:
run "cd #{current_path}; bundle exec script/delayed_job start #{rails_env}"
This will work !
after_create :update_queue
def update_queue
self.delay(run_at: 1.minutes.from_now, delayed_reference_id: self.id, delayed_reference_type: "Payment", queue: "qbo_payments").delayed_task()
end
def delayed_task
self.class.delayed_task(self.id)
end
def self.delayed_task
# Do some stuff
end

Can't successfully find a Twilio subaccount with twilio-rb

Big fan of using twilio-rb over twilio-ruby, but i'm stuck when trying to provide the Twilio subaccount's friendly_name to request the account_sid.
app.rb
Twilio::Config.setup \
:account_sid => '[MAIN ACCOUNT_SID]',
:auth_token => '[AUTH_TOKEN]'
subaccount.rb
post '/subaccount/create' do
Twilio::Account.create :friendly_name => params[:friendly_name]
account = Twilio::Account.find :friendly_name => params[:friendly_name]
#subaccount = Subaccount.new
#subaccount.friendly_name = params[:friendly_name]
#subaccount.account_sid = account.sid
if #subaccount.save
redirect '/subaccount/create'
else
erb 'There has been an error saving.'
end
end
It creates the subaccount in Twilio just fine, it does not however find the subaccount with the friendly_name.
When I break this down further, I have a subaccount named 'hello' and I want the SID for it:
account = Twilio::Account.find :friendly_name => "hello"
puts account.sid
This is what is returned in the Terminal
Called from: /Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/builder.rb:86:in `new'.
URI::InvalidURIError - bad URI(is not URI?): /Accounts/{:friendly_name=>"hello"}.json:
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/uri/common.rb:176:in `split'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/uri/common.rb:211:in `parse'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/uri/common.rb:747:in `parse'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/httparty-0.10.2/lib/httparty/request.rb:44:in `path='
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/httparty-0.10.2/lib/httparty/request.rb:33:in `initialize'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/httparty-0.10.2/lib/httparty.rb:456:in `new'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/httparty-0.10.2/lib/httparty.rb:456:in `perform_request'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/httparty-0.10.2/lib/httparty.rb:398:in `get'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/twilio-rb-2.3.0/lib/twilio/resource.rb:116:in `block (2 levels) in singleton class'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/twilio-rb-2.3.0/lib/twilio/finder.rb:9:in `find'
/Users/chadsakonchick/Projects/cloud-phone/lib/subaccounts.rb:27:in `block in <top (required)>'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `block in compile!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `[]'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (3 levels) in route!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:985:in `route_eval'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (2 levels) in route!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1006:in `block in process_route'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1004:in `catch'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1004:in `process_route'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:964:in `block in route!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:963:in `each'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:963:in `route!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1076:in `block in dispatch!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `block in invoke'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `catch'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `invoke'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1073:in `dispatch!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:898:in `block in call!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `block in invoke'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `catch'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `invoke'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:898:in `call!'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:886:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/session/abstract/id.rb:225:in `context'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/session/abstract/id.rb:220:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/logger.rb:15:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/commonlogger.rb:33:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:217:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:210:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/head.rb:13:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/methodoverride.rb:22:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/show_exceptions.rb:21:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:180:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:2014:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `block in call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1788:in `synchronize'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/handler/webrick.rb:89:in `service'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
[2014-11-07 18:22:32] ERROR NoMethodError: undefined method `join' for #<String:0x007fc7548fa570>
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/show_exceptions.rb:37:in `rescue in call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/show_exceptions.rb:21:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:180:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:2014:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `block in call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1788:in `synchronize'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `call'
/Users/chadsakonchick/.rvm/gems/ruby-2.1.2/gems/rack-1.6.0.beta/lib/rack/handler/webrick.rb:89:in `service'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/chadsakonchick/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
localhost - - [07/Nov/2014:18:22:32 CST] "GET /subaccount/twilio-rb/hello HTTP/1.1" 500 340
Try this:
account = Twilio::Account.all :friendly_name => "hello"
puts account[0].sid
It took me a few minutes of digging in the twilio-rb gem, it's the Finder class that is being included into the Account class that does the work. In this scenario we want to use the API List resource which Stevie and co supported with the all method with a filter parameter.
Using the find method requires the account SID, and doesn't seem to support parameters. I think it might be worth making a pull request to add this to the documentation, as it's not obvious.
Update
See above as I added the [0] indexer to account as this is an Array.

Ruby giving "NoMethodError: undefined method `[]' for nil:NilClass" with Sinatra

I am total newbie to Ruby and Sinatra and I am trying to learn them.
So this is my ruby code (File: ruby_test2.rb)
#!/usr/bin/ruby
require 'rubygems'
class LearnRuby
def initalize
#listen = Hash.new { |hash, key| hash[key] = []; }
end
def fill_listen(uid, mid)
#listen[uid]<<=mid
"In fill_listen #{#listen}\n"
end
end
And this is my Sinatra code (file: sinatra_test2.rb)
require 'sinatra'
require './ruby_test2'
if __FILE__ == $0
lr = LearnRuby.new
end
post '/listen' do
lr.fill_listen params[:uid] , params[:mid]
end
An this is how I am running it.
ruby sinatra_test2.rb <-- Starts the server
curl --data "uid=u1&mid=m312" http://localhost:4567/listen <-- Curl command for post request
When I execute the above I get this error (on server and client(curl) side)
localhost - - [22/Mar/2014:22:54:19 IST] "POST /listen HTTP/1.1" 500 3770
- -> /listen
NoMethodError - undefined method `[]' for nil:NilClass:
<path>/ruby_test2.rb:11:in `fill_listen'
sinatra_test2.rb:9:in `block in <main>'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `block in compile!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `[]'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (3 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:976:in `route_eval'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (2 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:997:in `block in process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:955:in `block in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `each'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `route!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1067:in `block in dispatch!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1064:in `dispatch!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `block in call!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `call!'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:877:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/xss_header.rb:18:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/path_traversal.rb:16:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/json_csrf.rb:18:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/frame_options.rb:31:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:217:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:210:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/show_exceptions.rb:21:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:180:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:2004:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `block in call'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1778:in `synchronize'
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `call'
/var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
localhost - - [22/Mar/2014:22:54:51 IST] "POST /listen HTTP/1.1" 500 3770
I may be missing out on something small but I need help as I am just starting to learn these technologies.
Thanks in advance.
Here is the working example for you, the #listen was nil, because you misspelled initalize -> initialize
require 'sinatra'
class LearnRuby
def initialize
#listen = {}
end
def fill_listen(uid, mid)
#listen[uid] = mid
"In fill_listen #{ #listen }\n"
end
end
post '/listen' do
lr = LearnRuby.new
lr.fill_listen(params[:uid], params[:mid])
end
replace #listen[uid]<<=mid with #listen[uid] = mid
Also, in case you only require initialize to create an empty hash then you can replace #listen = Hash.new { |hash, key| hash[key] = []; } with #listen = {}

Ruby IO Error, not opened for reading

Using OmniAuth for Facebook with Sinatra and Ruby 1.9.3. Getting the IO not opened for reading error during the latter stage of the auth process. Haven't seen much else about this online. If you require any more info please let me know.
Failing on the MultiJson line:
get '/auth/failure' do
content_type 'application/json'
MultiJson.encode(request.env)
end
Error:
IOError at /auth/facebook/callback
not opened for reading
file: encoding.rb location: each line: 256
Detailed error log:
== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.
Called from: /Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:86:in `new'.
127.0.0.1 - - [08/Dec/2013 18:34:03] "GET / HTTP/1.1" 302 - 0.0025
I, [2013-12-08T18:34:03.828713 #927] INFO -- omniauth: (facebook) Request phase initiated.
127.0.0.1 - - [08/Dec/2013 18:34:03] "GET /auth/facebook HTTP/1.1" 302 203 0.0049
I, [2013-12-08T18:34:04.278981 #927] INFO -- omniauth: (facebook) Callback phase initiated.
IOError - not opened for reading:
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:256:in `each'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:256:in `to_a'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:256:in `as_json'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:58:in `block in as_json'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:57:in `as_json'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:296:in `block in as_json'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:296:in `each'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:296:in `map'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:296:in `as_json'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:50:in `block in encode'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:49:in `encode'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/json/encoding.rb:34:in `encode'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/multi_json-1.8.2/lib/multi_json/adapters/json_common.rb:21:in `dump'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/multi_json-1.8.2/lib/multi_json/adapter.rb:24:in `dump'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/multi_json-1.8.2/lib/multi_json.rb:137:in `dump'
app.rb:87:in `block in <main>'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `block in compile!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `[]'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (3 levels) in route!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:976:in `route_eval'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (2 levels) in route!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:997:in `block in process_route'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `catch'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `process_route'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:955:in `block in route!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `each'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `route!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1067:in `block in dispatch!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1064:in `dispatch!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `block in call!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `call!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:877:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:401:in `call_app!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:363:in `callback_phase'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/omniauth-oauth2-1.0.3/lib/omniauth/strategies/oauth2.rb:65:in `callback_phase'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:226:in `callback_call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:182:in `call!'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/omniauth-1.1.4/lib/omniauth/strategy.rb:164:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/omniauth-1.1.4/lib/omniauth/builder.rb:49:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-protection-1.5.1/lib/rack/protection/xss_header.rb:18:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-protection-1.5.1/lib/rack/protection/path_traversal.rb:16:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-protection-1.5.1/lib/rack/protection/json_csrf.rb:18:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-protection-1.5.1/lib/rack/protection/base.rb:50:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-protection-1.5.1/lib/rack/protection/base.rb:50:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:217:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:210:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/show_exceptions.rb:21:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:180:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:2004:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `block in call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1778:in `synchronize'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/thin-1.6.1/lib/thin/connection.rb:82:in `block in pre_process'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/thin-1.6.1/lib/thin/connection.rb:80:in `catch'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/thin-1.6.1/lib/thin/connection.rb:80:in `pre_process'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `call'
/Users/philhudson/.rvm/gems/ruby-1.9.3-p448/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `block in spawn_threadpool'
Turns out a GEM conflict was the issue. I fixed the problem with the code from an indirectly related question that used Rails instead. Please see below:
#fix for JSON gem/activesupport bug. More info: http://stackoverflow.com/questions/683989/how-do-you-deal-with-the-conflict-between-activesupportjson-and-the-json-gem
if defined?(ActiveSupport::JSON)
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
klass.class_eval do
def to_json(*args)
super(args)
end
def as_json(*args)
super(args)
end
end
end
end

Debugging Sinatra application

I install pry and try to use it.
gem "pry"
require 'pry'
#..........
binding.pry
But there are 3 issues :
I don't know to go to the next string (not to the next break point) or watch the value of a variable. There is no information about it!
there is a lot of different prys! pry-debug, pry, pry-db and so on and so forth. Which one should I use?
It stops there it should. But I don't know yet (and didn't find) how to watch the variable's value or how to go to the next line. And it always gives in a few seconds a kind of an error below:
pry(#)> Error: execution expired
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:600:in `getbyte'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:600:in `readline'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:600:in `block in readline'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:544:in `handle_read_errors'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:589:in `readline'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:369:in `retrieve_line'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:304:in `block in r'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:301:in `loop'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:301:in `r'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:271:in `re'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:251:in `rep'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:231:in `block (3 levels) in repl'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:230:in `loop'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:230:in `block (2 levels) in repl'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:229:in `catch'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:229:in `block in repl'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:228:in `catch'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_instance.rb:228:in `repl'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/pry_class.rb:154:in `start'
/var/lib/gems/1.9.1/gems/pry-0.9.10/lib/pry/core_extensions.rb:22:in `pry'
/home/alex/Documents/ruby/my_projects/controllers/my_controller.rb:24:in `block in '
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
/home/alex/Documents/ruby/my_projects/app.rb:21:in `block in '
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
/var/lib/gems/1.9.1/gems/rack-flash3-1.0.1/lib/rack/flash.rb:120:in `call'
/var/lib/gems/1.9.1/gems/rack-timeout-0.0.3/lib/rack/timeout.rb:16:in `block in call'
/usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
/var/lib/gems/1.9.1/gems/rack-timeout-0.0.3/lib/rack/timeout.rb:16:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/var/lib/gems/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:129:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/showexceptions.rb:21:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:99:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:64:in `block in call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `each'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/urlmap.rb:49:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/lint.rb:48:in `_call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/lint.rb:36:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/showexceptions.rb:24:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
/var/lib/gems/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/chunked.rb:43:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/var/lib/gems/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
pry(#)>
Alright, I install
gem "debugger"
and add the breakpoint
debugger
Nothing happens at all.
Log file is not what I'm looking for. I just want to debug a Sinatra application. How do I do that?
In your Gemfile:
gem "pry"
gem "pry-nav"
Run bundle. Then whenever the script encounters binding.pry, you will have a shell opened. You can see where you are with whereami, you move to the next line with next, continue running the script with continue. If you want to inspect a variable, just type its name.
See pry-nav for more info.
pry-debugger is better. and you need to run sinatra app under Modular style.
I created a repo at github which explains how to use pry to debug.
Here is the repo: https://github.com/hlee/sinatra_debugger_example
Difference between pry-debugger and pry-nav
Pry-debugger
if you check the source code of pry-debugger, you will see:
# pry-debugger.gemspec
# Dependencies
gem.required_ruby_version = '>= 1.9.2'
gem.add_runtime_dependency 'pry', '>= 0.9.10'
gem.add_runtime_dependency 'debugger', '~> 1.3'
gem.add_development_dependency 'pry-remote', '~> 0.1.6'
which means, pry-debugger Only supports MRI 1.9.2 and 1.9.3. Since it reliant on debugger, Pry-debugger will support faster tracing, breakpoints and more.
Pry-nav
on the other hand, here is pry-nav source:
# pry-nav.gemspec
# Dependencies
gem.required_ruby_version = '>= 1.8.7'
gem.add_runtime_dependency 'pry', '~> 0.9.10'
gem.add_development_dependency 'pry-remote', '~> 0.1.6'
which meas, pry-nav is a pure ruby approach not reliant on debugger. will supports 1.8.7, will not supports breakpoints and some other features from debugger.
Breakpoints
break SomeClass#run # Break at the start of `SomeClass#run`.
break Foo#bar if baz? # Break at `Foo#bar` only if `baz?`.
break app/models/user.rb:15 # Break at line 15 in user.rb.
break 14 # Break at line 14 in the current file.
break --condition 4 x > 2 # Change condition on breakpoint #4 to 'x > 2'.
break --condition 3 # Remove the condition on breakpoint #3.
break --delete 5 # Delete breakpoint #5.
break --disable-all # Disable all breakpoints.
break # List all breakpoints. (Same as `breakpoints`)
break --show 2 # Show details about breakpoint #2.
Note
pry-nav and pry-debugger cannot be loaded together.

Resources