Do you have to lock the session before calling removeAll() in a layout?

I’m getting an exception from the RemoveAll() statement in the displayWod method. I understand the need to lock the session in the hourly method when I get a session attribute, buy why the need to lock it just to remove components?

 @Scheduled(cron = "0 0 * * * ?")
    public void hourly() {
        String userTimezone = null;
        if (vaadinSession != null) {
            vaadinSession.lock();
            try {
                userTimezone = (String) vaadinSession.getAttribute(USER_TIMEZONE);
            } finally {
                vaadinSession.unlock();
            }
        }
        if (userTimezone != null) {
            ZoneId zoneId = ZoneId.of(userTimezone);
            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), zoneId);
            if (zonedDateTime.getHour() == 0)
                wodView.displayWod(zonedDateTime); //line 64
        }
    }
 public void displayWod(ZonedDateTime zonedDateTime) {
        removeAll(); //line 44
		// additional code omitted
java.lang.IllegalStateException: Cannot access state in VaadinSession or UI without locking the session.
	at com.vaadin.flow.server.VaadinSession.checkHasLock(VaadinSession.java:518) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.server.VaadinSession.checkHasLock(VaadinSession.java:532) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.StateTree.checkHasLock(StateTree.java:408) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.StateTree.markAsDirty(StateTree.java:279) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.StateNode.markAsDirty(StateNode.java:548) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.nodefeature.NodeList.addChange(NodeList.java:277) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.nodefeature.NodeList.clear(NodeList.java:403) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.nodefeature.StateNodeNodeList.clear(StateNodeNodeList.java:77) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.internal.nodefeature.ElementChildrenList.clear(ElementChildrenList.java:57) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.dom.impl.AbstractNodeStateProvider.removeAllChildren(AbstractNodeStateProvider.java:115) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.dom.Node.removeAllChildren(Node.java:363) ~[flow-server-2.1.9.jar:2.1.9]

	at com.vaadin.flow.component.HasComponents.removeAll(HasComponents.java:102) ~[flow-server-2.1.9.jar:2.1.9]

	at com.wodiq.ui.views.WodView.displayWod(WodView.java:44) ~[classes/:na]

	at com.wodiq.logic.Cron.hourly(Cron.java:64) ~[classes/:na]

Yes, it’s needed to ensure no more than just one thread modifies UI elements concurrently.
I don’t know the exact details but just think about an ArrayList or a HashSet with children elements. If two threads concurrently remove or add children to the map the Array/HashSet would throw an ConcurrentModificationException.

Therefore some sort of synchronization is needed. The contract is that all UI/Session work is being done in threads with a locked UI.

Read
https://vaadin.com/docs/v14/flow/advanced/tutorial-push-access.html
for more info on this topic.

Regards