From 1a721f9ae521eb96856b06f5d90771ef7bec646c Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:34:59 +0000 Subject: [PATCH 1/2] fix: run element effect inline only when session is locked An effect ran inline whenever its UI was the current UI, also after the request had released the session lock, e.g. in a request end listener, and then failed on the lock check. Fixes #27187 --- .../com/vaadin/flow/dom/ElementEffect.java | 15 +++++++-- .../vaadin/flow/dom/ElementEffectTest.java | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java b/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java index 6fe002d15f0..520f3c9aa45 100644 --- a/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java +++ b/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java @@ -32,6 +32,7 @@ import com.vaadin.flow.function.SerializableExecutor; import com.vaadin.flow.function.SerializableFunction; import com.vaadin.flow.server.ErrorEvent; +import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.shared.Registration; import com.vaadin.flow.signals.DeniedSignalUsageException; import com.vaadin.flow.signals.EffectContext; @@ -273,7 +274,7 @@ private void enableEffect(UI ui) { .error(new ErrorEvent(e, elem.getNode())); SerializableExecutor uiDispatcher = command -> { - if (UI.getCurrent() == ui) { + if (isRunningWithLock(ui)) { // Run immediately if on the same UI command.run(); } else { @@ -286,7 +287,7 @@ private void enableEffect(UI ui) { // UI.getCurrent() is already set. In that case // run directly instead of ui.access() which // would only enqueue without executing. - if (UI.getCurrent() == ui) { + if (isRunningWithLock(ui)) { command.run(); } else { ui.access(command::run); @@ -314,6 +315,16 @@ private void enableEffect(UI ui) { effect.setOwnerUI(ui); } + /* + * The current UI alone is not enough: code that runs after a request has + * released the session lock, such as a request end listener, still has the + * UI of the request as the current UI. + */ + private static boolean isRunningWithLock(UI ui) { + VaadinSession session = ui.getSession(); + return UI.getCurrent() == ui && session != null && session.hasLock(); + } + private void disableEffect() { if (effect != null) { effect.passivate(); diff --git a/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java b/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java index 1fc443ba5dc..583383e5510 100644 --- a/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java @@ -458,6 +458,38 @@ void effect_triggeredWithOtherUILocked_effectRunAsynchronously() { "Effect should run with correct UI context"); } + @Test + void effect_triggeredWithOwnerUICurrentButSessionUnlocked_effectRunWithLock() { + // Code that runs after a request has released the session lock, + // such as a request end listener, still has the UI of the request + // as the current UI + CurrentInstance.clearAll(); + VaadinService.setCurrent(service); + + var session = new MockVaadinSession(service); + session.lock(); + VaadinSession.setCurrent(session); + var ui = new MockUI(session); + + ValueSignal signal = new ValueSignal<>("initial"); + List lockedDuringRun = new ArrayList<>(); + Signal.effect(ui, () -> { + signal.get(); + lockedDuringRun.add(session.hasLock()); + }); + session.unlock(); + assertSame(ui, UI.getCurrent()); + + signal.set("changed"); + assertEquals(List.of(true), lockedDuringRun, + "Effect must not run inline without the session lock"); + + service.flushExecutorAndAccessTasks(session); + + assertEquals(List.of(true, true), lockedDuringRun, + "Effect should run once more with the session lock held"); + } + @Test void effect_sharedSignalUpdatedOutsideSessionTransaction_reRunsWithFreshValue() { // Reproduces issue #24399: while a UIDL request holds the session From 4ed85a21725b8526ea82a98e1f12d884e64c9c14 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:38:59 +0000 Subject: [PATCH 2/2] fix: apply same lock check to signal result notifier and background flag --- .../flow/component/internal/UIInternals.java | 15 +++++++++++ .../com/vaadin/flow/dom/ElementEffect.java | 15 ++--------- .../com/vaadin/flow/server/VaadinService.java | 2 +- .../com/vaadin/flow/signals/impl/Effect.java | 3 ++- .../vaadin/flow/dom/ElementEffectTest.java | 6 ++++- ...aadinServiceSignalsInitializationTest.java | 27 +++++++++++++++++++ .../vaadin/flow/signals/impl/EffectTest.java | 9 ++++--- 7 files changed, 57 insertions(+), 20 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java b/flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java index a890b39ca8f..26deec9096e 100644 --- a/flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java +++ b/flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java @@ -1353,6 +1353,21 @@ public VaadinSession getSession() { return session; } + /** + * Checks whether the current thread may use the related UI directly: the UI + * is the current UI and the current thread holds the lock of its session. + *

+ * The current UI alone is not enough, as code that runs after a request has + * released the session lock, such as a request end listener, still has the + * UI of the request as the current UI. + * + * @return {@code true} if the related UI is the current UI and its session + * is locked by the current thread, {@code false} otherwise + */ + public boolean isCurrentAndLocked() { + return UI.getCurrent() == ui && session != null && session.hasLock(); + } + private static Logger getLogger() { return LoggerFactory.getLogger(UIInternals.class.getName()); } diff --git a/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java b/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java index 520f3c9aa45..6d845dd1b32 100644 --- a/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java +++ b/flow-server/src/main/java/com/vaadin/flow/dom/ElementEffect.java @@ -32,7 +32,6 @@ import com.vaadin.flow.function.SerializableExecutor; import com.vaadin.flow.function.SerializableFunction; import com.vaadin.flow.server.ErrorEvent; -import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.shared.Registration; import com.vaadin.flow.signals.DeniedSignalUsageException; import com.vaadin.flow.signals.EffectContext; @@ -274,7 +273,7 @@ private void enableEffect(UI ui) { .error(new ErrorEvent(e, elem.getNode())); SerializableExecutor uiDispatcher = command -> { - if (isRunningWithLock(ui)) { + if (ui.getInternals().isCurrentAndLocked()) { // Run immediately if on the same UI command.run(); } else { @@ -287,7 +286,7 @@ private void enableEffect(UI ui) { // UI.getCurrent() is already set. In that case // run directly instead of ui.access() which // would only enqueue without executing. - if (isRunningWithLock(ui)) { + if (ui.getInternals().isCurrentAndLocked()) { command.run(); } else { ui.access(command::run); @@ -315,16 +314,6 @@ private void enableEffect(UI ui) { effect.setOwnerUI(ui); } - /* - * The current UI alone is not enough: code that runs after a request has - * released the session lock, such as a request end listener, still has the - * UI of the request as the current UI. - */ - private static boolean isRunningWithLock(UI ui) { - VaadinSession session = ui.getSession(); - return UI.getCurrent() == ui && session != null && session.hasLock(); - } - private void disableEffect() { if (effect != null) { effect.passivate(); diff --git a/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java b/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java index 6c97d95c4d4..0d7d0eb48af 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java @@ -542,7 +542,7 @@ private Executor createCurrentUiDispatcher() { } return task -> { - if (UI.getCurrent() == owner) { + if (owner.getInternals().isCurrentAndLocked()) { task.run(); } else { try { diff --git a/flow-server/src/main/java/com/vaadin/flow/signals/impl/Effect.java b/flow-server/src/main/java/com/vaadin/flow/signals/impl/Effect.java index 459d504879a..5f7eedfe335 100644 --- a/flow-server/src/main/java/com/vaadin/flow/signals/impl/Effect.java +++ b/flow-server/src/main/java/com/vaadin/flow/signals/impl/Effect.java @@ -271,7 +271,8 @@ private boolean onDependencyChange(boolean immediate) { } if (ownerUI != null) { - invalidatedFromBackground = UI.getCurrent() != ownerUI; + invalidatedFromBackground = !ownerUI.getInternals() + .isCurrentAndLocked(); } else { invalidatedFromBackground = VaadinRequest.getCurrent() == null; } diff --git a/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java b/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java index 583383e5510..6bc28e77442 100644 --- a/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/dom/ElementEffectTest.java @@ -473,9 +473,11 @@ void effect_triggeredWithOwnerUICurrentButSessionUnlocked_effectRunWithLock() { ValueSignal signal = new ValueSignal<>("initial"); List lockedDuringRun = new ArrayList<>(); - Signal.effect(ui, () -> { + List backgroundChanges = new ArrayList<>(); + Signal.effect(ui, ctx -> { signal.get(); lockedDuringRun.add(session.hasLock()); + backgroundChanges.add(ctx.isBackgroundChange()); }); session.unlock(); assertSame(ui, UI.getCurrent()); @@ -488,6 +490,8 @@ void effect_triggeredWithOwnerUICurrentButSessionUnlocked_effectRunWithLock() { assertEquals(List.of(true, true), lockedDuringRun, "Effect should run once more with the session lock held"); + assertEquals(List.of(false, true), backgroundChanges, + "Change made without the session lock is a background change"); } @Test diff --git a/flow-server/src/test/java/com/vaadin/flow/server/VaadinServiceSignalsInitializationTest.java b/flow-server/src/test/java/com/vaadin/flow/server/VaadinServiceSignalsInitializationTest.java index ac3d14cd51d..5f85e53afea 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/VaadinServiceSignalsInitializationTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/VaadinServiceSignalsInitializationTest.java @@ -107,6 +107,33 @@ record EffectExecution(UI ui, String threadName) { "Expected effect to be executed in Vaadin Executor thread"); } + @Test + void resultNotifier_ownerUiCurrentButSessionUnlocked_taskRunWithLock() + throws InterruptedException { + var service = new MockVaadinServletService(); + VaadinService.setCurrent(service); + + var session = new MockVaadinSession(service); + session.lock(); + var ui = new MockUI(session); + var dispatcher = SignalEnvironment.getCurrentResultNotifier(); + session.unlock(); + // Like after a request has released the session lock + assertSame(ui, UI.getCurrent()); + + AtomicBoolean lockedDuringRun = new AtomicBoolean(); + CountDownLatch latch = new CountDownLatch(1); + dispatcher.execute(() -> { + lockedDuringRun.set(session.hasLock()); + latch.countDown(); + }); + + assertTrue(latch.await(500, TimeUnit.MILLISECONDS), + "Expected task to be executed"); + assertTrue(lockedDuringRun.get(), + "Expected task to run with the session lock held"); + } + @Test void resultNotifier_ownerUiIsClosing_taskNotScheduled() throws InterruptedException { diff --git a/flow-server/src/test/java/com/vaadin/flow/signals/impl/EffectTest.java b/flow-server/src/test/java/com/vaadin/flow/signals/impl/EffectTest.java index 14668015132..66d37bb358b 100644 --- a/flow-server/src/test/java/com/vaadin/flow/signals/impl/EffectTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/signals/impl/EffectTest.java @@ -45,6 +45,7 @@ import com.vaadin.flow.signals.shared.SharedMapSignal; import com.vaadin.flow.signals.shared.SharedValueSignal; import com.vaadin.flow.signals.shared.SharedValueSignalTest.AsyncSharedValueSignal; +import com.vaadin.tests.util.MockUI; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -841,7 +842,7 @@ public Registration onNextChange( @Test void ownerUI_sameUICurrent_notBackgroundChange() { - UI ui = mock(UI.class); + UI ui = new MockUI(); UI.setCurrent(ui); ValueSignal signal = new ValueSignal<>("hello"); @@ -865,7 +866,7 @@ void ownerUI_sameUICurrent_notBackgroundChange() { @Test void ownerUI_noUICurrent_isBackgroundChange() { - UI ui = mock(UI.class); + UI ui = new MockUI(); UI.setCurrent(ui); ValueSignal signal = new ValueSignal<>("hello"); @@ -889,8 +890,8 @@ void ownerUI_noUICurrent_isBackgroundChange() { @Test void ownerUI_differentUICurrent_isBackgroundChange() { - UI uiA = mock(UI.class); - UI uiB = mock(UI.class); + UI uiA = new MockUI(); + UI uiB = new MockUI(); ValueSignal signal = new ValueSignal<>("initial"); List backgroundChangesA = new ArrayList<>();