Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endless loop in IUpdateHandler, HandleErrorAsync #1104

Open
depler opened this issue Dec 4, 2021 · 0 comments · May be fixed by #1343
Open

Endless loop in IUpdateHandler, HandleErrorAsync #1104

depler opened this issue Dec 4, 2021 · 0 comments · May be fixed by #1343

Comments

@depler
Copy link

depler commented Dec 4, 2021

Here is code sample of IUpdateHandler implementation:

public class TelegramHandler : IUpdateHandler
    {
        public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
        {
            try
            {
                //disable internet connection at this point

                if (update.Type == UpdateType.Message)
                    await botClient.SendTextMessageAsync(update.Message.Chat.Id, "test"); 
            }
            catch { }
        }

        public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
        {
            //this will be called in endless loop, while internet is off
            Console.WriteLine("error");
            return Task.CompletedTask;
        }
    }

Now, do the following:

  1. Put breakpoint at line if (update.Type == UpdateType.Message)
  2. Send message to the bot to hit breakpoint
  3. Disable your internet connection
  4. Release breakpoint
  5. Watch at console output and CPU load 😊

This happens because of call _botClient.MakeRequestAsync inside loop:
image

Which is going to exception handler:
image

Probably some workaround can be done by catching HttpRequestException and invoking Task.Delay(1000)

@depler depler changed the title Endless loop in error handler Endless loop in error IUpdateHandler, HandleErrorAsync Dec 4, 2021
@depler depler changed the title Endless loop in error IUpdateHandler, HandleErrorAsync Endless loop in IUpdateHandler, HandleErrorAsync Dec 4, 2021
@karb0f0s karb0f0s transferred this issue from TelegramBots/Telegram.Bot Jan 10, 2022
@tuscen tuscen transferred this issue from TelegramBots/Telegram.Bot.Extensions.Polling Jun 18, 2022
@karb0f0s karb0f0s linked a pull request Jan 10, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant