Skip to content

fix(navigation): make BaseViewModel setState an atomic compare-and-set#1104

Merged
bmc08gt merged 1 commit into
code/cashfrom
fix/base-viewmodel-atomic-setstate
Jul 20, 2026
Merged

fix(navigation): make BaseViewModel setState an atomic compare-and-set#1104
bmc08gt merged 1 commit into
code/cashfrom
fix/base-viewmodel-atomic-setstate

Conversation

@bmc08gt

@bmc08gt bmc08gt commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Problem

BaseViewModel.setState did a non-atomic read-modify-write:

_stateFlow.value = _stateFlow.value.update()

Events are dispatched from multiple threads — the UI thread and background flows on defaultDispatcher. Two concurrent reducers can read the same snapshot and clobber each other, silently dropping one event's state change.

Fix

Use MutableStateFlow.update {}, which applies the reducer in a compare-and-set loop and retries against the latest value:

_stateFlow.update(update)

Events are dispatched from multiple threads — the UI thread and background flows
on defaultDispatcher. The old `_stateFlow.value = _stateFlow.value.update()` is a
non-atomic read-modify-write: concurrent reducers can derive from the same
snapshot and clobber each other, silently dropping one event's state change.

Use MutableStateFlow.update {} so the reducer retries against the latest value.
@github-actions github-actions Bot added type: fix Bug fix area: ui Compose UI, theme, components, resources and removed type: fix Bug fix labels Jul 20, 2026
@bmc08gt
bmc08gt merged commit 17a69fe into code/cash Jul 20, 2026
3 checks passed
@bmc08gt
bmc08gt deleted the fix/base-viewmodel-atomic-setstate branch July 20, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ui Compose UI, theme, components, resources

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant