'Errno::ECONNRESET: Connection reset by peer in Rails using rest-client

We have a Ruby on Rails application and this has a "search" functionality (search for some company). From browser user key-in some name and hit search and this search make an rest api call to outside system and get us some search results.

We are using "rest-client" (for Ruby on Rails).

I noticed this seems to work for few hours and suddenly my search seems to be broken all of a sudden and I can see in my log I get:

Errno::ECONNRESET: Connection reset by peer

We tried to investigate this issue by looking in to logs and we dont see any logs.

If we need to make this search work again we need to restart the passenger and then it works immediately. This is happening only in production environment. I tested in staging it seems to work well.

Questions:

  1. What could be causing this "reset issue"
  2. Why on my prod passenger reset it starts to work again.
  3. We use reset-client should be write a code to manually close connection when this exception happens.
  4. Any issue in firewall could causing this?
  5. Is there any code I can place in the exception to restart this connection so the next call is success.

Code:

def call
   resp_data = RestClient.get(@request_url, @header)
   rescue => error
     puts 'Exception: ' error.message
end


Solution 1:[1]

Try to the following

resp_data = RestClient::Request.new(
    method: :get,
    url: @request_url, #=> https://api.example.com/auth2/endpoint
    :headers => {
        :Authorization => @header, #=> "Bearer access_token",
    }
)

rescue => error
    puts 'Exception: ' error.message

Solution 2:[2]

It's very wired. I met the same problem.

my script is shown as below: ( works great in my local machine, works greate in remote server, until someday the disk was full and this scripts dead, saying: Errno::ECONNRESET: Connection reset by peer)

ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'production'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'rails'
require 'rubygems'

require 'rest-client'

url = 'https://api-cn.faceplusplus.com/cardpp/v1/ocridcard'
id_card = File.expand_path(File.dirname(__FILE__) + "/id_card.jpg")

puts "== id_card: #{id_card}"
response = RestClient.post url,
  {
    api_key: 'Td_XijYBZUMp',
    api_secret: 'ihehEuWYwOWM',
    image_file: File.open(id_card, 'rb')
  }

puts "==response: "
puts response.inspect

my environment: ruby 2.5.0 , ubuntu server 16, with 8 core CPU, 50G ram

this problem happens when I found my hard disk was used 100%. no space left.

However, once I freed enough disk space, this problem exists.

after I restart my server, this problem exists.

when I run this script under rails, this problem exists.

However, when I run this script stand alone, it works fine.

so , finally, I turned to "curl" command. works great!

the working CURL script looks like:

$ curl -X POST "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard" \ 
-F "api_key=Td_XijYBCOYh-Rf_kCMj" \
-F "api_secret=iheWYoQcbCPM9n2VS" \
-F "image_file=@scripts/id_card.jpg

Solution 3:[3]

I'm late on this, but in my case the problem is that I was using AWS Redis for ElastiCache and in there, I had a cluster with a primary endpoint and read-only endpoint.

This error message can show up if the application is having problems connecting to redis!

I was using the read-only endpoint instead of the primary one, and the primary is used to write data to redis.

Taking a close look at my endpoint, it was something like application-name.qwerty-ro.cache.amazonaws.com:6379 and I changed it for application-name.qwerty.cache.amazonaws.com:6379 without the -ro part which is what made it read-only.

I lost about 6 hours trying to figure it out, so hope it helps someone else!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fool-dev
Solution 2
Solution 3 Eduardo Yutaka Nakanishi