spring icon indicating copy to clipboard operation
spring copied to clipboard

VaadinSession unavailable during application context shutdown

Open archiecobbs opened this issue 8 years ago • 1 comments

If you have a Spring DestructionAwareBeanPostProcessor in your application context, when a VaadinSession is shutdown it will handle cleanup for beans that it deals with.

This is important to avoid memory leaks, for example, when registering session-scoped beans as listeners on globally-scoped event sources. These listener registrations must be removed when the session is shutdown, otherwise the session beans are never unreferenced.

The problem is that during application context shutdown, the VaadinSession is (apparently) already destroyed, so any session-scoped beans are effectively inaccessible, and so the DestructionAwareBeanPostProcessor can no longer find them to clean them up.

Here's an exception thrown on shutdown showing the problem:

2017-08-10T15:38:29,210 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] DEBUG: Vaadin service has been destroyed, destroying [SessionAwareBeanStore[id=4fc3d268, name=Session:1F9513D17A6E95DAC111D7CEC64165F8]]
2017-08-10T15:38:29,210 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] DEBUG: Destroying [SessionAwareBeanStore[id=4fc3d268, name=Session:1F9513D17A6E95DAC111D7CEC64165F8]]
2017-08-10T15:38:29,217 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ERROR: BeanStore destruction callback failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionEventBus': Scope 'vaadin-session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No VaadinSession bound to current thread
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:355) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        ...

To fix this, the application context needs to be shutdown prior to the VaadinSession being shutdown, or if that's too hard, at least make the VaadinSession available via VaadinSession.getCurrent() so accessing beans in the session scope still works.

archiecobbs avatar Aug 10 '17 20:08 archiecobbs

Your problem sounds like a different thing but might be related to #208.

StefanPenndorf avatar Aug 20 '17 19:08 StefanPenndorf