Repository navigation
fix: run signal effects inline only when session is locked - #27460
Conversation
Signal effects, the signal result notifier and the background change flag took "the UI is current" to mean "the session lock is held by this thread". The request handlers now keep that rule (#27201, #27439), but code that sets the current UI without holding the lock, such as custom request handling, tests or third party integrations, still made effects touch UI state without the lock and fail. Now the work runs inline only when the UI is current and its session is locked by the current thread, and otherwise goes through UI.access. A change made without the lock is reported as a background change. Related to #27187
The EffectTest owner UI tests use a real MockUI because the owner UI's internals and session lock are now checked; a Mockito UI has no internals.
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 1 comment.
| Finding | |
|---|---|
| 🧪 | Swapping the mocked UI for MockUI registers a Vaadin signal environment that stays active for the rest of the test class |
| @Test | ||
| void ownerUI_sameUICurrent_notBackgroundChange() { | ||
| UI ui = mock(UI.class); | ||
| UI ui = new MockUI(); |
There was a problem hiding this comment.
🧪 Swapping the mocked UI for MockUI registers a Vaadin signal environment that stays active for the rest of the test class
new MockUI() builds a whole MockServletServiceSessionSetup, and the VaadinService.init() it triggers registers a VaadinServiceEnvironment in the static SignalEnvironment list and leaves the service, session and request as current instances. Nothing in SignalTestBase undoes either, and the environment keeps a strong reference to the service, so it stays active (VaadinService.getCurrent() == service) for every later test on that thread.
The base environment returns null from getEffectDispatcher()/getResultNotifier() unless a test calls useTestEffectDispatcher(), and SignalEnvironment.resolve skips nulls, so from then on effects and shared-signal results are dispatched to the service's real virtual-thread executor. Tests that assert right after signal.set(...), such as changeTracking_readSameSignalMultipleTimes_effectRunOnlyOnce and the two that follow it, then race against a background thread.
There is also a dependency in the other direction: MockUI() reuses VaadinSession.getCurrent() when one is left over from an earlier class, and if that session is not one whose hasLock() is stubbed true, these three tests fail instead.
A UI without a service behind it would avoid both, for example new UI() with getInternals().setSession(...) on a session mock that reports the lock.
EffectTest.java:845 · tests
There was a problem hiding this comment.
Good catch. The three owner-UI tests now use a plain new UI() whose session is a mock that reports the lock as held. No VaadinService is started, so no signal environment is registered, and the tests no longer depend on a session left current by an earlier test.
MockUI initializes a VaadinService, which registers a Vaadin signal environment that stays active for the rest of the test class and dispatches later effects to a real executor. A plain UI with a session mock that reports the lock avoids that and no longer depends on a session left current by an earlier test.
|
The three Sonar findings in |
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — collected 1 finding below that could not be attached to the diff.
| Finding | |
|---|---|
| 📝 | The architecture chapter still states that a signal effect treats a current UI as locked work and skips UI.access |
📝 The architecture chapter still states that a signal effect treats a current UI as locked work and skips UI.access
The paragraph on the UI.getCurrent() promise uses signal effects as its example of framework code that relies on the promise without checking it. After this change the effect dispatcher, the result notifier and the background-change flag all verify the lock, so the example describes behavior that no longer exists — in the very chapter that explains the rule this hardening enforces.
The surrounding argument still holds for application code, so only the framework example needs rewording (effects now check, application code still does not).
architecture.md:109 · documentation
|
@vaadin-review-bot Updated the architecture guideline. The |
…quire-session-lock
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
|
|
Documentation Bot: No documentation needed for this change. This is a hardening fix classified as Nothing was opened in vaadin/docs. If you disagree, the change has to be documented by hand there.
|
…CP: 25.4) Related to #27187, #27188 **Hardening** · flow-server · signal effects owned by a UI Signal effects, the signal result notifier and the background change flag treated "this UI is current" as "this thread holds the session lock". Flow's request handling now keeps that rule (#27201, #27439). Code outside Flow can still break it, for example by calling `UI.setCurrent` without locking the session. Then an effect touched UI state without the lock and failed with "Cannot access state in VaadinSession or UI without locking the session". After that, its error handler failed the same way. **Risks:** -⚠️ Behaviour change, only when the UI is current but its session is not locked by this thread: the effect, or the result notifier task, is queued through `UI.access` instead of running right away. `isBackgroundChange()` reports `true` for that run. - When the UI is current and locked, which is every normal case, nothing changes. - ✅ No new public API. The shared check is `UIInternals.isCurrentAndLocked()`, which is in the internal package. The added cost is one `hasLock()` call. - `ElementEffect`, the result notifier in `VaadinService` and `Effect`'s background change flag use the new check. - `guidelines/architecture.md` now describes signal effects as checking the session lock instead of trusting the current UI. - Three existing `EffectTest` owner-UI tests now use a plain `UI` with a mocked session that reports the lock as held, instead of a Mockito `UI` mock. The owner UI's internals and session lock are now read, and a Mockito `UI` has no internals. They avoid `MockUI` because it starts a `VaadinService`, which registers a signal environment that stays active for the rest of the test class. - Tests: an effect and a result notifier task triggered while the owner UI is current but unlocked run later with the lock held, and the effect reports a background change. Both tests fail without the fix. Working on behalf of @Artur- --------- Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com> Co-authored-by: caalador <mikael.grankvist@vaadin.com>




Related to #27187, #27188
Hardening · flow-server · signal effects owned by a UI
Signal effects, the signal result notifier and the background change flag treated "this UI is current" as "this thread holds the session lock". Flow's request handling now keeps that rule (#27201, #27439). Code outside Flow can still break it, for example by calling
UI.setCurrentwithout locking the session. Then an effect touched UI state without the lock and failed with "Cannot access state in VaadinSession or UI without locking the session". After that, its error handler failed the same way.Risks:
UI.accessinstead of running right away.isBackgroundChange()reportstruefor that run.When the UI is current and locked, which is every normal case, nothing changes.
✅ No new public API. The shared check is
UIInternals.isCurrentAndLocked(), which is in the internal package. The added cost is onehasLock()call.ElementEffect, the result notifier inVaadinServiceandEffect's background change flag use the new check.guidelines/architecture.mdnow describes signal effects as checking the session lock instead of trusting the current UI.Three existing
EffectTestowner-UI tests now use a plainUIwith a mocked session that reports the lock as held, instead of a MockitoUImock. The owner UI's internals and session lock are now read, and a MockitoUIhas no internals. They avoidMockUIbecause it starts aVaadinService, which registers a signal environment that stays active for the rest of the test class.Tests: an effect and a result notifier task triggered while the owner UI is current but unlocked run later with the lock held, and the effect reports a background change. Both tests fail without the fix.
Working on behalf of @Artur-