Skip to content
Merged
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 @@ -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
Expand Down Expand Up @@ -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.
* <p>
* {@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 {

Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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.
* <p>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<UI> currentUis = new ArrayList<>();
List<Boolean> 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();
Expand Down
6 changes: 4 additions & 2 deletions guidelines/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading