Skip to content

Commit b12b5d9

Browse files
Fix race when persisting Termux output for next bubble
1 parent a17f2e2 commit b12b5d9

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/util/TermuxOutputPreferences.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ object TermuxOutputPreferences {
1111
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
1212
val existing = prefs.getString(KEY_PENDING_OUTPUT, "").orEmpty()
1313
val merged = if (existing.isBlank()) output else "$existing\n\n$output"
14-
prefs.edit().putString(KEY_PENDING_OUTPUT, merged).apply()
14+
val committed = prefs.edit().putString(KEY_PENDING_OUTPUT, merged).commit()
15+
if (!committed) {
16+
throw IllegalStateException("Failed to persist pending Termux output")
17+
}
1518
}
1619

1720
fun consumeOutput(context: Context): String? {
1821
val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
1922
val value = prefs.getString(KEY_PENDING_OUTPUT, "").orEmpty().trim()
2023
if (value.isBlank()) return null
21-
prefs.edit().remove(KEY_PENDING_OUTPUT).apply()
24+
val committed = prefs.edit().remove(KEY_PENDING_OUTPUT).commit()
25+
if (!committed) {
26+
throw IllegalStateException("Failed to clear consumed Termux output")
27+
}
2228
return value
2329
}
2430
}

0 commit comments

Comments
 (0)