Skip to content

Commit

Permalink
Add try catch blocks around executors.execute statements in SessionCo…
Browse files Browse the repository at this point in the history
…ntroller
  • Loading branch information
abhaysood committed Sep 26, 2023
1 parent 307de67 commit 2d4463f
Showing 1 changed file with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sh.measure.android.session

import android.os.StrictMode
import sh.measure.android.exceptions.MeasureException
import sh.measure.android.executors.BackgroundTaskRunner
import sh.measure.android.logger.LogLevel
import sh.measure.android.logger.Logger
import sh.measure.android.network.Transport
import sh.measure.android.storage.Storage
import java.util.concurrent.RejectedExecutionException

// TODO: write unit tests
/**
Expand All @@ -23,11 +25,6 @@ internal interface SessionController {
*/
fun createSession()

/**
* Deletes the [Session] with the given [sessionId].
*/
fun deleteSession(sessionId: String)

/**
* Deletes all [Session]s which did not end with a crash.
* This helps in removing stable sessions which we do not need to send to the server.
Expand Down Expand Up @@ -59,42 +56,58 @@ internal class SessionControllerImpl(
sessionProvider.createSession()
}

override fun deleteSession(sessionId: String) {
backgroundTaskRunner.execute {
storage.deleteSession(sessionId)
}
}

override fun deleteSessionsWithoutCrash() {
backgroundTaskRunner.execute {
storage.deleteSessionsWithoutCrash(sessionProvider.session.id)
try {
backgroundTaskRunner.execute {
storage.deleteSessionsWithoutCrash(sessionProvider.session.id)
}
} catch (e: RejectedExecutionException) {
logger.log(LogLevel.Debug, "Failed to delete sessions without crash", e)
} catch (e: NullPointerException) {
logger.log(LogLevel.Debug, "Failed to delete sessions without crash", e)
}
}

override fun syncSessions() {
backgroundTaskRunner.execute {
storage.getUnsyncedSessions().forEach { sessionId ->
logger.log(LogLevel.Debug, "Sending unsynced session report: $sessionId")
storage.getSessionReport(sessionId)?.let {
transport.sendSessionReport(it, object : Transport.Callback {
override fun onSuccess() {
storage.deleteSessionAndSignals(sessionId)
}
})
try {
backgroundTaskRunner.execute {
storage.getUnsyncedSessions().forEach { sessionId ->
logger.log(LogLevel.Debug, "Sending unsynced session report: $sessionId")
storage.getSessionReport(sessionId)?.let {
transport.sendSessionReport(it, object : Transport.Callback {
override fun onSuccess() {
storage.deleteSessionAndSignals(sessionId)
}
})
}
}
}
} catch (e: RejectedExecutionException) {
logger.log(LogLevel.Debug, "Failed to sync sessions", e)
} catch (e: NullPointerException) {
logger.log(LogLevel.Debug, "Failed to sync sessions", e)
}
}

override fun syncActiveSessionOnCrash(measureException: MeasureException) {
val activeSessionId = session.id
storage.saveUnhandledException(measureException.toSignal(activeSessionId))
storage.getSessionReport(activeSessionId)?.let {
transport.sendSessionReport(it, object : Transport.Callback {
override fun onSuccess() {
storage.deleteSessionAndSignals(activeSessionId)
}
})
// The write operation is performed synchronously to ensure it is completed before the
// process terminates in the event of a crash.
val oldPolicy = StrictMode.getThreadPolicy()
StrictMode.setThreadPolicy(StrictMode.allowThreadDiskWrites())
try {
val activeSessionId = session.id
storage.saveUnhandledException(measureException.toSignal(activeSessionId))
storage.getSessionReport(activeSessionId)?.let {
transport.sendSessionReport(it, object : Transport.Callback {
override fun onSuccess() {
storage.deleteSessionAndSignals(activeSessionId)
}
})
}
} catch (e: Exception) {
logger.log(LogLevel.Debug, "Failed to sync active session on crash", e)
} finally {
StrictMode.setThreadPolicy(oldPolicy)
}
}
}

0 comments on commit 2d4463f

Please sign in to comment.