question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VaadinSession.getSession() is sometimes null in session destroy listeners

See original GitHub issue

Description of the bug / feature

In my usecase I want to keep track of all VaadinSessions. For that reason I am doing the following in the AppShell:

		serviceInitEvent.getSource().addSessionInitListener( ... );
		serviceInitEvent.getSource().addSessionDestroyListener(sessionDestroyEvent -> {
			if(sessionDestroyEvent != null && sessionDestroyEvent.getSession() != null) {
				//do smth with the sessionId
                               //this code is not reached
			}
		});

Expected behavior

I get the correct session when initialized, and I get the session before it is destroyed.

Actual behavior

The destroyed is session null. Problem: I can not track which session was destroyed.

Versions:

- Vaadin / Flow version: 18.0.3
- Java version: 11
- OS version: Windows 10
- Application Server (if applicable): Spring Boot with Tomcat

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
javier-godoycommented, Feb 4, 2021

In https://github.com/vaadin/flow/issues/6959 it was suggested to use VaadinRequest.getCurrent().getWrappedSession() instead of event.getSession().getSession()

1reaction
Legiothcommented, Jan 15, 2021

I couldn’t reproduce the symptoms that you’re describing. I created a new project with everything set as default from https://start.vaadin.com/ and made this addition to the Application class:

@Bean
public VaadinServiceInitListener serviceListener() {
    return new VaadinServiceInitListener() {
        @Override
        public void serviceInit(ServiceInitEvent serviceInitEvent) {
            System.out.println("Service init");
            serviceInitEvent.getSource().addSessionInitListener(sessionInitEvent -> {
                System.out.println("Session init: " + sessionInitEvent.getSession());
            });
            serviceInitEvent.getSource().addSessionDestroyListener(sessionDestroyEvent -> {
                System.out.println("Session destroy: " + sessionDestroyEvent.getSession());
            });
        }
    };
}

When the application is running, I can see log messages like these:

Session destroy: com.vaadin.flow.spring.SpringVaadinSession@3948cea6
Session init: com.vaadin.flow.spring.SpringVaadinSession@4a85427
Session destroy: com.vaadin.flow.spring.SpringVaadinSession@4a85427

I also looked at the framework code that creates those event objects and I couldn’t find any code path that wouldn’t have failed with a NullPointerException before an event without a session would have reached the listener.

Based on that, I have two potential things for you to look up:

  1. Maybe there’s some other problem that means your listener is never called, rather than being called but going the wrong way in the if? You can find out that by adding the same kind of simple logging that I had in my test code.
  2. Maybe there’s some add-on or such that introduces another code path than then one that I could find. Identifying that kind of case should be easy if you could share the stacktrace that leads to invoking the listener. You can find the stacktrace by adding this line to the beginning of the listener: new Exception().printStackTrace();.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vaadin SessionDestroyListener session access - Stack Overflow
While the session object is available by calling sessionDestroyEvent.getSession() , its state is CLOSING and all attributes have already ...
Read more >
com.vaadin.server.VaadinSession.getSession java ... - Tabnine
Returns whether the given session is active or whether it can be closed. * <p> * A session is active if and only...
Read more >
VaadinSession (vaadin-all 8.16.1 API)
Retrieves all VaadinSession s which are stored in the given HTTP session. ... returned by addBootstrapListener(BootstrapListener) to remove a listener.
Read more >
com.vaadin.server Class VaadinSession - javadoc.io
Locks this session and runs the provided Runnable right away. ... Remove a bootstrap listener that was previously added.
Read more >
Vaadin flow: .../vaadin/flow/server/VaadinService.java | Fossies
572 * 573 * @param listener 574 * the vaadin service session destroy listener 575 ... getSession() != null) { 1308 /* 1309...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found