Errors handled while the widget tree is building crashed or wedged the
app in three related ways:
- FlutterError.onError showed dialogs and snackbars synchronously.
Pushing a route mid-build trips the navigator's !_debugLocked
assertion, and because the one-dialog guard was set before showDialog
threw, its release never attached: every later error dialog was then
silently suppressed while nothing was on screen, leaving a blank,
unresponsive app until restart. Snackbars crashed outright via the
messenger's setState.
- The sync status and barcode dialogs did ref.read inside their route
builders, which run during build. With the provider chain dirty (any
token refresh dirties wgerBase -> auth), the read flushed it there and
Riverpod scheduled a scope refresh mid-build. The barcode builder also
created its search future inline, re-firing the search on every dialog
rebuild.
- Provider create bodies that widgets watch during build read providers
synchronously: the dashboard's routine hydration chain crashed on cold
start right after a token refresh, and TrophyStateNotifier.build()
has the same shape in the post-login window, where invalidation order
left networkStatusProvider dirty while its dependents rebuilt.
lib/core/build_safety.dart adds the two idioms as helpers: runAfterFrame
defers UI side effects past the build phase, yieldPastBuild yields
provider create bodies past their synchronous phase (a microtask, not a
timer, since widget tests assert !timersPending at teardown). The error
dialog guard is released when the context is gone or the push fails, the
snackbar helpers re-resolve the messenger inside the callback, the
dialog builders snapshot their providers in the tap handler, and
post-login invalidation runs leaf-first. Regression tests cover each
shape.
A rejected refresh token runs clearSessionOnly inside the single-flight
refresh future. PowerSync's disconnect can block on an in-flight sync
fetch that is itself awaiting that same refresh future through
AuthHttpClient's pre-emptive refresh, closing a three-party cycle
(refresh -> disconnect -> sync fetch -> refresh) that permanently wedges
every authenticated request behind the never-completing refresh. The
session-expired message never appears because it is sequenced after the
hanging call, and an app restart re-triggers the same rejected refresh
during startup revalidation, so the deadlock re-forms immediately.
On the keep-data path the DB is not wiped, so there is no re-attach race
to guard against; the disconnect is only a courtesy stop of the sync
loop and is now fire-and-forget. The wipe path (explicit logout) keeps
its ordering. The disconnect hook is injectable for tests because
PowerSyncDatabase is a base class that cannot be faked.
When the server-provided powersync URL is not reachable, try using $server-url/ps/.
This should help with self-hosted instances where SITE_URL points to localhost
or other similar misconfigurations
See https://github.com/wger-project/wger/issues/2432