From 1602d9bc012970fd19d53bf3a25316f51934114d Mon Sep 17 00:00:00 2001 From: Waozi Date: Fri, 9 Oct 2026 11:25:31 -0300 Subject: [PATCH] Keep visible practices free of frame-thread sync and request work On Android and the web a sync runs on the frame thread, and every one stopped the breathing circle for about 0.3 s on a Pixel 3a while a practice was running. A practice on screen, also in its small window, now holds back starting a sync and finishing one that began before it; Lumi and package requests, which use the frame thread everywhere, wait too. Scheduled work runs when the practice ends. In the background nothing is drawn, so background practice sync continues. --- src/app/app_sync.zi | 6 ++++++ src/app/application.zi | 8 ++++++-- src/app/sync_safety.zi | 28 +++++++++++++++++++++++++++- src/app/sync_safety_policy.zi | 18 +++++++++++++++--- src/cells/lumi_telegram.zi | 18 ++++++------------ tests/sync_safety_behavior.zi | 12 ++++++++++++ tests/sync_safety_link_test.c | 20 ++++++++++++++++++++ tests/sync_safety_zi_test.sh | 1 + 8 files changed, 93 insertions(+), 18 deletions(-) diff --git a/src/app/app_sync.zi b/src/app/app_sync.zi index 61c2730a6..f44f96b24 100644 --- a/src/app/app_sync.zi +++ b/src/app/app_sync.zi @@ -1151,6 +1151,12 @@ app_sync_pump :: (app: *InnerBreeze) { app.ui.sync_requested = false unused app_auto_sync(app) } + // Here sync runs on the frame thread: a visible practice also waits to + // finish a request that started before it, since applying its records + // takes longer than a frame. Scheduled work runs when it ends. + if SyncOnFrameThread() && app_practice_holds_frame_work(app) { + return + } now = GetTime() app_sync_record_input(app, now) app_collect_finished_sync() diff --git a/src/app/application.zi b/src/app/application.zi index 263cdd0f3..0c60620f7 100644 --- a/src/app/application.zi +++ b/src/app/application.zi @@ -65,6 +65,7 @@ PackageNow :: () -> s64 #foreign c "now_seconds"; #import, file "../screens/settings/settings_status.zi"; #import, file "app_sync.zi"; +#import, file "sync_safety.zi"; #import, file "telegram_account_flow.zi"; #import, file "../platform/telegram_bridge.zi"; #import, file "../storage/delegate_store.zi"; @@ -1523,8 +1524,11 @@ app_update_draw :: (vapp: *void, viewport: Rectangle) { settings_sync_account_pump(app) ActivityPump(app) app_sync_pump(app) - app_lumi_pump(app) - PackagePump(app.enabled_apps, cast(s32)PackageNow()) + // Lumi and package requests run on the frame thread on every platform. + if !app_practice_holds_frame_work(app) { + app_lumi_pump(app) + PackagePump(app.enabled_apps, cast(s32)PackageNow()) + } if app.app_choices_pending && practice_active(app) == null && app.modal.active == 0 && app.breathing.screen != ScreenHabitEdit && app.breathing.screen != ScreenHabitSessionEdit && app_diary_save() { diff --git a/src/app/sync_safety.zi b/src/app/sync_safety.zi index 250a1edc4..b1be9ea66 100644 --- a/src/app/sync_safety.zi +++ b/src/app/sync_safety.zi @@ -1,6 +1,31 @@ #import, file "app_types.zi"; #import, file "../core/types.zi"; #import, file "sync_safety_policy.zi"; +#import, file "session_screen.zi"; + +// Android and the web run sync on the frame thread instead of a worker. +SyncOnFrameThread :: () -> bool { + #if #defined(PLATFORM_WEB) || #defined(ANDROID_BUILD) { + return true + } + return false +} + +// The practice drawn on screen, also in its small window. While the app is +// in the background nothing is drawn, so no practice holds work back. +app_visible_practice_screen :: (app: *InnerBreeze) -> s32 { + if app == null || app.backgrounded != 0 { + return ScreenStart + } + return app_session_screen(app) +} + +// A visible practice animates every frame, so frame-thread work that can +// outlast a frame (finishing a request, applying its records, package +// updates) waits until the practice ends. +app_practice_holds_frame_work :: (app: *InnerBreeze) -> bool { + return SyncScreenBlocksRefresh(app_visible_practice_screen(app)) +} #program_export app_background_sync_safe :: (app: *InnerBreeze) -> s32 { @@ -8,7 +33,8 @@ app_background_sync_safe :: (app: *InnerBreeze) -> s32 { return ifx BackgroundSyncSafeFields( app.modal.active, app.habit_edit.active, app.sync_server_url_focused, app.sync_alias_focused, - app.profile_friend_input_focused, app.breathing.screen) then 1 else 0 + app.profile_friend_input_focused, app.breathing.screen, + app_visible_practice_screen(app), SyncOnFrameThread()) then 1 else 0 } SyncUiUpdateSafe :: (app: *InnerBreeze, now: float64, diff --git a/src/app/sync_safety_policy.zi b/src/app/sync_safety_policy.zi index 52ed7b97d..4740d78b6 100644 --- a/src/app/sync_safety_policy.zi +++ b/src/app/sync_safety_policy.zi @@ -9,6 +9,10 @@ SyncSafetyState :: struct { alias_focused: s32 friend_input_focused: s32 screen: s32 + // The running practice, also while it shows in its small window. + practice_screen: s32 + // Android and the web run sync on the frame thread, not a worker. + frame_thread_transport: bool } SyncScreenBlocksRefresh :: (screen: s32) -> bool { @@ -21,14 +25,20 @@ SyncInputBlocksRefresh :: (state: SyncSafetyState) -> bool { } BackgroundSyncSafe :: (state: SyncSafetyState) -> bool { - // Transport may exchange encrypted records during a practice. Refreshing - // its settings or screen remains a separate, deferred operation. + // A sync started on the frame thread stops every frame while it builds + // and applies encrypted records, so a running practice waits until it + // ends. A worker-thread transport may exchange records during a + // practice; refreshing settings or the screen stays deferred. + if state.frame_thread_transport && SyncScreenBlocksRefresh(state.practice_screen) { + return false + } return state.modal_active == 0 && !SyncInputBlocksRefresh(state) } BackgroundSyncSafeFields :: (modal_active: s32, habit_edit_active: s32, server_url_focused: s32, alias_focused: s32, - friend_input_focused: s32, screen: s32) -> bool { + friend_input_focused: s32, screen: s32, practice_screen: s32, + frame_thread_transport: bool) -> bool { state: SyncSafetyState state.modal_active = modal_active state.habit_edit_active = habit_edit_active @@ -36,6 +46,8 @@ BackgroundSyncSafeFields :: (modal_active: s32, habit_edit_active: s32, state.alias_focused = alias_focused state.friend_input_focused = friend_input_focused state.screen = screen + state.practice_screen = practice_screen + state.frame_thread_transport = frame_thread_transport return BackgroundSyncSafe(state) } diff --git a/src/cells/lumi_telegram.zi b/src/cells/lumi_telegram.zi index b98dbeeba..cdd01afe6 100644 --- a/src/cells/lumi_telegram.zi +++ b/src/cells/lumi_telegram.zi @@ -18,7 +18,6 @@ #import, file "../app/text_buffers.zi"; #import, file "../app/app_nav_state.zi"; #import, file "../app/route_types.zi"; -#import, file "../app/session_screen.zi"; #import, file "../app/locale.zi"; #import, file "../practices/practice_actions.zi"; #import, file "../platform/network_transport.zi"; @@ -42,9 +41,6 @@ c :: #system_library "c"; // Telegram messages reach the app within this interval while Inbe is open. LUMI_TELEGRAM_POLL_SECONDS :: 2 LUMI_TELEGRAM_RETRY_SECONDS :: 5 -// While a practice runs, requests on Android and the web share its frame -// thread, so idle polls slow down to keep the animation smooth. -LUMI_TELEGRAM_PRACTICE_POLL_SECONDS :: 10 lumi_telegram_session: Session; lumi_telegram_transfer: NetworkTransfer; @@ -332,7 +328,11 @@ LumiTelegramPump :: (app: *InnerBreeze) { } } } - if lumi_telegram_operation.active || storage_has_sync_account() == 0 { return } + if lumi_telegram_operation.active { return } + now := TelegramNow(null) + requested := lumi_telegram_disconnect_requested || lumi_telegram_link_requested + if !requested && now < lumi_telegram_retry { return } + if storage_has_sync_account() == 0 { return } client := storage_get_setting_text("lumi_telegram_client") if client.count != 64 { bytes: [65]u8 @@ -359,13 +359,7 @@ LumiTelegramPump :: (app: *InnerBreeze) { } return } - now := TelegramNow(null) - if now < lumi_telegram_retry { return } - poll_seconds := LUMI_TELEGRAM_POLL_SECONDS - if app_is_session_screen(app_session_screen(app)) { - poll_seconds = LUMI_TELEGRAM_PRACTICE_POLL_SECONDS - } - lumi_telegram_retry = now + poll_seconds + lumi_telegram_retry = now + LUMI_TELEGRAM_POLL_SECONDS if lumi_telegram_ack != 0 { out: Vec(u8) BuilderPrint(*out, "{\"id\":%,\"client\":\"%\"}", lumi_telegram_ack, client) diff --git a/tests/sync_safety_behavior.zi b/tests/sync_safety_behavior.zi index 0e750ee37..5d9da1048 100644 --- a/tests/sync_safety_behavior.zi +++ b/tests/sync_safety_behavior.zi @@ -34,6 +34,18 @@ Answer :: () -> s32 { if !BackgroundSyncSafe(state) || SyncUiRefreshSafe(state, 3.0, 1.0, false) { return 14 } state.screen = ScreenHabits + // Where sync runs on the frame thread, a running practice waits for it, + // also while the practice shows in its small window. + state.frame_thread_transport = true + if !BackgroundSyncSafe(state) { return 15 } + state.practice_screen = ScreenPracticeSession + if BackgroundSyncSafe(state) { return 16 } + state.practice_screen = ScreenMeditation + if BackgroundSyncSafe(state) { return 17 } + state.frame_thread_transport = false + if !BackgroundSyncSafe(state) { return 18 } + state.practice_screen = ScreenStart + if SyncUserIdle(2.49, 1.0, false) || !SyncUserIdle(2.50, 1.0, false) || SyncUserIdle(3.0, 1.0, true) { return 11 } diff --git a/tests/sync_safety_link_test.c b/tests/sync_safety_link_test.c index 668b2125f..3dee41bb9 100644 --- a/tests/sync_safety_link_test.c +++ b/tests/sync_safety_link_test.c @@ -17,6 +17,26 @@ main(void) app.ui.text_input_active = false; assert(sync_safety_SyncUiUpdateSafe(&app, 20.0, 1.0, false)); + /* This native build runs sync on a worker thread, so a running + practice does not hold it back. */ + app.breathing.screen = ScreenSession; + assert(app_background_sync_safe(&app) == 1); + app.breathing.screen = ScreenLumi; + + /* A visible practice, also in its small window, holds frame-thread + work; in the background nothing is drawn. */ + assert(!sync_safety_app_practice_holds_frame_work(&app)); + app.breathing.screen = ScreenSession; + assert(sync_safety_app_practice_holds_frame_work(&app)); + app.breathing.screen = ScreenHabits; + app.session_window.screen = ScreenMeditation; + assert(sync_safety_app_practice_holds_frame_work(&app)); + app.backgrounded = 1; + assert(!sync_safety_app_practice_holds_frame_work(&app)); + app.backgrounded = 0; + app.session_window.screen = ScreenStart; + app.breathing.screen = ScreenLumi; + app.modal.active = 1; assert(app_background_sync_safe(&app) == 0); app.sync_alias_focused = 1; diff --git a/tests/sync_safety_zi_test.sh b/tests/sync_safety_zi_test.sh index 09c048a53..c78ae49e9 100644 --- a/tests/sync_safety_zi_test.sh +++ b/tests/sync_safety_zi_test.sh @@ -76,5 +76,6 @@ cmp "$work/source/safety.zib" "$work/saved/safety.zib" "$root/tests/sync_safety_link_test.c" \ "$work/native/app/sync_safety.c" \ "$work/native/sync_safety_policy.c" \ + "$work/native/session_screen.c" \ -o "$work/native/test" env -u DISPLAY -u WAYLAND_DISPLAY "$work/native/test"