flow icon indicating copy to clipboard operation
flow copied to clipboard

onDetach() is not called after page closed and hearbeats stopped, even for a long time

Open bardyamomeni opened this issue 1 year ago • 4 comments

Description of the bug

When a page is closed, heartbeats stop, but the UI is not destroyed and detached, even after a long time.

Expected behavior

the UI should get detached after a finite amount of time after the last heartbeat

Minimal reproducible example

class UiServletInitListener : VaadinServiceInitListener {
    
     override fun serviceInit(serviceInitEvent: ServiceInitEvent) {
        
           serviceInitEvent.source.addUIInitListener { e: UIInitEvent ->
                e.ui.addHeartbeatListener {
                   println("HEARTBEAT ${it.heartbeatTime}")
                }
               e.ui.addDetachListener {
                   println("DETACHED") //never gets called. even after 10x times heartbeat interval later of the last heartbeat
                }
        }
    }
}

Versions

  • Vaadin / Flow version: 23.1.3
  • Java version: 17
  • OS version: macOS 12
  • Browser version (if applicable): Firefox
  • Application Server (if applicable): Spring Boot 2.7.2 + rsocket + vaadin
  • IDE (if applicable): IntelliJ IDEA

bardyamomeni avatar Jul 30 '22 21:07 bardyamomeni

@bardyamomeni thanks for creating this issue. If "when a page is closed" means closing the browser's tab, I think this might not be exactly a bug, but more of an enhancement request since currently the support for this is not implemented yet, similar to #6293 and #13221. If this is about other circumstances or a regression, please elaborate more.

taefi avatar Aug 02 '22 10:08 taefi

@taefi Hi Soroosh, Thank you for your comment.

What I think about sessions and UI's is that UI's could attach to a session and can be destroyed before the Vaadin session expires, so they could get detached sooner by an event like closing the browser tab.

Currently I am attaching a RSocket connection to my main UI, so I want it to get destroyed when user closes the browser tab as soon as possible (maybe 3x or 4x times the heartbeat interval).

The workaround for this is to set the session timeout to a small value like 30 seconds (for my usecase) to force the session and its UI's to detach and destroy.

bardyamomeni avatar Aug 02 '22 10:08 bardyamomeni

Another workaround (before the functionality is added officially to the platform) can be something like: https://cookbook.vaadin.com/notice-closed to take control of what should happen when the user closes a browser tab. Note that the unload event on the browser gets fired as well when the user refreshes the page.

taefi avatar Aug 02 '22 10:08 taefi

Thanks Soroosh, I will take a look at the link. 🤟🏼

bardyamomeni avatar Aug 02 '22 10:08 bardyamomeni

@bardyamomeni this has been fixed in Vaadin 24.1.0.alpha4 by implementing https://github.com/vaadin/flow/issues/6293. Now when you close a tab in browser or refresh a page, onDetach listener is called immediately. I close this ticket.

mshabarov avatar May 04 '23 07:05 mshabarov