I have an application that starts a long synchronous operation. The users see a blue loading indicator at the top of the application. When they close the frozen browser tab and open a new one with the app, it is not loaded until the operation is complete.
I know that the operation can be started in a background thread, but I wonder why this happens. I found out that the SynchronizedRequestHandler blocks the VaadinSession before performing the request. Is there a reason to block the session? How can I avoid this behavior and not use background threads?
When they close the frozen browser tab and open a new one with the app, it is not loaded until the operation is complete.
This is because the request-handling thread that does the long-running operation still holds the session lock. Sessions are locked to protect the UI state and various framework-internal bookkeeping from corruption that might happen if updated from multiple threads at the same time.
The lock could in theory be per UI instance instead of per session but it’s not built in that way for historical reasons and changing it at this point would not be practical due to the amount of existing application code that expects it to be like it is now.
How can I avoid this behavior and not use background threads?
This is what threads are meant for: allow doing multiple things in parallel. If you’re concerned by the overhead of starting new OS threads, then you can use a thread pool or virtual threads (introduced in Java 21).
It doesn’t hurt to create a ticket in the Flow repository. We don’t have plans for making this type of change for the moment but we do keep any eye on how many reactions there are on all issues and use that as one input for prioritizing improvements.