Skip to content

Cannot invalidate HTTP session from SessionDestroyListener #6959

Description

@javier-godoy

In #6763 it's suggested that HttpSession can be invalidated from Vaadin SessionDestroyListener, however, when the listener is executed the wrapped session is null.

  • Expected behavior:
    I should be able to invalidate the HttpSession after some period of inactivity.
  • Actual behavior
    The HttpSession is not available after the Vaadin session has been destroyed.

Vaadin 14.0.11

Example application

The SessionDestroyListener is:

	public void serviceInit(ServiceInitEvent event) {
		event.getSource().addSessionDestroyListener(ev->{
			System.out.println("Vaadin session destroyed");
			VaadinSession vsession = ev.getSession();
			WrappedSession wsession = vsession.getSession();
			if (wsession==null) {
				System.out.println("wrapped session is null");
			} else {
				wsession.invalidate();
			}
		});
	}

Activity

  1. denis-anisimov commented on Nov 18, 2019

    @denis-anisimov
    Contributor

    That's unfortunately true, the wrapped session in destroyed listener is null.
    You may use VaadinRequest.getCurrent().getWrappedSession() to access it instead.
    But this bug is valid anyway.

    I would avoid using Spring to reproduce.

    Here is my code which I use to reproduce the issue without Spring:

    @WebServlet(asyncSupported = true, urlPatterns = { "/*" }, initParams = {
            @WebInitParam(name = "closeIdleSessions", value = "true"),
            @WebInitParam(name = "heartbeatInterval", value = "10") })
    public class MyServlet extends VaadinServlet {
    
    }
    
    public class VaadinServiceInitListenerImpl
            implements VaadinServiceInitListener {
    
        @Override
        public void serviceInit(ServiceInitEvent event) {
            event.getSource().addSessionInitListener(
                    ev -> ev.getSession().getSession().setMaxInactiveInterval(30));
            event.getSource().addSessionDestroyListener(ev -> {
                System.out.println("Vaadin session destroyed");
                VaadinSession vsession = ev.getSession();
                WrappedSession wsession = vsession.getSession();
                if (wsession == null) {
                    System.out.println("wrapped session is null");
                } else {
                    wsession.invalidate();
                }
            });
        }
    
    }
    

    Also you need src/main/resources/META-INF/services/com.vaadin.flow.server.VaadinServiceInitListener
    file with the content:
    com.vaadin.starter.skeleton.VaadinServiceInitListenerImpl (I'm using plain skeleton-starter project)

  2. added a commit that references this issue on Jul 2, 2021
    d0f2e58
  3. added a commit that references this issue on Jul 6, 2021
    7e89dfe
  4. added 2 commits that reference this issue on Jul 6, 2021
    1689e50
    c6cf052
  5. added a commit that references this issue on Jul 7, 2021
    842adbd
  6. added a commit that references this issue on Jul 8, 2021
    1a5854e
  7. added a commit that references this issue on Jul 8, 2021
    436ebdd
  8. added a commit that references this issue on Jul 9, 2021
    f2b5883
  9. 8 remaining items

  10. added a commit that references this issue on Jul 20, 2021
    bea5e2b
  11. vaadin-bot commented on Jul 26, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 20.0.5. For prerelease versions, it will be included in its final version.

  12. added a commit that references this issue on Jul 28, 2021
    60f0421
  13. added a commit that references this issue on Jul 29, 2021
    095043a
  14. added 2 commits that reference this issue on Jul 29, 2021
    2054e1d
    1c99d8b
  15. vaadin-bot commented on Aug 6, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 21.0.0.beta1 and is also targeting the upcoming stable 21.0.0 version.

  16. vaadin-bot commented on Aug 9, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 14.7.0.alpha3 and is also targeting the upcoming stable 14.7.0 version.

  17. vaadin-bot commented on Sep 13, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 14.6.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata