Skip to content
Open
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
72 changes: 0 additions & 72 deletions flow-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void setPushMode(PushMode pushMode) {
"Cannot set the push mode for a detached UI");
}

session.checkHasLock();
session.checkHasUIAccess(ui);

if (pushMode.isEnabled()
&& !session.getService().ensurePushAvailable()) {
Expand Down
78 changes: 72 additions & 6 deletions flow-server/src/main/java/com/vaadin/flow/component/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import com.vaadin.flow.server.Command;
import com.vaadin.flow.server.ErrorEvent;
import com.vaadin.flow.server.ErrorHandlingCommand;
import com.vaadin.flow.server.FutureAccess;
import com.vaadin.flow.server.HttpStatusCode;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
Expand Down Expand Up @@ -443,8 +444,11 @@ public void close() {
// the UI is detached and cleaned up.

// Can't use UI.push() directly since it checks for a valid session
if (getSession() != null) {
getSession().getService().runPendingAccessTasks(getSession());
VaadinSession session = getSession();
if (session != null && session.isUILockingEnabled()) {
getInternals().runPendingAccessTasks();
} else if (session != null) {
session.getService().runPendingAccessTasks(session);
}
pushConnection.push();
}
Expand Down Expand Up @@ -593,6 +597,11 @@ private void accessSynchronously(Command command,

VaadinService.verifyNoOtherSessionLocked(session);

if (session.isUILockingEnabled()) {
accessSynchronouslyWithUILock(session, command, detachHandler);
return;
}

session.lock();
try {
if (getSession() == null) {
Expand All @@ -612,6 +621,46 @@ private void accessSynchronously(Command command,

}

/**
* Runs the command while holding only the lock of this UI, used when the
* session uses UI locking.
*/
private void accessSynchronouslyWithUILock(VaadinSession session,
Command command, SerializableRunnable detachHandler) {
UIInternals internals = getInternals();
if (!internals.hasUILock() && session.hasAccess()) {
/*
* Threads holding a UI lock may wait for the session lock, so
* waiting for this UI lock while holding the session lock or the
* lock of another UI could deadlock.
*/
if (!internals.tryLockUI()) {
throw new IllegalStateException(
"Cannot synchronously access a UI that is locked by another thread "
+ "while holding the session lock or the lock of another UI. "
+ "Use UI.access() instead.");
}
} else {
internals.lockUI();
}

Map<Class<?>, CurrentInstance> old = null;
try {
if (getSession() == null) {
// UI was detached before the lock was acquired
handleAccessDetach(detachHandler);
return;
}
old = CurrentInstance.setCurrent(this);
command.execute();
} finally {
internals.unlockUI();
if (old != null) {
CurrentInstance.restoreInstances(old);
}
}
}

/**
* Provides exclusive access to this UI from outside a request handling
* thread.
Expand All @@ -621,6 +670,10 @@ private void accessSynchronously(Command command,
* be acquired and the command is run right away. If the session is
* currently locked, the command will be run before that lock is released.
* <p>
* With {@link VaadinSession#isUILockingEnabled() UI locking}, only the lock
* of this UI is held instead of the session lock, and the command is run
* before the UI lock is released.
* <p>
* RPC handlers for components inside this UI do not need to use this method
* as the session is automatically locked by the framework during RPC
* handling.
Expand Down Expand Up @@ -670,7 +723,7 @@ private Future<Void> access(Command command,
return null;
}

return session.access(new ErrorHandlingCommand() {
ErrorHandlingCommand task = new ErrorHandlingCommand() {
@Override
public void execute() {
accessSynchronously(command, detachHandler);
Expand Down Expand Up @@ -710,7 +763,16 @@ public void handleError(Exception exception) {
getLogger().error(e.getMessage(), e);
}
}
});
};

if (session.isUILockingEnabled()) {
// Only this UI needs to be locked, the session lock is not used
FutureAccess future = new FutureAccess(session, task);
getInternals().getPendingAccessQueue().add(future);
getInternals().ensureAccessQueuePurged();
return future;
}
return session.access(task);
}

/**
Expand Down Expand Up @@ -890,7 +952,7 @@ public void push() {
if (session == null) {
throw new UIDetachedException("Cannot push a detached UI");
}
session.checkHasLock();
session.checkHasUIAccess(this);

if (!getPushConfiguration().getPushMode().isEnabled()) {
throw new IllegalStateException("Push not enabled");
Expand All @@ -904,7 +966,11 @@ public void push() {
* when the push would otherwise be ignored because there are no changes
* to push.
*/
session.getService().runPendingAccessTasks(session);
if (session.isUILockingEnabled()) {
getInternals().runPendingAccessTasks();
} else {
session.getService().runPendingAccessTasks(session);
}

if (!getInternals().isDirty()
|| getInternals().getStateTree().isPreparingForResync()) {
Expand Down
Loading