atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.35k stars 217 forks source link

Periodic Net::ReadTimeout errors causing restart for 24/7 Bot #276

Closed Ifiht closed 1 year ago

Ifiht commented 1 year ago

This issue is very sporadic, but today it happened twice within only 7 hours. Probably the only reason I notice is because I run my telegram bot using PM2, and I get alerts anytime it restarts without manual interaction.

These restarts are happening at times when the bot is not receiving any inputs from either chat users or the machine it lives on. Here's the full error log:

/home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/protocol.rb:219:in `rbuf_fill': Net::ReadTimeout with #<TCPSocket:(closed)> (Net::ReadTimeout)
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/protocol.rb:193:in `readuntil'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/protocol.rb:203:in `readline'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/http/response.rb:42:in `read_status_line'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/http/response.rb:31:in `read_new'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/http.rb:1609:in `block in transport_request'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/http.rb:1600:in `catch'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/http.rb:1600:in `transport_request'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/http.rb:1573:in `request'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-net_http-3.0.2/lib/faraday/adapter/net_http.rb:113:in `block in request_with_wrapped_block'
        from /home/ifiht/.rvm/rubies/ruby-3.1.3/lib/ruby/3.1.0/net/http.rb:985:in `start'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-net_http-3.0.2/lib/faraday/adapter/net_http.rb:112:in `request_with_wrapped_block'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-net_http-3.0.2/lib/faraday/adapter/net_http.rb:102:in `perform_request'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-net_http-3.0.2/lib/faraday/adapter/net_http.rb:66:in `block in call'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-2.7.4/lib/faraday/adapter.rb:45:in `connection'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-net_http-3.0.2/lib/faraday/adapter/net_http.rb:65:in `call'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-2.7.4/lib/faraday/request/url_encoded.rb:25:in `call'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-multipart-1.0.4/lib/faraday/multipart/middleware.rb:28:in `call'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-2.7.4/lib/faraday/rack_builder.rb:153:in `build_response'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-2.7.4/lib/faraday/connection.rb:444:in `run_request'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/faraday-2.7.4/lib/faraday/connection.rb:280:in `post'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/telegram-bot-ruby-1.0.0/lib/telegram/bot/api.rb:59:in `call'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/telegram-bot-ruby-1.0.0/lib/telegram/bot/api.rb:46:in `method_missing'
        from standalone.rb:15:in `block in <main>'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/telegram-bot-ruby-1.0.0/lib/telegram/bot/client.rb:20:in `run'
        from /home/ifiht/.rvm/gems/ruby-3.1.3/gems/telegram-bot-ruby-1.0.0/lib/telegram/bot/client.rb:10:in `run'
        from standalone.rb:14:in `<main>'

Seems like there may be an issue between the ruby libraries and Telegram when run for an extended period of time? Is this something anyone else is seeing, could it be handled in the bot code?

atipugin commented 1 year ago

Hi @Ifiht,

looks like it's just a network issue and not related to this gem. I suggest you to rescue Faraday::TimeoutError and try to make request again. Something like this: https://github.com/atipugin/telegram-bot-ruby/blob/master/lib/telegram/bot/client.rb#L31-L40

github-actions[bot] commented 1 year ago

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.

Ifiht commented 1 year ago

@atipugin thanks for the suggestion, I was hoping to fix the problem and update here but haven't been able to find time to test - closing for now but if I find anything that could help the gem I'll post back.