From 75a2bbdb4ba08cac11f0776561dcb2eea42540be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Kalici=C5=84ski?= Date: Fri, 16 Aug 2024 16:11:16 +0200 Subject: [PATCH] Fix crash on Android in retry logic after destroy called --- .../presence/eventengine/effect/WaitEffect.kt | 13 +++++++++---- .../com/pubnub/internal/retry/RetryableCallback.kt | 9 +++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/presence/eventengine/effect/WaitEffect.kt b/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/presence/eventengine/effect/WaitEffect.kt index bb59cc33a..e0c859fc3 100644 --- a/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/presence/eventengine/effect/WaitEffect.kt +++ b/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/presence/eventengine/effect/WaitEffect.kt @@ -5,6 +5,7 @@ import com.pubnub.internal.eventengine.Sink import com.pubnub.internal.extension.scheduleWithDelay import com.pubnub.internal.presence.eventengine.event.PresenceEvent import org.slf4j.LoggerFactory +import java.util.concurrent.RejectedExecutionException import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.ScheduledFuture import kotlin.time.Duration @@ -29,10 +30,14 @@ internal class WaitEffect( return } - scheduled = - executorService.scheduleWithDelay(heartbeatInterval) { - presenceEventSink.add(PresenceEvent.TimesUp) - } + try { + scheduled = + executorService.scheduleWithDelay(heartbeatInterval) { + presenceEventSink.add(PresenceEvent.TimesUp) + } + } catch (_: RejectedExecutionException) { + log.trace("Unable to schedule retry, PubNub was likely already destroyed.") + } } @Synchronized diff --git a/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/retry/RetryableCallback.kt b/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/retry/RetryableCallback.kt index 34c57120c..19f8a6d69 100644 --- a/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/retry/RetryableCallback.kt +++ b/pubnub-core/pubnub-core-impl/src/main/kotlin/com/pubnub/internal/retry/RetryableCallback.kt @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory import retrofit2.Call import retrofit2.Callback import retrofit2.Response +import java.util.concurrent.RejectedExecutionException import java.util.concurrent.ScheduledExecutorService import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds @@ -82,8 +83,12 @@ internal abstract class RetryableCallback( val effectiveDelay: Duration = delay + randomDelayInMillis.milliseconds log.trace("Added random delay so effective retry delay is ${effectiveDelay.inWholeMilliseconds} millis") // don't want to block the main thread in case of Android so using executorService - executorService.scheduleWithDelay(effectiveDelay) { - call.clone().enqueue(this) + try { + executorService.scheduleWithDelay(effectiveDelay) { + call.clone().enqueue(this) + } + } catch (_: RejectedExecutionException) { + log.trace("Unable to schedule retry, PubNub was likely already destroyed.") } }