0

So I've working on a website on Ruby on Rails that uses the Geocoder gem.

Here is my implementation:

def User < ActiveRecord::Base    
    before_save :geocode
    geocoded_by :address do |usr, geocode_results|
    if geo = geocode_results.first
       usr.latitude  = geo.latitude
       usr.longitude = geo.longitude
       usr.geocode_address = geo.address
    else
      usr.latitude  = nil
      usr.longitude = nil
      usr.geocode_address = nil
    end
end

And on production initially it works... But in 2-3 days I'll get an error saying undefined method to_sym on nil:NilClass. on any controller action that calls @user.save

I investigated further, and here is the stack trace:

Stacktrace (most recent call first):   
kernel/delta/kernel.rb:78:in `to_sym (method_missing)'
rubysl/net/http/http.rb:576:in `start'
   key = $1.to_sym
kernel/common/enumerable.rb:217:in `grep'
kernel/bootstrap/array.rb:76:in `each'
kernel/common/enumerable.rb:213:in `grep'
rubysl/net/http/http.rb:575:in `start'
    http.methods.grep(/\A(\w+)=\z/) do |meth|
geocoder/lookups/base.rb:268:in `make_api_request'
    http_client.start(uri.host, uri.port, use_ssl: use_ssl?) do |client|
rubysl/timeout/timeout.rb:149:in `timeout'
    yield sec
rubysl/timeout/timeout.rb:168:in `timeout'
     Timeout.timeout(n, e, &block)
geocoder/lookups/base.rb:266:in `make_api_request'
    timeout(configuration.timeout) do
geocoder/lookups/base.rb:218:in `fetch_raw_data'
    response = make_api_request(query)
geocoder/lookups/base.rb:169:in `fetch_data'
    parse_raw_data fetch_raw_data(query)
geocoder/lookups/google.rb:28:in `results'
    return [] unless doc = fetch_data(query)
geocoder/lookups/base.rb:47:in `search'
    results(query).map{ |r|
geocoder/query.rb:11:in `execute'
    lookup.search(text, options)
geocoder.rb:21:in `search'
    query.blank? ? [] : query.execute
geocoder/stores/base.rb:111:in `do_lookup' 
    results = Geocoder.search(query, query_options)
geocoder/stores/active_record.rb:259:in `geocode'
    do_lookup(false) do |o,rs|   active_support/callbacks.rb:424:in `make_lambda'
    lambda { |target, _, &blk| target.send filter, &blk }

It turns out that this is the culprit on the to_sym for nil:NilClass error - it seems that the grep isn't finding anything?

The Net:HTTP is being called by Rails Geocoder here.

I suspect its an rbx-2.2.1 issue, so I'm gonna try switching to ruby-2.2.1 and see if it happens in the next couple days. Has this ever happened to anyone?

Edit: If I restart my rails server thread the problem goes away, but will come back in ~1-2 days

Edit: this is the user_controller that caused the issue

respond_to do |format| #line 169
    @id = @user.id
    @user.update(user_params) #line 171
    if params[:user][:redirect_to_index].present? or @user.chef?
        format.html { redirect_to root_path }
        format.json { render :show, status: :ok, location: @user }
    else
        format.html { redirect_to @user }
        format.json { render :show, status: :ok, location: @user }
    end
end
cozos
  • 787
  • 10
  • 19
  • It's really hard to read that "stacktrace"... maybe you could clean it up? – Taryn East Jul 09 '15 at 04:28
  • Hey, sorry about that - what's the preferred way of showing a stack trace? Maybe I can remove the irrelevant parts – cozos Jul 09 '15 at 04:29
  • I dunno - I just can't really tell when one line ends and another begins :P It looks like it's wrapped the lines. If you can unwrap the lines, then just use the clickable "code" thingie (rather than using < code > tags or >) to make it into code it might be easier to decode :) – Taryn East Jul 09 '15 at 04:30
  • Sure give me a minute to do it – cozos Jul 09 '15 at 04:33
  • I've code-ified them... but it still looks like the lines were wrapped before you pasted them in here? There's intermingled code and stacktrace... – Taryn East Jul 09 '15 at 04:33
  • I'm looking for the part that shows where in your code the error springs from... so we can see if there's anything we can do about it from your perspective (apart from just updating gems to different versions) – Taryn East Jul 09 '15 at 04:35
  • Sure I can add the controller code – cozos Jul 09 '15 at 04:41
  • So... can you edit your question and add the relevant part of the users-controller (and show us which lines are 169/171) :) – Taryn East Jul 09 '15 at 04:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82771/discussion-between-cozos-and-taryn-east). – cozos Jul 09 '15 at 04:49
  • Sorry - I'm checking this out asynchronously while I'm working... I don't have time for a synchronous chat. What I've asked for should be a part of your original question (relevant code and all ;)) – Taryn East Jul 09 '15 at 04:51
  • OOC - what happens if you just remove the block... does it all work? how do the results differ from what you want? (and by "work" I mean "fail to explode", not "do everything you want") – Taryn East Jul 09 '15 at 04:54
  • Hey Taryn, if I remove line #171, the line of code that does @user.update then the controller action will work. I believe that the problem stems from the Geocoder that is triggered by any save or update call on the User model. However, the stack trace indicates a ruby exception that occurs when the Geocoder gem is attempting to establish an HTTP connection to the google maps API. The error occurs here: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L577. – cozos Jul 09 '15 at 05:07
  • so if you remove the block from `geocode_by :address` - does it still explode? – Taryn East Jul 09 '15 at 05:36

1 Answers1

0

I fixed the issue by switching from rbx-2.2.1 to ruby-2.2.1. It seems to be caused by some bug between geocoder and rbx-2.2.1 (I think).

cozos
  • 787
  • 10
  • 19