games-hub: make session end updates idempotent
- Commit
3279a0b03ba3f5b0d6d082151181e13561181b11- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:41 UTC (28 days ago)
- Parents
43a5aaa
GamesHubProvider.updateSession recomputed duration and unconditionally added playtime plus a SESSION_COMPLETED activity row on every update, so a retried endSession (game-side retry, double lifecycle callback, process-death replay of a persisted sessionId) double-counted playtime and duplicated feed rows. The SDK client (GameHubClient.endSession) sends only END_TIME via update and startSession never sends END_TIME, so an update arriving for an already-ended session can only be a repeat. Return success without writing when existing.endTime is already set: the desired end state is already achieved, giving retries idempotent-success semantics. Verified with :games:hub:testDevUnitTest (17 tests, 0 failures; provider change covered by compilation plus unchanged suite). User-visible effect: per-game playtime totals and the activity feed no longer inflate when a game reports a session end more than once.
Diff
games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubUpdateHandlers.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubUpdateHandlers.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubUpdateHandlers.ktindex d0965cfae8..1d4d95b3ec 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubUpdateHandlers.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubUpdateHandlers.kt@@ -85,6 +85,10 @@ internal suspend fun GamesHubProvider.updateSession( ): Int { val sessionId = uri.pathSegments.lastOrNull() ?: return 0 val existing = repo.sessions.getSessionById(sessionId) ?: return 0+ // Idempotent end: a repeated update (game-side retry, double lifecycle+ // callback, process-death replay) must not add playtime or log completion+ // twice. An already-ended session stays ended.+ if (existing.endTime != null) return 1 val endTime = v.getAsLong(GameHubContract.Sessions.END_TIME) ?: v.getAsLong("end_time") ?: System.currentTimeMillis() val duration = (endTime - existing.startTime).coerceAtLeast(0L)