Skip to content

UI locking: forEachUi helper, documentation and configuration property #27585

Backlog

Description

@totally-not-ai

Part of #24187. Depends on #27580–#27584.

Finish UI locking and make it a supported configuration.

Scope

  • Add the forEachUi(Consumer<UI>) helper on VaadinSession that runs an action for each UI of the session under that UI's lock, handling UIs that are detached meanwhile.
  • Document the threading rules for application and add-on developers: lock order, what the UI lock and the session lock protect, and how to access session attributes safely.
  • Promote the experimental feature flag to a documented configuration property (uiLocking).

Acceptance criteria

  • forEachUi is tested.
  • Documentation is published.
  • The configuration property works in all supported ways to set deployment configuration.

Working on behalf of Mikhail

Activity

  1. totally-not-ai commented on Dec 1, 2026

    @totally-not-ai
    ContributorAuthor

    Triage: valid task, blocked. This is the last step of the UI locking plan in #24187, and it cannot start until the steps it builds on have landed.

    What this issue will need once unblocked
    • VaadinSession.forEachUi(Consumer<UI>): run the action under each UI's own lock, never while holding the session lock (to respect the lock order), and skip UIs removed from the session in between.
    • Promote the experimental flag to a uiLocking deployment property, readable from init parameters, system properties and Spring/Quarkus properties, like other InitParameters.
    • Docs (vaadin/docs): lock order, what the UI lock vs. the session lock protects, safe access to session attributes from UI-locked code, and guidance for add-on authors.
  2. Legioth commented on Dec 1, 2026

    @Legioth
    Member

    One gotcha to keep in mind is that it wouldn't be allowed to run the method as it's designed here while also holding the session lock. This also means that consumers might be invoked in different order between concurrent forEachUi calls, e.g. like this:

    1. Run forEachUi(ui -> doSomething(ui, "foo")) on thread 1
    2. The callback performs the action with foo for UI 1, after which the thread gets context switched
    3. Run forEachUi(ui -> doSomething(ui, "bar")) on thread 2
    4. The callback performs the action with bar for UI 1 and UI 2
    5. Progress continues on thread 1 to perform the action with foo for UI 2.

    The result would then be that foo is the last performed action on UI 2 whereas bar is the last performed action on UI 1. Depending on the nature of the action, this difference might or might not be acceptable. An action that is aware of the risk can explicitly check for the situation and recover but there's a risk that the caller of forEachUi doesn't realize that this is necessary.

    Anything I can see that the framework could do to resolve this on the application's behalf would require either blocking or work-stealing, which both come at a risk that might not be acceptable in all cases. Blocking has the risk of causing deadlocks and work-stealing has the risk of running actions with unexpected ThreadLocal values.

    I suggest that we accept the reordering risk and document it clearly for now. If some specific use case comes up, then we can consider introducing optional support for either a blocking or work-stealing mode e.g. as an overload that also accepts the mode to use as an enum parameter.

    @totally-not-ai Do you see any other alternatives?

  3. totally-not-ai commented on Dec 1, 2026

    @totally-not-ai
    ContributorAuthor

    @Legioth I agree: accept the reordering and document it. I don't see a fundamentally different option, only two variants that are worth naming in the docs.

    • Ordered async via the access queue. If forEachUi (or the app) enqueues the action to every UI's access queue in one atomic step, every UI sees foo and bar in the same order. That is your work-stealing mode, but with UI.access() semantics developers already know: the framework sets its own current instances, and only the app's own ThreadLocals are at risk. This could be the optional mode later, so the default doesn't have to be.
    • Share state, not commands. Much of the broadcast use case is "push the latest value to all UIs". If each UI binds to a shared signal, or reads a shared value inside the action, the order doesn't matter because every UI ends up showing the latest state. The docs should recommend this for state and keep forEachUi for one-off actions.
    • Ruled out: taking all UI locks in a fixed order before running. That is just blocking, and it defeats per-UI locking.

    So the Javadoc should say: no ordering guarantee across UIs between concurrent calls, never call it while holding the session lock, and use signals (or UI.access() from a single producer thread) when order matters.

  4. Legioth commented on Dec 1, 2026

    @Legioth
    Member

    enqueues the action to every UI's access queue in one atomic step

    Note that this still requires blocking while enqueuing to prevent interleaving from another invocation. The key point is that this is would be a dedicated lock only for this purpose and the risk of deadlocks would be reduced by never calling application logic while holding that lock. But this also means that we can't use the regular UI.access() that runs the task immediately if the UI lock is available. Instead, we would have to add the task to the access queue of each UI while holding the forEachUi lock and then do another pass over the same UIs to make sure each access queue is purged while not holding the forEachUi lock.

    This means that ordering between UIs might be inconsistent between runs. One invocation might see the task run on UI 1 first and UI 2 second whereas another invocation might see UI 2 first.

  5. added theissue type on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions