go-telegram / bot

Telegram Bot API Go framework
MIT License
669 stars 60 forks source link

The Telegram bot library for Golang is not shutting down gracefully. #121

Open suleymanmyradov opened 4 hours ago

suleymanmyradov commented 4 hours ago

Problem:

The Telegram bot library for Golang is not shutting down gracefully. When I stop the bot and restart it, I encounter conflict errors: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running.

Expected Behavior:

The bot should shut down cleanly, releasing any resources it's using. On restart, it should start without any conflicts.

Steps to Reproduce:

  1. Run the provided Golang code.
  2. Press Ctrl+C to stop the bot.
  3. Run the code again.
  4. Observe the conflict errors in the console output.

Code Snippet:

var configFile = flag.String("f", "../etc/bot.yaml", "the config file")

func main() {
    flag.Parse()

    ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    var cfg config.Config
    conf.MustLoad(*configFile, &cfg)

    svcCtx, err := svc.NewServiceContext(&cfg)
    if err != nil {
        fmt.Println("the error:", err)
        panic(fmt.Sprintf("failed to create service context: %v", err))
    }

    b, err := bot.New(svcCtx.Config.BotToken, bot.WithServerURL(svcCtx.Config.StartBotApi))
    if err != nil {
        panic(fmt.Sprintf("failed to create bot: %v", err))
    }
    fmt.Println("Bot started")

    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        b.Start(ctx)
    }()
    wg.Wait()
    <-ctx.Done()
    fmt.Println("Shutting down the bot gracefully...")

    fmt.Println("Bot stopped")
}

Errors

➜  cmd git:(main) ✗ go run .
Bot started
^CShutting down the bot gracefully...
Bot stopped
{"@timestamp":"2024-09-30T10:50:37.996+05:00","caller":"proc/shutdown.go:58","content":"Got signal 2, shutting down...","level":"info"}
➜  cmd git:(main) ✗ go run .
Bot started
2024/09/30 10:50:39 [TGBOT] [ERROR] error get updates, conflict, Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
2024/09/30 10:50:39 [TGBOT] [ERROR] error get updates, conflict, Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
2024/09/30 10:50:40 [TGBOT] [ERROR] error get updates, conflict, Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
2024/09/30 10:50:40 [TGBOT] [ERROR] error get updates, conflict, Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
2024/09/30 10:50:41 [TGBOT] [ERROR] error get updates, conflict, Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
negasus commented 2 hours ago

This means that at the moment another Bot has been launched. Make sure the application with a similar bot token is no longer running anywhere