onDetach() not called on browser close. Ideas for handling?

Hello :)
Setup:

  • Java version: 21
  • Vaadin version: 24.8
  • Browser: Google Chrome 138.0.7204.101
    I’m not sure whether this issue matches the ones mentioned below, or if I’m misunderstanding how onDetach() is intended to behave.

Related discussions I’ve reviewed:

I believe the StackOverflow post may be outdated, as it refers to the Beacon API when it was still an add-on, not part of the core Vaadin framework.

In my case, I have a virtual thread that polls a backend job every five seconds and updates the UI. These jobs can run for a long time. I’ve overridden the onDetach() method to stop the thread once the UI is no longer active, in order to avoid wasting resources. This works well when I navigate within the app — the onDetach() method is called, and the thread stops as expected. However, if the user closes the browser tab or the browser itself, the thread continues running until the job completes, since onDetach() is not called, which is not ideal.

Am I expecting too much from onDetach() in this scenario? Is there a more reliable way to detect when the user closes the tab or browser so I can stop the thread accordingly?

Yes, it’s not really “reliable” in that sense that is is guaranteed to run IMMEDIATELY. It takes time…

If you need immediately feedback this add-on of @Matti might be helpful `PageVisibility` in Viritin – Because polling in the void is just sad

Thanks for showing me this topic. I will check it in detail and try using some of the suggested solutions. I am getting back to your first sentence. As you said, “It is not guaranteed to run immediately”, when should I expect it to run then? I just tried testing closing a tab in the first minute of a process that takes 10 minutes, and onDetach() was never called. How much time is it expected to take?

It takes up to three missed heartbeat which can take up to 15min by default or longer/less depending on your configuration

I tried setting the heartbeat to 1 minute as you have suggested how in other forum topics, verified that this is the case via the network tab, started a long process, closed the tab, but detach is not called even though more than 7-8 minutes have passed. Could it have something to do with the thread that updates the UI information, even though a browser is not opened, to somehow still have a heartbeat?

My guess above is wrong, since I tried setting the heartbeat’s value to 15 seconds, and the thread’s update to 2 minutes, and still, the updates are happening and onDetach is not called, even though the browser tab is closed for more than three heartbeats.

Do you have closeIdleSessions enabled? If not… the default session timeout comes way later. It’s also in detail explained here Vaadin Session Timeout / Heartbeats – Martin Vysny – First Principles Thinking

Note that the missed heartbeats +closeIdleSessions=true will cause cleanup only if you have another tab open to the same session. Otherwise you will need to wait until session timeout.

I did not have closeIdleSessions as enabled.
After the test I conducted and the last two replies, I can confirm what Olli said.
To summarize what I have learned, if I have two tabs opened and I close one of them, on detach is called after a few missed heartbeats, but if I have only one tab and close it, it is not.

It should be gone after the session times out, which is normally 30min

And of course you can configure a shorter session timeout; 30 minutes is quite a lot.

1 Like

Thank you both for the quick responses and info. :pray: