From 02c01b3ae26f79b5d35545f9cfd50cbc2cc4e138 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:00:27 +0000 Subject: [PATCH] fix: fire session lock released event without current UI (#27439) (CP: 25.4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- .../server/InstrumentedReentrantLock.java | 21 +++++++++++++ .../flow/server/SessionLockListener.java | 4 ++- .../flow/server/SessionLockReleasedEvent.java | 15 +++++++++ .../flow/server/SessionLockListenerTest.java | 31 +++++++++++++++++++ guidelines/architecture.md | 6 ++-- 5 files changed, 74 insertions(+), 3 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/InstrumentedReentrantLock.java b/flow-server/src/main/java/com/vaadin/flow/server/InstrumentedReentrantLock.java index 2846009e98d..d9791c7c6f9 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/InstrumentedReentrantLock.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/InstrumentedReentrantLock.java @@ -21,6 +21,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; +import com.vaadin.flow.component.UI; + /** * {@link ReentrantLock} used for Vaadin session locking that notifies the * owning {@link VaadinService}'s {@link SessionLockListener}s around the @@ -59,6 +61,11 @@ * matching acquire. {@code lockReleased} is only fired for a hold whose * acquisition had a listener for it, which also keeps the events balanced when * the service is bound while the lock is held. + *

+ * {@code lockReleased} is fired after the lock has been released, so its + * listeners run without a current {@link UI}: a current UI means that the + * thread holds the lock of its session. The current UI is restored once the + * listeners have run. */ class InstrumentedReentrantLock extends ReentrantLock { @@ -166,12 +173,26 @@ public void unlock() { } super.unlock(); if (fireReleased) { + fireLockReleased(currentService, heldNanos); + } + } + + private void fireLockReleased(VaadinService currentService, + long heldNanos) { + // A UI made current for the locked work is still current here, but + // the lock is no longer held, and a signal effect would take the + // current UI to mean that the listener may touch it directly + UI ui = UI.getCurrent(); + UI.setCurrent(null); + try { // Released is fired in reverse registration order so that the // listeners nest: a listener notified first of the acquisition is // notified last of the release currentService.getEventBus().fireEventInReverseOrder( new SessionLockReleasedEvent(currentService, getSession(), Duration.ofNanos(heldNanos))); + } finally { + UI.setCurrent(ui); } } diff --git a/flow-server/src/main/java/com/vaadin/flow/server/SessionLockListener.java b/flow-server/src/main/java/com/vaadin/flow/server/SessionLockListener.java index 71f655877b1..f7394345918 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/SessionLockListener.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/SessionLockListener.java @@ -88,7 +88,9 @@ default void lockAcquired(SessionLockEvent event) { /** * Invoked on the locking thread immediately after the outermost lock hold * has been released (the lock's hold count reached zero). The time since - * {@link #lockAcquired} is the lock hold time. + * {@link #lockAcquired} is the lock hold time. The callback runs without + * the session lock and without a current UI. Accessing the session from + * this callback calls it again, see {@link SessionLockReleasedEvent}. * * @param event * the session lock event diff --git a/flow-server/src/main/java/com/vaadin/flow/server/SessionLockReleasedEvent.java b/flow-server/src/main/java/com/vaadin/flow/server/SessionLockReleasedEvent.java index 7d2f559be4e..4e44bb9a731 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/SessionLockReleasedEvent.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/SessionLockReleasedEvent.java @@ -17,11 +17,26 @@ import java.time.Duration; +import com.vaadin.flow.component.UI; + /** * Event fired through the {@link VaadinService#getEventBus() service event bus} * when a Vaadin session lock has been released, for the outermost release only. * It is fired in reverse registration order so that listeners nest: a listener * added first is notified of the release last. + *

+ * 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 a UI keeps its own reference to it. + *

+ * Any session access a listener makes takes the lock again and fires the lock + * events again when it is released: on this thread if the lock is free, + * otherwise on the thread that holds it. This includes + * {@link UI#access(Command)}, {@link VaadinSession#access(Command)} and + * changing a signal that a UI's effect depends on, since the effect then runs + * through {@link UI#access(Command)}. A listener that accesses the session on + * every release therefore never stops: it must recognize the release caused by + * its own access and skip it. * * @see AbstractSessionLockEvent * @since 25.3 diff --git a/flow-server/src/test/java/com/vaadin/flow/server/SessionLockListenerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/SessionLockListenerTest.java index 7b64aeb345b..5d9618398d7 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/SessionLockListenerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/SessionLockListenerTest.java @@ -24,10 +24,13 @@ import org.apache.commons.lang3.SerializationUtils; import org.junit.Test; +import com.vaadin.flow.component.UI; +import com.vaadin.flow.internal.CurrentInstance; import com.vaadin.flow.shared.Registration; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -177,6 +180,34 @@ public void lockReleased(SessionLockEvent event) { "released-second", "released-first"), order); } + @Test + public void releasedListener_runsUnlockedWithoutCurrentUi() { + MockVaadinServletService service = new MockVaadinServletService(); + InstrumentedReentrantLock lock = new InstrumentedReentrantLock(service); + List currentUis = new ArrayList<>(); + List locked = new ArrayList<>(); + service.getEventBus().addListener(SessionLockReleasedEvent.class, + event -> { + currentUis.add(UI.getCurrent()); + locked.add(lock.isHeldByCurrentThread()); + }); + UI ui = mock(UI.class); + + try { + lock.lock(); + UI.setCurrent(ui); + lock.unlock(); + + assertEquals(1, currentUis.size()); + assertNull(currentUis.get(0)); + assertEquals(List.of(false), locked); + assertSame("The UI current before unlocking is restored", ui, + UI.getCurrent()); + } finally { + CurrentInstance.clearAll(); + } + } + @Test public void removedListener_isNotNotified() { MockVaadinServletService service = new MockVaadinServletService(); diff --git a/guidelines/architecture.md b/guidelines/architecture.md index 9015c32184e..a0fffcb356e 100644 --- a/guidelines/architecture.md +++ b/guidelines/architecture.md @@ -118,8 +118,10 @@ to copy: lock the session, set the current instances with `CurrentInstance.setCurrent`, run the work, then unlock and restore the previous instances with `CurrentInstance.restoreInstances` in a `finally` block. Restoring right after `unlock()` keeps the session current for as long -as the lock is held, and the window in between is harmless because the access -tasks that `unlock()` runs set their own instances under the lock. A request +as the lock is held. The window in between is harmless: the access tasks that +`unlock()` runs set their own instances under the lock, and the lock clears the +current UI while it fires `SessionLockReleasedEvent`, whose listeners run +after the release. A request handler that sets the current UI while it holds the lock — to find the UI of a UIDL or heartbeat request, or to bootstrap a page — is no different: everything after it unlocks, from writing the response to the request end callbacks, runs