Skip to content

Commit

Permalink
Fix crash on Android in retry logic after destroy called
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Aug 16, 2024
1 parent 973d1a0 commit 75a2bbd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,8 +83,12 @@ internal abstract class RetryableCallback<T>(
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.")
}
}

Expand Down

0 comments on commit 75a2bbd

Please sign in to comment.