Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undeploy shows SecurityContextImpl [Null authentication] memory leak on tomcat #9841

Closed
shirosaki opened this issue Jun 1, 2021 · 3 comments
Assignees
Labels
in: web An issue in web modules (web, webmvc) type: bug A general bug

Comments

@shirosaki
Copy link
Contributor

shirosaki commented Jun 1, 2021

Describe the bug
Create a spring boot war application and deploy to tomcat server.
Undeploy the app from tomcat causes the following log.

SEVERE [Catalina-utility-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [xxxxxx] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@b58535e3]) and a value of type [org.springframework.security.core.context.SecurityContextImpl] (value [SecurityContextImpl [Null authentication]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

To Reproduce
Put war file to webapps directory in tomcat and remove the war from webapps.
Spring boot v2.4.1, Java 11 and tomcat 9.0.41.

Expected behavior
SecurityContextImpl [Null authentication] is not created with ThreadLocal or removed on stopping.

If SecurityContextHolder strategy is MODE_GLOBAL, this doesn't occur.
I don't find how to remove SecurityContextImpl [Null authentication].

@shirosaki shirosaki added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Jun 1, 2021
@jzheaux
Copy link
Contributor

jzheaux commented Jun 1, 2021

Hi, @shirosaki, thanks for the report. I've attempted to reproduce this behavior using the Hello Security sample and the versions described; however, I'm unsuccessful. Would you be able to supply a minimal sample that reproduces the issue?

@jzheaux jzheaux self-assigned this Jun 1, 2021
@jzheaux jzheaux added in: web An issue in web modules (web, webmvc) and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 1, 2021
@shirosaki
Copy link
Contributor Author

@jzheaux
Thanks. I've created a sample.
https://github.com/shirosaki/spring-security-samples/tree/threadlocalleak/servlet/spring-boot/java/oauth2/webclient

Create war.

./gradlew war

Put the war file to tomcat webapps. Then the war is deployed.

cp build/libs/webclient-5.6.0-SNAPSHOT-plain.war /path/to/apache-tomcat/webapps/

And remove the war file to undeploy.

rm /path/to/apache-tomcat/webapps/webclient-5.6.0-SNAPSHOT-plain.war

@shirosaki shirosaki changed the title Undeploy shows SecurityContextImpl [Null authentication]] memory leak on tomcat Undeploy shows SecurityContextImpl [Null authentication] memory leak on tomcat Jun 2, 2021
@shirosaki
Copy link
Contributor Author

shirosaki commented Jun 3, 2021

This change seems to fix leaks.
shirosaki@6065479

SecurityContextHolder.getContext() creates null authentication object.

jzheaux added a commit that referenced this issue Nov 30, 2021
jzheaux pushed a commit that referenced this issue Nov 30, 2021
To get current context without creating a new context.
Creating a new context may cause ThreadLocal leak.

Closes gh-9841
jzheaux added a commit that referenced this issue Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web An issue in web modules (web, webmvc) type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants