Skip to content

fix: fire session lock released event without current UI (#27439) (CP: 25.4) - #27453

Merged
vaadin-bot merged 1 commit into
25.4from
cherry-pick-27439-to-25.4-1795711523778
Nov 26, 2026
Merged

vaadin-bot merged 1 commit into
25.4from
cherry-pick-27439-to-25.4-1795711523778

Conversation

@vaadin-bot

Copy link
Copy Markdown
Collaborator

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-

…: 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>
@github-actions

Copy link
Copy Markdown
Contributor

The release listener now runs with no current UI, bracketed around the event

sequenceDiagram
    participant C as locked work (e.g. UI.access task)
    participant L as InstrumentedReentrantLock.unlock()
    participant E as SessionLockReleasedEvent listener
    C->>L: unlock()
    L->>L: super.unlock() releases the lock
    L->>L: UI.setCurrent(null) (new)
    L->>E: fireEventInReverseOrder(SessionLockReleasedEvent)
    Note over E: UI.getCurrent() is null here, was: the caller's UI, though the lock was already released
    E-->>L: listener returns
    L->>L: UI.setCurrent(ui) restores the caller's UI (new)
    L-->>C: unlock() returns
Loading

InstrumentedReentrantLock.unlock() already released the lock before firing SessionLockReleasedEvent, but left whatever UI the locked work had made current still current while listeners ran. The new fireLockReleased wraps the fireEventInReverseOrder call with UI.setCurrent(null) and a finally that restores the prior UI, so UI.getCurrent() is null for the duration of the listener call and the caller sees its own UI current again once unlock() returns. Per the PR description, this keeps the invariant that a current UI implies the thread holds that UI's session lock.

Diagram Bot draws the mechanism this pull request touches; it does not review the change. Verify it against the diff.

Generated by Diagram Bot for #27453 · agent · 39.5 AIC · ⊞ 9.2K · ◷

@vaadin-bot

Copy link
Copy Markdown
Collaborator Author

This PR is eligible for auto-merging policy, so it has been approved automatically. If there are pending conditions, auto merge (with 'squash' method) has been enabled for this PR [Message is sent from bot]

@vaadin-bot
vaadin-bot enabled auto-merge (squash) November 26, 2026 17:21
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 705 files   1 795 suites   2h 13m 32s ⏱️
13 800 tests 13 728 ✅ 72 💤 0 ❌
14 478 runs  14 406 ✅ 72 💤 0 ❌

Results for commit 02c01b3.

@vaadin-bot
vaadin-bot merged commit eb57189 into 25.4 Nov 26, 2026
58 checks passed
@vaadin-bot
vaadin-bot deleted the cherry-pick-27439-to-25.4-1795711523778 branch November 26, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants