Skip to content

Commit

Permalink
Discard session after exception to clear up storage
Browse files Browse the repository at this point in the history
Storage leaks can still happen if session commits have errors (for
instance, because the device was shutdown during installation), as those
sessions can still be reopened and retried according to docs. However,
we may expect all dangling sessions to be cleared after a certain
timespan in the magnitude of a day.
  • Loading branch information
fynngodau committed Sep 26, 2024
1 parent 352260d commit 2109e46
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ private suspend fun Context.installPackagesInternal(
try {
sessionId = packageInstaller.createSession(params)
session = packageInstaller.openSession(sessionId)
componentNames.forEach { component ->
for (component in componentNames) {
session.openWrite(component, 0, -1).use { outputStream ->
writeComponent(component, outputStream)
session.fsync(outputStream)
session!!.fsync(outputStream)
}
}
val deferred = CompletableDeferred<Unit>()
Expand All @@ -127,13 +127,17 @@ private suspend fun Context.installPackagesInternal(

emitProgress(CommitingSession)
session.commit(pendingIntent.intentSender)
// don't abandon if `finally` step is reached after this point
session = null

Log.d(TAG, "installPackages session commit")
return deferred.await()
} catch (e: IOException) {
Log.w(TAG, "Error installing packages", e)
throw e
} finally {
session?.close()
Log.d(TAG, "Discarding session after error")
// discard downloaded data
session?.abandon()
}
}

0 comments on commit 2109e46

Please sign in to comment.