Repository navigation
fix: fire session lock released event without current UI - #27439
Conversation
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
Type of change
|
vaadin-review-bot
left a comment
There was a problem hiding this comment.
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)}. |
There was a problem hiding this comment.
📝 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
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Seems ok except for the JavaDoc gotcha from the review bot
|
@Legioth The Javadoc issue is fixed in c6ce6f8, which was pushed right after your review. |
|
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`).
|
Pull request created: #6790
|
|
Documentation Bot: Draft documentation pull request for this change: vaadin/docsvaadin/docs#6790 Files updated:
It was written from the state of this pull request as you see it now. Please review it and mark it ready for review.
|
…: 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>
…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 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>
#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>



Part of #27187
Related to #27201, #27198
Behavior deviation · flow-server · apps with
SessionLockReleasedEventlisteners (or the deprecatedSessionLockListener.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 throughUI.access.Risks:
UI.getCurrent()returnsnullin released listeners. Before, it returned a UI whose session the thread no longer held, so code using it there was not safe.unlock()after the release.Context. The released event is fired from the lock itself, after the real release.
UI.accessSynchronouslyand the request handlers restore the previous current instances only afterunlock()returns, so the listeners ran in that window.unlock()returns.SessionLockReleasedEventandSessionLockListener.lockReleasedthat 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.guidelines/architecture.md, which said the window afterunlock()runs no application code.Working on behalf of @Artur-