cjheath/geoip

Very weird bug. Sometime GEO_IP_CITY.city returns incorrect info

Eastknight opened this issue · 1 comments

I've been using this gem for a long time. It worked just fine until recently I found a very weird bug when I try to track requests from Africa to my website.

My ruby code is like this:

# In class ApplicationController < ActionController::Base

after_filter :monitor_african_requests

def monitor_african_requests
    request_ip = request.ip
    geo_city = GEO_IP_CITY.city(request_ip)
    user = current_user
    condition = user && geo_city.is_a?(GeoIP::City) && geo_city.continent_code == 'AF'
    if condition
      log = "LOG for African Request: geo_city: #{geo_city.inspect}; IP: #{request_ip.inspect}"
      Rails.logger.info log
    end
end

This works for some request but can not find the correct location info for some other requests. For example, this is one of the output in my log file:

LOG for African Request: geo_city: #<struct GeoIP::City request="135.0.174.69", ip="135.0.174.69", country_code2="DZ", country_code3="DZA", country_name="Algeria", continent_code="AF", region_name="09", city_name="Oran", postal_code="", latitude=35.6911, longitude=-0.641699999999986, dma_code=nil, area_code=nil, timezone="Africa/Algiers">; IP: "135.0.174.69"

It shows this request is from Africa, but in my Rails console, I tried the same code, it turned out to be a Canadian IP.

GEO_IP_CITY.city("135.0.174.69")
=> #<struct GeoIP::City request="135.0.174.69", ip="135.0.174.69", country_code2="CA", country_code3="CAN", country_name="Canada", continent_code="NA", region_name="ON", city_name="Toronto", postal_code="M2J", latitude=43.7685, longitude=-79.3584, dma_code=nil, area_code=nil, timezone="America/Rainy_River">

Any idea why this could happen? I'll appreciate any help.

My Rails version is 2.3.18 and the geoip version is "~> 4.0.1".

Are you sure you're using the same GeoIP database file?
Have you recently changed to a multi-threaded web server?
GeoIP is not thread-safe unless your operating system provides "pread", or you use the :preload option.
Otherwise it shares the open file pointer.

Try (for an experiment) to open a local instance of the GeoIP instance for each request, and close it again after that request - see if the same problem occurs. Or use pread or :preload