0

I have the following broadcast handler in the MainLayout

@Override
protected void onAttach(AttachEvent attachEvent) {
    super.onAttach(attachEvent);

    SecurityService securityService = serviceFacade.getSecurityService();
    UI ui = attachEvent.getUI();
    if (securityService.isAuthorized()) {
        broadcasterRegistration = serviceFacade.getBroadcasterService().register(securityService.getCurrentUserUuid(), broadcasterMessage -> {
            ui.access(() -> {
                processBroadcasterMessage(broadcasterMessage);
            });
        });
    }
}

and from time to time I see the following exception in the logs:

Exception in thread "pool-3-thread-5" com.vaadin.flow.component.UIDetachedException
    at com.vaadin.flow.component.UI.handleAccessDetach(UI.java:415)
    at com.vaadin.flow.component.UI.access(UI.java:515)
    at com.vaadin.flow.component.UI.access(UI.java:502)
    at com.example.ui.layout.MainLayout.lambda$onAttach$2(MainLayout.java:331)
    at com.example.ui.service.broadcaster.BroadcasterServiceImpl.lambda$broadcast$0(BroadcasterServiceImpl.java:46)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)

Despite the fact of this exception everything works properly, so my question is - should I react somehow on this UIDetachedException exception or not?

alexanoid
  • 24,051
  • 54
  • 210
  • 410

1 Answers1

1

UIDetachedException serves a purpose. It is thrown when you attempt to access closed UI. User has closed the Browser. You should catch the exception, e.g. stop the background task, unregister the broadcast listener.

Tatu Lund
  • 9,949
  • 1
  • 12
  • 26