Skip to content

fix: fire session lock released event without current UI - #27439

Merged
Artur- merged 3 commits into
mainfrom
fix/session-lock-released-without-current-ui
Nov 26, 2026
Merged

Artur- merged 3 commits into
mainfrom
fix/session-lock-released-without-current-ui

Conversation

@totally-not-ai

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

Copy link
Copy Markdown
Contributor

Part of #27187
Related to #27201, #27198

Behavior deviation · flow-server · apps with SessionLockReleasedEvent listeners (or the deprecated SessionLockListener.lockReleased)

A session lock released listener runs after the lock is released, but the UI that the locked work made current, for example inside UI.access, was still current. A listener that changed a shared signal then ran that UI's signal effects without the lock, and they failed with "Cannot access state in VaadinSession or UI without locking the session". Released listeners now run with no current UI, so such effects go through UI.access.

Risks:

  • ⚠️ Behaviour change: UI.getCurrent() returns null in released listeners. Before, it returned a UI whose session the thread no longer held, so code using it there was not safe.
  • 🧵 Threading: restores the rule that a current UI means its session is locked by this thread, for the one callback that runs inside unlock() after the release.
  • ✅ No public API, security, memory, serialization or performance impact.

Context. The released event is fired from the lock itself, after the real release. UI.accessSynchronously and the request handlers restore the previous current instances only after unlock() returns, so the listeners ran in that window.

  • The session lock clears the current UI while it fires the released event and restores it afterwards, so callers see the same current instances as before once unlock() returns.
  • Documented in SessionLockReleasedEvent and SessionLockListener.lockReleased that listeners run without the lock and without a current UI, and that any session access from a listener, signal changes that run effects included, fires the released event again, so a listener must skip the release caused by its own access.
  • Updated guidelines/architecture.md, which said the window after unlock() runs no application code.
  • Added a test that a released listener sees no current UI and no lock, and that the caller's UI is current again after unlocking.

Working on behalf of @Artur-

Released listeners run after the lock is released, but a UI made
current for the locked work, e.g. by UI.access, was still current. A
listener that changed a shared signal then ran the UI's effects inline
without the lock.

Part of #27187
@totally-not-ai
totally-not-ai Bot requested a review from Artur- November 26, 2026 14:36
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Type of change

  • Bugfix

@Artur-
Artur- marked this pull request as ready for review November 26, 2026 14:37
@Artur-
Artur- requested a review from Legioth November 26, 2026 14:37

@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
📝 The javadoc recommends UI.access from a released listener, which re-enters the listener on the same thread

* Listeners run on the thread that released the lock, after the release, so
* they do not hold the session lock and {@link UI#getCurrent()} returns
* {@code null}. A listener that needs to touch a UI goes through
* {@link UI#access(Command)}.

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.

📝 The javadoc recommends UI.access from a released listener, which re-enters the listener on the same thread

UI.access always enqueues and then calls ensureAccessQueuePurged, which takes the lock and releases it again. Since the hold started with a released-event listener registered, that release fires SessionLockReleasedEvent again on the same thread, so a listener that follows the advice on every release recurses until the stack overflows (and the error escapes unlock(), because the event bus only catches Exception).

Worth saying in the javadoc that the callback is re-entered by any session access it makes, so a listener must guard against that. A second gap in the same sentence: with no current UI and getSession() unusable without the lock, the doc never says where the listener is supposed to get the UI from.

SessionLockReleasedEvent.java:31 · documentation

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.

Right, a session access takes the lock and releases it again, so following that advice on every release never ends. I removed the recommendation. The Javadoc now says three things. A listener that needs a UI keeps its own reference to it. Any session access the listener makes fires the lock events again: UI.access, VaadinSession.access, and changing a signal that a UI's effect depends on. The events fire on this thread when the lock is free, otherwise on the thread that holds it. So a listener that accesses the session must recognize the release caused by its own access and skip it. The deprecated SessionLockListener.lockReleased now points to the same note.

@Legioth Legioth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems ok except for the JavaDoc gotcha from the review bot

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

@Legioth The Javadoc issue is fixed in c6ce6f8, which was pushed right after your review. SessionLockReleasedEvent no longer recommends UI.access. It now says that any session access from a listener fires the released event again, including a signal change that runs an effect, so a listener has to skip the release caused by its own access. The deprecated SessionLockListener.lockReleased points to the same note.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 706 files   - 30   1 796 suites   - 30   2h 6m 29s ⏱️ - 10m 37s
13 803 tests  -  2  13 731 ✅  -  2  72 💤 ±0  0 ❌ ±0 
14 481 runs   - 65  14 409 ✅  - 65  72 💤 ±0  0 ❌ ±0 

Results for commit c6ce6f8. ± Comparison against base commit 2f4cede.

@sonarqubecloud

Copy link
Copy Markdown

@Artur-
Artur- added this pull request to the merge queue Nov 26, 2026
Merged via the queue into main with commit c134760 Nov 26, 2026
68 checks passed
@Artur-
Artur- deleted the fix/session-lock-released-without-current-ui branch November 26, 2026 16:44
vaadin-bot added a commit to vaadin/docs that referenced this pull request Nov 26, 2026
Documents that SessionLockReleasedEvent listeners run after the lock is
released without a current UI, and that session access from such a
listener re-fires the release event, on the Service Event Bus page.

Documents vaadin/flow#27439 (`c6ce6f818e6ed2cdb36f12e1e23bae7b1907c10e`).
@github-actions

Copy link
Copy Markdown
Contributor

Pull request created: #6790

Generated by Documentation Bot · agent · 86.1 AIC · ⊞ 11.7K

@github-actions

Copy link
Copy Markdown
Contributor

Documentation Bot: Draft documentation pull request for this change: vaadin/docsvaadin/docs#6790

Files updated:

  • articles/flow/advanced/session-lock-and-rpc-listeners.adoc

It was written from the state of this pull request as you see it now. Please review it and mark it ready for review.

Generated by Documentation Bot for #27439 · agent · 86.1 AIC · ⊞ 11.7K · ◷

vaadin-bot added a commit that referenced this pull request Nov 26, 2026
…: 25.4) (#27453)

This PR cherry-picks changes from the original PR #27439 to branch 25.4.

---

> Part of #27187
> Related to #27201, #27198
> 
> **Behavior deviation** · flow-server · apps with
`SessionLockReleasedEvent` listeners (or the deprecated
`SessionLockListener.lockReleased`)
> 
> A session lock released listener runs after the lock is released, but
the UI that the locked work made current, for example inside
`UI.access`, was still current. A listener that changed a shared signal
then ran that UI's signal effects without the lock, and they failed with
"Cannot access state in VaadinSession or UI without locking the
session". Released listeners now run with no current UI, so such effects
go through `UI.access`.
> 
> **Risks:**
> - ⚠️ Behaviour change: `UI.getCurrent()` returns `null` in released
listeners. Before, it returned a UI whose session the thread no longer
held, so code using it there was not safe.
> - 🧵 Threading: restores the rule that a current UI means its session
is locked by this thread, for the one callback that runs inside
`unlock()` after the release.
> - ✅ No public API, security, memory, serialization or performance
impact.
> 
> **Context.** The released event is fired from the lock itself, after
the real release. `UI.accessSynchronously` and the request handlers
restore the previous current instances only after `unlock()` returns, so
the listeners ran in that window.
> 
> - The session lock clears the current UI while it fires the released
event and restores it afterwards, so callers see the same current
instances as before once `unlock()` returns.
> - Documented in `SessionLockReleasedEvent` and
`SessionLockListener.lockReleased` that listeners run without the lock
and without a current UI, and that any session access from a listener,
signal changes that run effects included, fires the released event
again, so a listener must skip the release caused by its own access.
> - Updated `guidelines/architecture.md`, which said the window after
`unlock()` runs no application code.
> - Added a test that a released listener sees no current UI and no
lock, and that the caller's UI is current again after unlocking.
> 
> Working on behalf of @Artur-
> 
>

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
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>
mshabarov pushed a commit to OriginalFelix/flow that referenced this pull request Dec 1, 2026
)

Related to vaadin#27187, vaadin#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 (vaadin#27201, vaadin#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>
peholmst pushed a commit to vaadin/docs that referenced this pull request Dec 2, 2026
#6790)

Documentation for vaadin/flow#27439 by `@totally-not-ai`[bot].

> [!NOTE]
> The source pull request is merged, so this documentation describes the
final shape of the change. Please review it and mark it ready for
review.

**Change categories:** BEHAVIOR_CHANGE

| File | Change |
|------|--------|
| `articles/flow/advanced/session-lock-and-rpc-listeners.adoc` |
Documents that `SessionLockReleasedEvent` listeners (and the deprecated
`SessionLockListener.lockReleased`) run after the lock is released
without a current `UI`, and that session access from such a listener
re-fires the release event, so a listener that accesses the session on
every release must guard against it. |

Auto-generated by the Documentation Bot — review before merging.

> Generated by [Documentation
Bot](https://github.com/vaadin/flow/actions/runs/45861501077) for #27439
· agent · 86.1 AIC · ⊞ 11.7K ·
[◷](https://github.com/search?q=repo%3Avaadin%2Fdocs+%22gh-aw-workflow-id%3A+doc-bot%22&type=pullrequests)
> - [x] expires <!-- gh-aw-expires: 2026-12-26T16:58:00.179Z --> on Dec
26, 2026, 4:58 PM UTC

<!-- gh-aw-agentic-workflow: Documentation Bot, engine: claude, model:
agent, id: 45861501077, workflow_id: doc-bot, run:
https://github.com/vaadin/flow/actions/runs/45861501077 -->

<!-- gh-aw-expires-type: pull-request -->

<!-- gh-aw-workflow-id: doc-bot -->
<!-- gh-aw-workflow-call-id: vaadin/flow/doc-bot -->

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.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