UiDetachedException - How cleanup client session correctly

Hi Guys, if a user has closed the browser I see a lot of UiDetachedException’s. I understand the underlying problem, but i have no glue how to react correctly.
In my understanding I want to close the client-session. Therefore I addes a addSessionDestroyListener and call event.getSession().getSession().invalidate();. In my opinion, this code will fire an detach-event and afterwards i´m able to cancel message-subscriptions or event-listereners. But why do I keep getting the error?

If a UI (~ browser window or tab) is closed, the session is not. The session gets destroyed after some timeout once all windows are closed.

So whatever thread/task is accessing your UIs, should be cleaned up a detach listener of some relevant component.

Or add isAttach check to the task (and some potential cleanup code).