From 4c2089d62ebc2c8d19e9a9cb1a37035967509fc6 Mon Sep 17 00:00:00 2001 From: Waozi Date: Thu, 15 Oct 2026 22:27:39 -0300 Subject: [PATCH] Keep sync from getting stuck on Review sync changes Android gives the app no writable temporary directory, and the debug and Play builds changed into the release package's files path, which failed and left the process in "/". Any SQLite statement journal or temporary table that spilled to a file failed with "disk I/O error", rolled back the whole transaction, and made every review merge fail, so the review window came back each time it closed. Temporary data now stays in memory on every connection, Android changes into the package's own files directory, a step failure logs SQLite's reason, and a review is merged or shown once per account snapshot instead of reopening right after it closes. --- CHANGELOG.md | 1 + src/app/app_sync.zi | 10 +++++++++- src/app/sync_safety_policy.zi | 8 ++++++++ src/platform/main_platform.zi | 8 +++++--- src/storage/schema_sql.zi | 5 +++++ src/storage/sqlite.zi | 10 +++++++++- src/storage/storage_core.zi | 3 ++- src/storage/sync_review.zi | 9 +++++++++ tests/storage_sync_behavior.zi | 19 +++++++++++++++++++ tests/sync_safety_behavior.zi | 7 +++++++ 10 files changed, 74 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 755038315..837219c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ ### Removed - Donations: the reminder, About's Donate button and Lumi's donation links. Dew is how you pay in Inner Breeze. ### Fixed +- Sync no longer gets stuck on Review sync changes: Android merges large changes again, and a closed review stays closed until your account has newer data. - Android: closing the floating practice window with its X ends the practice. - Practice downloads show their real size, and each practice's pictures come only with its download. diff --git a/src/app/app_sync.zi b/src/app/app_sync.zi index 441de576b..8b6046f6b 100644 --- a/src/app/app_sync.zi +++ b/src/app/app_sync.zi @@ -16,6 +16,7 @@ app_reload_after_import :: (app: *InnerBreeze, reload_settings: s32) #foreign c #import, file "../platform/environment.zi"; storage_sync_review_pending :: () -> s32 #foreign c "storage_sync_review_pending"; storage_sync_review_merge_pending :: () -> s32 #foreign c "storage_sync_review_merge_pending"; +storage_sync_review_version :: () -> s64 #foreign c "storage_sync_review_version"; app_open_modal :: (app: *InnerBreeze, modal_type: ModalType) #foreign c "app_open_modal"; PROFILE_LEADERBOARD_STREAK :: 0 PROFILE_LEADERBOARD_AVG_HOLD :: 1 @@ -1106,7 +1107,10 @@ app_sync_retry_ready :: (result: s32, now: float64) -> bool { } sync_review_cache: s32; +sync_review_cache_version: s64; sync_review_cache_revision: s64 = -1; +// The account snapshot whose review was last merged or shown in this run. +sync_review_handled_version: s64 = -1; // Whether a sync review waits, read again only after stored data changes. app_sync_review_pending :: () -> s32 { @@ -1114,6 +1118,7 @@ app_sync_review_pending :: () -> s32 { if revision != sync_review_cache_revision { sync_review_cache_revision = revision sync_review_cache = storage_sync_review_pending() + sync_review_cache_version = storage_sync_review_version() } return sync_review_cache } @@ -1166,7 +1171,10 @@ app_sync_pump :: (app: *InnerBreeze) { app_sync_record_input(app, now) app_collect_finished_sync() app_collect_finished_social_refresh(app) - if app_sync_review_pending() != 0 && app_sync_ui_update_safe(app, now) != 0 { + if SyncReviewDue(app_sync_review_pending() != 0, + app_sync_ui_update_safe(app, now) != 0, + sync_review_cache_version, sync_review_handled_version) { + sync_review_handled_version = sync_review_cache_version if storage_sync_review_merge_pending() != 0 { app_reload_after_import(app, 0) } else { diff --git a/src/app/sync_safety_policy.zi b/src/app/sync_safety_policy.zi index 571f2ba3f..a4b0b616a 100644 --- a/src/app/sync_safety_policy.zi +++ b/src/app/sync_safety_policy.zi @@ -62,3 +62,11 @@ SyncUiRefreshSafe :: (state: SyncSafetyState, now: float64, return BackgroundSyncSafe(state) && !SyncScreenBlocksRefresh(state.screen) && SyncUserIdle(now, last_input_at, input_active) && state.screen != ScreenStart } + +// A waiting sync review is merged, or shown, once per account snapshot. When +// the merge fails or the person closes the review, it waits for newer account +// data or the next start, so a review that cannot apply never traps the app. +SyncReviewDue :: (pending: bool, ui_safe: bool, snapshot_version: s64, + handled_version: s64) -> bool { + return pending && ui_safe && snapshot_version != handled_version +} diff --git a/src/platform/main_platform.zi b/src/platform/main_platform.zi index e94d6c53f..38d05f985 100644 --- a/src/platform/main_platform.zi +++ b/src/platform/main_platform.zi @@ -66,6 +66,7 @@ MAIN_SDL :: #defined(NATIVE_WINDOW_HAVE_SDL) && !#defined(PLATFORM_WEB) && !#def #if #defined(ANDROID_BUILD) { #import "android_surface" + #import "android_jni" AndroidInsetsInit :: () #foreign c "android_insets_init"; AndroidWakelockInit :: () #foreign c "android_wakelock_init"; android_log :: #system_library "log"; @@ -304,9 +305,10 @@ native_before_window :: () { AndroidInsetsInit() SetAndroidViewportPolicy(AndroidViewportPolicyResizeForIme()) AndroidWakelockInit() - path: [64]u8 - if CopyCString("/data/user/0/xyz.waozi.inbe/files", path[:]) && - !ChangeDirectory(*path[0]) { + // The package's own files directory: the debug and Play builds use + // other application IDs than the release path. + files := AndroidInternalDataPath() + if files == null || !ChangeDirectory(files) { TraceLog(4, "APP: failed to switch to Android files directory") } } diff --git a/src/storage/schema_sql.zi b/src/storage/schema_sql.zi index 2e8870883..282834160 100644 --- a/src/storage/schema_sql.zi +++ b/src/storage/schema_sql.zi @@ -1,16 +1,21 @@ // SQL for current database creation and data-preserving upgrades. // The upgrade queries retain the older layouts used by installed versions. +// Temporary tables and statement journals stay in memory: an Android app has +// no writable temporary directory, so a large sync merge that spilled to a +// file failed with "disk I/O error" and rolled back the whole transaction. SchemaWebPragma :: #string SQL PRAGMA journal_mode=WAL; PRAGMA synchronous=OFF; PRAGMA foreign_keys=ON; +PRAGMA temp_store=MEMORY; SQL SchemaNativePragma :: #string SQL PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL; PRAGMA foreign_keys=ON; +PRAGMA temp_store=MEMORY; SQL SchemaMeta :: #string SQL diff --git a/src/storage/sqlite.zi b/src/storage/sqlite.zi index 1c1a5450d..ac8e5d563 100644 --- a/src/storage/sqlite.zi +++ b/src/storage/sqlite.zi @@ -156,9 +156,17 @@ SqliteClearBindings :: (statement: *void) -> s32 { return ClearBindingsRaw(statement) } +// Imports, archives and upgrades keep temporary data in memory like the main +// database: Android gives apps no writable temporary directory. #program_export SqliteOpen :: (path: *u8, database: **void, flags: s32) -> s32 { - return OpenRaw(path, database, flags, null) + result := OpenRaw(path, database, flags, null) + if result == SQLITE_OK && database != null && database.* != null { + pragma: [32]u8 + CopyStringBytes("PRAGMA temp_store=MEMORY", *pragma[0]) + unused ExecRaw(database.*, *pragma[0], null, null, null) + } + return result } #program_export diff --git a/src/storage/storage_core.zi b/src/storage/storage_core.zi index a4346caa2..b92359e70 100644 --- a/src/storage/storage_core.zi +++ b/src/storage/storage_core.zi @@ -2087,7 +2087,8 @@ storage_exec_json_user_sql :: (sql: string, json: string) -> s32 { } rc = SqliteStep(stmt) if rc != SQLITE_DONE { - LogStorage(LOG_WARNING, "SYNC: changes SQL step failed") + LogStorageText(LOG_WARNING, "SYNC: changes SQL step failed: ", + AppTextFromCString(sqlite3_errmsg(storage_db_handle()), 200)) SqliteFinalize(stmt) return 0 } diff --git a/src/storage/sync_review.zi b/src/storage/sync_review.zi index 6340c1ea1..20c650c10 100644 --- a/src/storage/sync_review.zi +++ b/src/storage/sync_review.zi @@ -229,6 +229,15 @@ storage_sync_review_pending :: () -> s32 { } } +// The newest account version this device has heard of; a waiting review +// belongs to it until a newer snapshot replaces the review. +#program_export +storage_sync_review_version :: () -> s64 { + key_buffer: [64] u8 + AppCopyText("sync_seen_server_version", key_buffer[:]) + return get_meta_int64(*key_buffer[0], 0) +} + review_activity_name :: (activity: s32) -> string { if activity == { case 0; diff --git a/tests/storage_sync_behavior.zi b/tests/storage_sync_behavior.zi index ee1c0bde0..20a4f8fd3 100644 --- a/tests/storage_sync_behavior.zi +++ b/tests/storage_sync_behavior.zi @@ -28,6 +28,7 @@ #import, file "../src/storage/cell_records.zi"; #import, file "../src/storage/sync_nodes.zi"; #import, file "../src/storage/sync_node_storage.zi"; +#import, file "../src/storage/sqlite.zi"; #import "json_scan" Core :: #import, file "../src/storage/storage_core.zi"; @@ -420,6 +421,23 @@ test_rejects_unknown_outbox_record :: () { storage_close() } +// Android gives the app no writable temporary directory: a merge whose +// statement journal spilled to a file failed with "disk I/O error" and rolled +// back the whole review. The storage connection keeps temporary data in memory. +test_temporary_data_in_memory :: () { + root := make_root("temp-store") + check_true("init db", storage_init(root) != 0) + statement: *void = null + store: s32 = -1 + if SqlitePrepareText(storage_db_handle(), "PRAGMA temp_store", -1, *statement, null) == SQLITE_OK && + SqliteStep(statement) == SQLITE_ROW { + store = SqliteColumnInt(statement, 0) + } + SqliteFinalize(statement) + check_true("temporary data stays in memory", store == 2) + storage_close() +} + test_v6_encrypted_records :: () { root := make_root("sync-v6") collection: [80]u8 @@ -1791,6 +1809,7 @@ Check :: () -> s32 { test_payload_excludes_local_settings() test_payload_includes_queued_edits() test_rejects_unknown_outbox_record() + test_temporary_data_in_memory() test_v6_encrypted_records() test_plaintext_private_response_ignored() test_large_outbox_batches() diff --git a/tests/sync_safety_behavior.zi b/tests/sync_safety_behavior.zi index dec833ae1..9edaa87ee 100644 --- a/tests/sync_safety_behavior.zi +++ b/tests/sync_safety_behavior.zi @@ -55,5 +55,12 @@ Answer :: () -> s32 { SyncUiRefreshSafe(state, 2.50, 1.0, true) { return 12 } state.screen = ScreenStart if SyncUiRefreshSafe(state, 3.0, 1.0, false) { return 13 } + + // A review the merge could not apply, or the person closed, does not + // come back until the account sends a newer snapshot. + if !SyncReviewDue(true, true, 35326, -1) { return 20 } + if SyncReviewDue(true, true, 35326, 35326) { return 21 } + if !SyncReviewDue(true, true, 35400, 35326) { return 22 } + if SyncReviewDue(false, true, 35400, -1) || SyncReviewDue(true, false, 35400, -1) { return 23 } return 42 }