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
Locking issue when using Multiple Tabs in a near Real-Time-Application #24187
Here’s the situation: The application is capable of handling multiple tabs simultaneously, all within the same VaadinSession. Updates occur relatively frequently—and sometimes refresh the entire interface. The problem here is that even if Tab A only needs 150 ms for the update and there are 3 tabs, we’re already at 450 ms during which the lock is held. And if the updates occur every 500 ms, there is hardly any time to process user interactions.
If the lock was per UI, it would only be 150 ms and that’s it. This would significantly improve the user experience.
Is your use case such that the user has all three tabs visible at the same time, or is it typically only one tab that the user actively works with and the other tabs are hidden in the background? I'm thinking that one potential workaround for this situation could be if Vaadin would detect which tab is the "active" one and then prioritize pushing out changes for that tab before pushing out changes to other tabs.
@Legioth That would be ideal for most use cases, but in this specific instance, the problem is that the data displayed on the various tabs—which, unfortunately, are all visible—should be synchronized, as otherwise it would only confuse the user.
Another thing that you could try is to put each UI in its own VaadinSession while still sharing the servlet-level HttpSession. What happens behind the scenes is that VaadinService.getServiceName() is used to find the Vaadin session to use (or create) from within the HTTP session.
By default, the "service name" is taken from the underlying ServletConfig.getServletName() which means that you get separate Vaadin session if you have multiple Vaadin servlets on different paths within the same application. I haven't tested it in practice, but it could in theory also work to override getServiceName() to return different values based on something else, e.g. the UI id parameter from the request in cases where a UI id is present.
How would this affect VaadinSession.getUis()? Would it still return all the UIs, or just one? And what about the session ID and attributes? Since they should behave the same way across tabs, right?
Session id remains same for all instances. Attributes on HttpSession and Vaadin's WrappedSession are also shared whereas attributes directly on VaadinSession would be separate for each UI. getUIs() would return only the single UI. You can use the static VaadinSession.getAllSessions(HttpSession) method to find all the Vaadin sessions in a HTTP session and then lock each session separately to find the UI inside that session.
This sounds like a dirty workaround - Especially all usages of VaadinSession.setAttributes in the Application would need to be adapted... Is there any other way to fix this properly?
The other option that I'm aware of is to implement it as I suggested in #23365, i.e. to remove the session lock and add a UI lock instead. This wouldn't be backwards compatible so it would have to be configurable whether locking is done on the UI level or the session level. Furthermore, some existing VaadinSession API such as getAttribute and getUIs would become much more difficult to use correctly since there's e.g. the possibility that a UI has already been detached at the time when the iteration logic gets to it. You would basically have to do the same adaptations anyway.
You're Right!
One question remains for this:
Would it be possible for the Methods inside the VaadinSession to internally claim the SessionLock, if not already Running inside it?
This would ensure the Session Attributes would be accessed correctly right?
So every time a UI uses VaadinSession.setAttribute the VaadinSession is locked. This should also prevent deadlocks from happening by accident. Or am I missing something?
And then every time the UI is accessed only the lock inside the UI is locked.
UI-Based Locking would resolve the issue.
Now when I think about it, I think we should still keep the session lock around basically as today except that it's not held during regular UI event handling. A separate session lock is necessary so that you can e.g. check whether a session attribute already exists and initialize it only if it's missing. That case requires a separate lock that is held across both the read and the write operation. (This particular case could be handled with a computeIfAbsent operation but there are also other cases to consider.) I wouldn't make the existing session attribute methods handle locking automatically in case the lock isn't held since that would mean that any existing read+write case would suddenly become a race condition risk without the developer noticing anything.
The key insight is that we can do this without risking deadlocks with the help of a simple additional requirement. The typical deadlock happens if thread 1 holds the lock for UI 2 and tries to acquire the lock for session 3 while thread 4 holds the lock for session 3 and tries to lock UI 2. The typical solution for that is to make sure locking always happens in the same order. In our case, we want to enable running event handling with a UI locked but without any session locked, but still acquiring a session lock for e.g. checking attributes. This basically means that we would have to forbid locking in the opposite order, i.e. make the framework throw an exception if someone tries to lock a UI if the same thread also has a session locked. This would impact the typical operation to iterate over all UIs in a session and do something with them so we might have to introduce a dedicated helper method specifically for that case.
To summarize, this is how I imagine it could work.
There's a new application-wide configuration option, e.g. DeploymentConfiguration::isUiLocking. The option is disabled by default.
There are new methods in UI that correspond to existing VaadinSession methods: getLockInstance, lock, unlock, getPendingAccessQueue, hasLock, checkHasLock, getLastLocked, and getLastUnlocked. These methods would work similarly to the methods in VaadinSession so that e.g. unlocking triggers push() for that specific UI instance when appropriate (but not for other UIs in the same session) and access runs the callback synchronously if the instance is not locked and asynchronously if it's currently locked.
accessSynchronously is not allowed if already holding the lock for another UI, similarly to how VaadinSession works.
When UI locking mode is enabled:
Each UI instance has its own lock instance in addition to the session lock. UI::access and UI::accessSynchronously would work based on that UI-specific lock which also implies that each UI would have its own access queue instead of submitting tasks to the session's access queue.
UI event handlers are run with only that specific UI instance locked but without locking the session. Application logic that wants to do something session-scoped, e.g. reading or writing a session attribute, would have to explicitly access/lock the session. @VaadinSessionScope beans would have to handle concurrent access explicitly rather than assuming that usage is automatically protected by the session lock.
VaadinSession.getCurrent() is not set during UI event handling
To avoid causing deadlocks due to races between locks, it is explicitly not allowed to lock a UI instance while holding a session lock (technically while VaadinSession.getCurrent() is set, meaning that it's not even allowed to lock a UI from a different session). Trying to lock a UI while holding a session lock will throw an IllegalStateException. UI::access is still allowed since it will run asynchronously if necessary rather than blocking.
Unlocking the session does not iterate through UIs to run push().
The session is never locked during regular handling of UIDL requests. This implies that uIs is changed to a ConcurrentHashMap so that getUIById and getUIs() can work without locking the session. Instance fields related to tracking request durations and timestamps are also changed to use atomics and allow usage without locking.
addUI and removeUI require holding the lock for both the session and the UI.
It's the developer's responsibility to deal with cases where UIs are attached or detached while iterating the collection returned by getUIs(). In practice, this means that any iteration logic should be prepared for UIDetachedException from individual UI instances.
We should add a forEachUi(Consumer<UI> handler) helper method that iterates through a snapshot copy of the collection, does accessSynchronously on each UI, and calls the consumer while holding the lock for each specific UI assuming the UI is still attached at that point.
When UI locking mode is not enabled:
Vaadin behaves as today with a session lock that covers all UI instances in that session. UI locks are never acquired and application code can't get access to a UI lock instance by accident.
It remains allowed to run UI::accessSynchronously while holding a session lock.
Some of the new API and behavior might be useful for add-ons to work well when UI locking mode is enabled. When reasonable, this should also work as expected when UI locking is not enabled so that we reduce the cases where add-ons have to explicitly check which mode is used.
The new lock-related methods in UI delegate to the session lock and throw UIDetachedException in case the UI isn't attached to a session.
The relaxed lock requirements for various VaadinSession methods remain relaxed.
Activity
Legioth commentedon Apr 24, 2026
Is your use case such that the user has all three tabs visible at the same time, or is it typically only one tab that the user actively works with and the other tabs are hidden in the background? I'm thinking that one potential workaround for this situation could be if Vaadin would detect which tab is the "active" one and then prioritize pushing out changes for that tab before pushing out changes to other tabs.
OriginalFelix commentedon Apr 24, 2026
@Legioth That would be ideal for most use cases, but in this specific instance, the problem is that the data displayed on the various tabs—which, unfortunately, are all visible—should be synchronized, as otherwise it would only confuse the user.
Legioth commentedon Apr 27, 2026
Another thing that you could try is to put each UI in its own
VaadinSessionwhile still sharing the servlet-levelHttpSession. What happens behind the scenes is thatVaadinService.getServiceName()is used to find the Vaadin session to use (or create) from within the HTTP session.By default, the "service name" is taken from the underlying
ServletConfig.getServletName()which means that you get separate Vaadin session if you have multiple Vaadin servlets on different paths within the same application. I haven't tested it in practice, but it could in theory also work to overridegetServiceName()to return different values based on something else, e.g. the UI id parameter from the request in cases where a UI id is present.OriginalFelix commentedon Apr 27, 2026
How would this affect VaadinSession.getUis()? Would it still return all the UIs, or just one? And what about the session ID and attributes? Since they should behave the same way across tabs, right?
Legioth commentedon Apr 27, 2026
Session id remains same for all instances. Attributes on
HttpSessionand Vaadin'sWrappedSessionare also shared whereas attributes directly onVaadinSessionwould be separate for each UI.getUIs()would return only the single UI. You can use the staticVaadinSession.getAllSessions(HttpSession)method to find all the Vaadin sessions in a HTTP session and then lock each session separately to find the UI inside that session.OriginalFelix commentedon Apr 27, 2026
This sounds like a dirty workaround - Especially all usages of VaadinSession.setAttributes in the Application would need to be adapted... Is there any other way to fix this properly?
Legioth commentedon Apr 27, 2026
The other option that I'm aware of is to implement it as I suggested in #23365, i.e. to remove the session lock and add a UI lock instead. This wouldn't be backwards compatible so it would have to be configurable whether locking is done on the UI level or the session level. Furthermore, some existing
VaadinSessionAPI such asgetAttributeandgetUIswould become much more difficult to use correctly since there's e.g. the possibility that a UI has already been detached at the time when the iteration logic gets to it. You would basically have to do the same adaptations anyway.OriginalFelix commentedon Apr 27, 2026
You're Right!
One question remains for this:
Would it be possible for the Methods inside the VaadinSession to internally claim the SessionLock, if not already Running inside it?
This would ensure the Session Attributes would be accessed correctly right?
So every time a UI uses VaadinSession.setAttribute the VaadinSession is locked. This should also prevent deadlocks from happening by accident. Or am I missing something?
And then every time the UI is accessed only the lock inside the UI is locked.
UI-Based Locking would resolve the issue.
Legioth commentedon Apr 27, 2026
Now when I think about it, I think we should still keep the session lock around basically as today except that it's not held during regular UI event handling. A separate session lock is necessary so that you can e.g. check whether a session attribute already exists and initialize it only if it's missing. That case requires a separate lock that is held across both the read and the write operation. (This particular case could be handled with a
computeIfAbsentoperation but there are also other cases to consider.) I wouldn't make the existing session attribute methods handle locking automatically in case the lock isn't held since that would mean that any existing read+write case would suddenly become a race condition risk without the developer noticing anything.The key insight is that we can do this without risking deadlocks with the help of a simple additional requirement. The typical deadlock happens if thread 1 holds the lock for UI 2 and tries to acquire the lock for session 3 while thread 4 holds the lock for session 3 and tries to lock UI 2. The typical solution for that is to make sure locking always happens in the same order. In our case, we want to enable running event handling with a UI locked but without any session locked, but still acquiring a session lock for e.g. checking attributes. This basically means that we would have to forbid locking in the opposite order, i.e. make the framework throw an exception if someone tries to lock a UI if the same thread also has a session locked. This would impact the typical operation to iterate over all UIs in a session and do something with them so we might have to introduce a dedicated helper method specifically for that case.
OriginalFelix commentedon Apr 27, 2026
If you could introduce a locking procedure like this, it would be really awesome and would fix the problem.
Legioth commentedon Apr 30, 2026
To summarize, this is how I imagine it could work.
DeploymentConfiguration::isUiLocking. The option is disabled by default.UIthat correspond to existingVaadinSessionmethods:getLockInstance,lock,unlock,getPendingAccessQueue,hasLock,checkHasLock,getLastLocked, andgetLastUnlocked. These methods would work similarly to the methods inVaadinSessionso that e.g. unlocking triggerspush()for that specific UI instance when appropriate (but not for other UIs in the same session) andaccessruns the callback synchronously if the instance is not locked and asynchronously if it's currently locked.accessSynchronouslyis not allowed if already holding the lock for another UI, similarly to howVaadinSessionworks.When UI locking mode is enabled:
UI::accessandUI::accessSynchronouslywould work based on that UI-specific lock which also implies that each UI would have its own access queue instead of submitting tasks to the session's access queue.@VaadinSessionScopebeans would have to handle concurrent access explicitly rather than assuming that usage is automatically protected by the session lock.VaadinSession.getCurrent()is not set during UI event handlingVaadinSession.getCurrent()is set, meaning that it's not even allowed to lock a UI from a different session). Trying to lock a UI while holding a session lock will throw anIllegalStateException.UI::accessis still allowed since it will run asynchronously if necessary rather than blocking.push().uIsis changed to aConcurrentHashMapso thatgetUIByIdandgetUIs()can work without locking the session. Instance fields related to tracking request durations and timestamps are also changed to use atomics and allow usage without locking.addUIandremoveUIrequire holding the lock for both the session and the UI.getUIs(). In practice, this means that any iteration logic should be prepared forUIDetachedExceptionfrom individual UI instances.forEachUi(Consumer<UI> handler)helper method that iterates through a snapshot copy of the collection, doesaccessSynchronouslyon each UI, and calls the consumer while holding the lock for each specific UI assuming the UI is still attached at that point.When UI locking mode is not enabled:
UI::accessSynchronouslywhile holding a session lock.UIDetachedExceptionin case the UI isn't attached to a session.VaadinSessionmethods remain relaxed.OriginalFelix commentedon Apr 30, 2026
Sounds good!
OriginalFelix commentedon Jul 13, 2026
Quick Question is there any news on this topic already? :)