xooniverse / televerse

Your gateway to seamless Telegram Bot Development 💙
https://televerse.xooniverse.com
BSD 3-Clause "New" or "Revised" License
64 stars 11 forks source link

Bot stopping randomly #134

Closed doiraduc closed 1 year ago

doiraduc commented 1 year ago

After a random amount of time (hours or even days), the bot crashes. Sometimes, there's no error or message in the console, it just exits. Other times, there are errors like

We are doubling the retry delay to 2 seconds
Unhandled exception:
ClientException: Broken pipe, uri=https://api.telegram.org/bot<TOKENHERE>/getUpdates
#0      IOClient.send (package:http/src/io_client.dart:121)
<asynchronous suspension>
#1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:93)
<asynchronous suspension>
#2      _withClient (package:http/http.dart:166)
<asynchronous suspension>
#3      HttpClient.postURI (package:televerse/src/utils/http.dart:27)
<asynchronous suspension>
#4      RawAPI.getUpdates (package:televerse/src/televerse/raw_api.dart:88)
<asynchronous suspension>
#5      LongPolling._poll (package:televerse/src/televerse/fetch/long_polling.dart:81)
<asynchronous suspension>
#6      LongPolling.start (package:televerse/src/televerse/fetch/long_polling.dart:66)
<asynchronous suspension>
#7      Televerse.start (package:televerse/src/televerse/televerse.dart:275)
<asynchronous suspension>

Do note that in the error I included above, I hid my bot's token with < TOKENHERE >.

Usually, restarting the bot does the trick, only to crash again after another random amount of time (usually 8-10 hours or 2-5 days).

HeySreelal commented 1 year ago

Hey, thanks for the report. That sounds weird to me and looks like something I've never encountered before.

I'll check it and follow up on this thread. I see that this is something from the HTTP package - ClientException is thrown when a request fails.

HeySreelal commented 1 year ago

Btw, can you please tell me if you have attached an error handler using the Televerse.onError method?

doiraduc commented 1 year ago

Hi, sorry for the late response. No, I didn't set the onError handler.

doiraduc commented 1 year ago

If it helps, in case I will get more errors like this I'll post them here.

HeySreelal commented 1 year ago

No problem and thanks for the response.

Sure, more logs will surely be helpful. Also, please try attaching a error handler with Televerse.onError method.

All of the exceptions happening around the library code or user defined handlers will be caught there. That's really sounds like your dream crash free bot. Let's see if this issue one breaks the laws of Televerse :)

HeySreelal commented 1 year ago

Hey, just wanted to follow up on the issue. Have you seen it occurring again? If so, please let me know the details :)

doiraduc commented 1 year ago

Hi, for the moment, I've been working on the bot, and tomorrow, I will leave it running for at least 5 days without stopping it to see if it crashes again. I'll certainly let you know if it does or it doesn't.

devsdocs commented 1 year ago

Got the same issue here, happen to all my bots, no logs whatsoever, maybe trying to use dio instead could help fix the issue.

HeySreelal commented 1 year ago

I'm pretty sure that it anything happens around long polling gets failed it will be caught on onError handler.

So the idea of no logs is kind of a wonder to me.

@devsdocs were you using onError handler to catch unexpected errors?

devsdocs commented 1 year ago

yep, got nothing, my code below:

bot.onError((err, st) async { errorExist(err, st); });

void errorExist(dynamic err, dynamic st) { String? data; if (err is TelegramException) { data = 'Telegram Exception: ${err.code}\n${err.description}\n${err.parameters}\n${err.stackTrace}'; } else if (err is TeleverseException) { data = 'Televerse Exception: ${err.description}\n${err.message}\n${err.stackTrace}'; } else if (err is LongPollingException) { data = 'Longpolling Exception: ${err.code}\n${err.message}\n${err.parameters}\n${err.stackTrace}'; } else { data = 'Error: $err'; } bot.api.sendMessage(BotConfig.logChat, '$data\nStack: $st'); }

HeySreelal commented 1 year ago

Wow that's weird. So the bot was unexpectedly stopped even without any log? If it was an uncaught exception at least the Dart process will log something...

Can you check any other possibilities for the process to be terminated?

HeySreelal commented 1 year ago

Hey @iamcosmin @devsdocs, I've merged PR #139 and pushed #142 to release - we're now using dio instead of http. (Thanks @devsdocs) 🚀

Please note that there's a breaking change around Televerse.onError handler. The handler should now accept a single BotError instance instead of an err and stackTrace.

This is to also include the context inside the error which will possibly help you to auto-retry the requests (as discussed in the chat group)

Please let me know if this issue happens again... And also, feel free to share more logs if you find any.

devsdocs commented 1 year ago

You're Welcome @HeySreelal. I'll post if any problem occur. Thank you.

HeySreelal commented 1 year ago

Hey, just checking in - is this still up?

doiraduc commented 1 year ago

On my end I haven't experienced this anymore.

HeySreelal commented 1 year ago

Thankss for confirming. So we can close this?

doiraduc commented 1 year ago

I think so.

HeySreelal commented 1 year ago

Perfect! Closing this now. Thanks again :)