You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UI locking: forEachUi helper, documentation and configuration property #27585
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.
Related:Feat/UI locking #26804 (open) implements per-UI locking in one PR. It already adds a uiLocking parameter, but no forEachUi helper and no developer documentation.
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.
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:
Run forEachUi(ui -> doSomething(ui, "foo")) on thread 1
The callback performs the action with foo for UI 1, after which the thread gets context switched
Run forEachUi(ui -> doSomething(ui, "bar")) on thread 2
The callback performs the action with bar for UI 1 and UI 2
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.
@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.
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.
Activity
totally-not-ai commentedon Dec 1, 2026
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.
forEachUiyet, so there is nothing for this issue to finish or document.uiLockingparameter, but noforEachUihelper and no developer documentation.forEachUiis new public API onVaadinSession. Its exact contract (skip or report UIs detached meanwhile, behaviour when the session lock is already held) depends on the lock order chosen in UI locking: per-UI lock core behind an experimental feature flag #27582.@totally-not-aion this issue then and I'll open the PR.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.uiLockingdeployment property, readable from init parameters, system properties and Spring/Quarkus properties, like otherInitParameters.Legioth commentedon Dec 1, 2026
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
forEachUicalls, e.g. like this:forEachUi(ui -> doSomething(ui, "foo"))on thread 1foofor UI 1, after which the thread gets context switchedforEachUi(ui -> doSomething(ui, "bar"))on thread 2barfor UI 1 and UI 2foofor UI 2.The result would then be that
foois the last performed action on UI 2 whereasbaris 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 offorEachUidoesn'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
ThreadLocalvalues.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?
totally-not-ai commentedon Dec 1, 2026
@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.
forEachUi(or the app) enqueues the action to every UI's access queue in one atomic step, every UI seesfooandbarin the same order. That is your work-stealing mode, but withUI.access()semantics developers already know: the framework sets its own current instances, and only the app's ownThreadLocals are at risk. This could be the optional mode later, so the default doesn't have to be.forEachUifor one-off actions.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.Legioth commentedon Dec 1, 2026
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 theforEachUilock and then do another pass over the same UIs to make sure each access queue is purged while not holding theforEachUilock.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.