From f97575405881a992d9978d8bced5f5c5e3ea156e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 6 Aug 2025 13:14:22 +0600 Subject: [PATCH] try to fix cancelling on timeout for long polling --- CHANGELOG.md | 3 +++ .../utils/updates/retrieving/LongPolling.kt | 25 +++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ace938af0..d2b9bfd9bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 27.1.2 +* `Core`: + * Try to fix cancelling on timeout for long polling + ## 27.1.1 * `Version`: diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index da18e877a6..b59dc5a4c0 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.extensions.utils.updates.retrieving +import dev.inmo.kslog.common.logger import dev.inmo.micro_utils.coroutines.* import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.bot.TelegramBot @@ -107,17 +108,13 @@ fun TelegramBot.longPollingFlow( lastUpdateIdentifier = update.updateId } } - }.onFailure { - it.causedCancellationException() ?.let { - cancel(it) - } } } } withContext(contextToWork) { while (isActive) { - runCatching { + runCatchingLogging(logger = Log) { execute( GetUpdates( offset = lastUpdateIdentifier?.plus(1), @@ -128,14 +125,16 @@ fun TelegramBot.longPollingFlow( updatesHandler(originalUpdates) } }.onFailure { e -> - val isHttpRequestTimeoutException = - e is HttpRequestTimeoutException || (e is CommonBotException && e.cause is HttpRequestTimeoutException) - if (isHttpRequestTimeoutException && autoSkipTimeoutExceptions) { - return@onFailure - } - exceptionsHandler?.invoke(e) - if (e is RequestException) { - delay(1000L) + runCatchingLogging(logger = Log) { + val isHttpRequestTimeoutException = + e is HttpRequestTimeoutException || (e is CommonBotException && e.cause is HttpRequestTimeoutException) + if (isHttpRequestTimeoutException && autoSkipTimeoutExceptions) { + return@onFailure + } + exceptionsHandler?.invoke(e) + if (e is RequestException) { + delay(1000L) + } } } }