Skip to content

deep state rune captures old state and does not provide current value even outside reactive access #17375

@lucidNTR

Description

@lucidNTR

Describe the bug

A deep state rune that is accessed in an onDestroy of a component and that is updated in same tick as removing the component will provide old value even to non reactive code directly accessing the state object, same with untrack and snapshot. The js assumption of synchronous reasonable order of write/read operations is broken making it impossible to reason about order of state updates. The documentation mentions that reactivity would not be ensured in situations like this which would require eager but eager is also not working for data changes in same tick as on destroy, making ugly hacks for cleanup that depends on state needed and the documentation mentions nothing about state being frozen internally and runes providing outdated data to different access contexts.

This also hints at an implementation that copy state data around instead of using js objects that behave as users expect, which would hint at a plethora of similar possible bugs.

Reproduction

https://svelte.dev/playground/49d503ea86854196b5f928f202761f1d?version=5.46.0

Logs

value in change method: changed
value in comp destroy: init value
value in comp destroy, snapshot: init value
value in comp destroy, untrack: init value

System Info

version=5.46.0

Severity

severe, as this is easy to fix with an await tick(), but to know misbehaviour is present in the first place and find the bug is near impossible as fundamental mental model how objects work in js is broken.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions