telegram-bot-ruby icon indicating copy to clipboard operation
telegram-bot-ruby copied to clipboard

Crashes under bad network condition

Open shouya opened this issue 8 years ago • 13 comments

This exception raises when the network condition goes terrible. Is there a way to bypass the exception and force it to retry? Thank you in advance.

/usr/lib64/ruby/2.3.0/net/protocol.rb:44:in `connect_nonblock': SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A (Faraday::SSLError)
	from /usr/lib64/ruby/2.3.0/net/protocol.rb:44:in `ssl_socket_connect'
	from /usr/lib64/ruby/2.3.0/net/http.rb:928:in `connect'
	from /usr/lib64/ruby/2.3.0/net/http.rb:863:in `do_start'
	from /usr/lib64/ruby/2.3.0/net/http.rb:852:in `start'
	from /usr/lib64/ruby/2.3.0/net/http.rb:1384:in `request'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/adapter/net_http.rb:80:in `perform_request'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/adapter/net_http.rb:38:in `block in call'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/adapter/net_http.rb:85:in `with_net_http_connection'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/adapter/net_http.rb:33:in `call'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/request/url_encoded.rb:15:in `call'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/request/multipart.rb:15:in `call'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/rack_builder.rb:141:in `build_response'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/connection.rb:387:in `run_request'
	from /home/shou/.gem/ruby/2.3.0/gems/faraday-0.13.1/lib/faraday/connection.rb:174:in `post'
	from /home/shou/.gem/ruby/2.3.0/gems/telegram-bot-ruby-0.8.5/lib/telegram/bot/api.rb:72:in `call'
	from /home/shou/.gem/ruby/2.3.0/gems/telegram-bot-ruby-0.8.5/lib/telegram/bot/api.rb:60:in `method_missing'
	from /home/shou/.gem/ruby/2.3.0/gems/telegram-bot-ruby-0.8.5/lib/telegram/bot/client.rb:30:in `fetch_updates'
	from /home/shou/.gem/ruby/2.3.0/gems/telegram-bot-ruby-0.8.5/lib/telegram/bot/client.rb:25:in `listen'
	from hejmo-bot.rb:98:in `block in <main>'
	from /home/shou/.gem/ruby/2.3.0/gems/telegram-bot-ruby-0.8.5/lib/telegram/bot/client.rb:18:in `run'
	from /home/shou/.gem/ruby/2.3.0/gems/telegram-bot-ruby-0.8.5/lib/telegram/bot/client.rb:8:in `run'
	from hejmo-bot.rb:95:in `<main>'

shouya avatar Nov 15 '17 08:11 shouya

It shouldn't be a problem of my code, my code looks like just the standard message processing in the listen block:

token = 'SECRET_BOT_TOKEN'
Telegram::Bot::Client.run(token) do |bot|
  bot.api.get_updates(offset: -1) # eat up all previous update 
  bot.listen do |msg|
    # do stuff with msg
  end
end

Where in the error message, hejmo-bot.rb:95 corresponds to the Telegram::Bot::Client.run(token) line and hejmo-bot.rb:98 corresponds to the bot.listen line.

shouya avatar Nov 15 '17 08:11 shouya

hmmm are you using windows ? anyway you have two way to fix this error: 1: write this command in cmd set SSL_CERT_FILE=:c/path_to_certfile.pem or try this one: #101

eVanilla avatar Nov 15 '17 08:11 eVanilla

Thanks for the reply. I was using it under Linux, and I'm pretty sure it's not the certficate's problem.

Usually my script works all fine; but if I left it to run for a few minutes, exceptions like this might occur and crash the script.

shouya avatar Nov 15 '17 08:11 shouya

hmmm weird but it look like a SSLError ... did you checked your ssl certification?

eVanilla avatar Nov 15 '17 08:11 eVanilla

I don't really think it's about SSL certificate. I think it could be some packet losses happen when making requests so it failed the SSL handshake.

shouya avatar Nov 15 '17 08:11 shouya

no idea ... but it's not bad to try the 2nd way to fix this ... try it, maybe it worked

eVanilla avatar Nov 15 '17 08:11 eVanilla

So my temporary dirty fix towards this is writing code like this:

begin
  bot.listen do |msg|
    # do stuff with msg
  end
rescue
  retry
end

While it's very temporary and doesn't fix the problem at all. If the network error happens on other requests within the bot.listen block, it can't retry that specific request alone rather than restarting the whole listen process.

Anyway, thanks for the help though.

shouya avatar Nov 15 '17 08:11 shouya

yea i had these problems too ... i usually use this method .. yea it's not so good but ... it works

eVanilla avatar Nov 15 '17 08:11 eVanilla

Sounds like an issue with SSL version. Unfortunately, there is no way to configure it for now.

atipugin avatar Nov 15 '17 09:11 atipugin

Catching the exception and further investigating what is causing the issue is probably the right thing to do. Instead of catching all the exceptions like you are doing now, you could try catching only the Faraday SSL exception and handle it properly. What "properly" means depends on the specific context. An idea could be to store the messages in some data structure (array?) and process them later when the connection is stable again. In addition you could add a timeout instead of retrying immediately. Just be careful not to block the bot with too long timeouts.

cippaciong avatar Nov 15 '17 09:11 cippaciong

hmmm are you using windows ? anyway you have two way to fix this error: 1: write this command in cmd set SSL_CERT_FILE=:c/path_to_certfile.pem or try this one: #101

i wrote that on my cmd on window and it did nothing

chinweokwu avatar May 31 '20 16:05 chinweokwu

i wrote that on my cmd on window and it did nothing

Did you replace c/path_to_certfile.pem with path to your cert file?

alexander-saratovcev avatar May 31 '20 18:05 alexander-saratovcev

i wrote that on my cmd on window and it did nothing

Did you replace c/path_to_certfile.pem with path to your cert file?

how can i know the path to my cert file pls

chinweokwu avatar May 31 '20 20:05 chinweokwu

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Feb 19 '23 15:02 github-actions[bot]