Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void enableEffect(UI ui) {
.error(new ErrorEvent(e, elem.getNode()));

SerializableExecutor uiDispatcher = command -> {
if (UI.getCurrent() == ui) {
if (ui.getInternals().isCurrentAndLocked()) {
// Run immediately if on the same UI
command.run();
} else {
Expand All @@ -286,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 (UI.getCurrent() == ui) {
if (ui.getInternals().isCurrentAndLocked()) {
command.run();
} else {
ui.access(command::run);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@

private Iterable<DependencyFilter> dependencyFilters;

private boolean atmosphereAvailable = checkAtmosphereSupport();

Check warning on line 187 in flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make this private field "final", it is never reassigned.

See more on https://sonarcloud.io/project/issues?id=vaadin_flow&issues=AaII9Rg0ejW5ErJUKANq&open=AaII9Rg0ejW5ErJUKANq&pullRequest=27188

private BootstrapInitialPredicate bootstrapInitialPredicate;
private BootstrapUrlPredicate bootstrapUrlPredicate;
Expand Down Expand Up @@ -542,7 +542,7 @@
}

return task -> {
if (UI.getCurrent() == owner) {
if (owner.getInternals().isCurrentAndLocked()) {
task.run();
} else {
try {
Expand Down Expand Up @@ -1169,7 +1169,7 @@
}
}
SessionDestroyEvent event = new SessionDestroyEvent(
VaadinService.this, session);

Check warning on line 1172 in flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this redundant qualifier.

See more on https://sonarcloud.io/project/issues?id=vaadin_flow&issues=AaII9Rg0ejW5ErJUKANr&open=AaII9Rg0ejW5ErJUKANr&pullRequest=27188
// Listeners registered on the session are notified before the ones
// registered on the service
session.destroyListeners.forEach(listener -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,42 @@ 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<String> signal = new ValueSignal<>("initial");
List<Boolean> lockedDuringRun = new ArrayList<>();
List<Boolean> backgroundChanges = new ArrayList<>();
Signal.effect(ui, ctx -> {
signal.get();
lockedDuringRun.add(session.hasLock());
backgroundChanges.add(ctx.isBackgroundChange());
});
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");
assertEquals(List.of(false, true), backgroundChanges,
"Change made without the session lock is a background change");
}

@Test
void effect_sharedSignalUpdatedOutsideSessionTransaction_reRunsWithFreshValue() {
// Reproduces issue #24399: while a UIDL request holds the session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> signal = new ValueSignal<>("hello");
Expand All @@ -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<String> signal = new ValueSignal<>("hello");
Expand All @@ -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<String> signal = new ValueSignal<>("initial");
List<Boolean> backgroundChangesA = new ArrayList<>();
Expand Down