Skip to content

fix: run signal effects inline only when session is locked - #27460

Merged
Artur- merged 6 commits into
mainfrom
fix/signal-effects-require-session-lock
Nov 30, 2026
Merged

Artur- merged 6 commits into
mainfrom
fix/signal-effects-require-session-lock

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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-

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 vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

The three Sonar findings in VaadinService (lines 188, 1173 and 2275) are on lines this PR does not change. This PR only changes the result notifier check around line 546, so I left them out to keep the PR to one concern.

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 709 files  ±0   1 799 suites  ±0   2h 12m 28s ⏱️ +20s
13 919 tests +2  13 847 ✅ +2  72 💤 ±0  0 ❌ ±0 
14 597 runs  +2  14 525 ✅ +2  72 💤 ±0  0 ❌ ±0 

Results for commit 8403f18. ± Comparison against base commit e23aabe.

♻️ This comment has been updated with latest results.

@Artur-
Artur- requested review from vaadin-review-bot and removed request for vaadin-review-bot November 28, 2026 13:43
Artur-
Artur- previously approved these changes Nov 28, 2026

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@vaadin-review-bot Updated the architecture guideline. The UI.getCurrent() paragraph no longer uses signal effects as an example of framework code that trusts the current UI. It now names them as the exception: they run inline only when the UI is current and its session is locked by the current thread, and otherwise go through UI.access. The point about application code is unchanged.

@Artur-
Artur- requested review from vaadin-review-bot and removed request for vaadin-review-bot November 28, 2026 14:03

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@Artur-
Artur- enabled auto-merge November 30, 2026 10:15
@Artur-
Artur- added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit a64d3aa Nov 30, 2026
95 of 98 checks passed
@Artur-
Artur- deleted the fix/signal-effects-require-session-lock branch November 30, 2026 11:02
@github-actions

Copy link
Copy Markdown
Contributor

Documentation Bot: No documentation needed for this change.

This is a hardening fix classified as BEHAVIOR_CHANGE + INTERNAL_ONLY: it adds an internal UIInternals.isCurrentAndLocked() check so signal effects, the signal result notifier, and the background-change flag only run inline when the owning UI's session is actually locked by the current thread. The behavior only differs when code outside Flow already violates the documented session-locking contract (e.g. calling UI.setCurrent without locking the session); in every normal case nothing changes, and no public API is added or modified.

Nothing was opened in vaadin/docs. If you disagree, the change has to be documented by hand there.

Generated by Documentation Bot for #27460 · agent · 39.1 AIC · ⊞ 11.7K · ◷

vaadin-bot pushed a commit that referenced this pull request Nov 30, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants