[JAVA-595] With redeploy web app created a ThreadLocal but failed to remove it when the web application was stopped Created: 03/Jul/12 Updated: 22/Nov/13 Resolved: 10/Mar/13 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | Connection Management |
| Affects Version/s: | 2.8.0 |
| Fix Version/s: | 2.11.0 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Gert Vaartjes | Assignee: | Jeffrey Yemin |
| Resolution: | Done | Votes: | 8 |
| Labels: | mongodb | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
windows/linux (Ubuntu 12.04),sun java 1.7, tomcat 7 |
||
| Attachments: |
|
||||||||||||
| Issue Links: |
|
||||||||||||
| Description |
|
With a redeploy of the webapplication to tomcat: This error message appears: uploaded simple webapp to demonstrate |
| Comments |
| Comment by Tourendal [X] [ 22/Nov/13 ] | ||||||||
|
I am not sure. Thanks for pointing this out. I will check the code again. | ||||||||
| Comment by Jeffrey Yemin [ 22/Nov/13 ] | ||||||||
|
Are you absolutely sure you are closing any and all MongoClient instances? If you don't class MongoClient#close, the threads won't die. | ||||||||
| Comment by Tourendal [X] [ 22/Nov/13 ] | ||||||||
|
I still see this error with mongo-java-driver 2.11.3 on Tomcat 6.0.37: SEVERE: The web application [/xyz] appears to have started a thread named [MongoCleaner1574466115] but has failed to stop it. This is very likely to create a memory leak. Am I missing something? It happens without a redeploy but during normal operations of Tomcat. Is this another bug? | ||||||||
| Comment by auto [ 12/Mar/13 ] | ||||||||
|
Author: {u'date': u'2013-03-12T14:54:49Z', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}Message: | ||||||||
| Comment by auto [ 10/Mar/13 ] | ||||||||
|
Author: {u'date': u'2013-03-10T02:29:02Z', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}Message: | ||||||||
| Comment by Jeffrey Yemin [ 10/Mar/13 ] | ||||||||
|
Fixing this by only keeping a thread local set while a request is active. So if an application either doesn't use DB.requestStart/DB.requestDone, or else always ensures a matching call to DB.requestDone for every call to DB.requestStart (like in a finally block), there should no longer be any dangling thread locals.
| ||||||||
| Comment by Olivier Allouch [ 19/Feb/13 ] | ||||||||
|
Setting the ThreadLocal reference to null is often enough. | ||||||||
| Comment by Ignacio [ 23/Nov/12 ] | ||||||||
|
I think that the problem with the current implementation is the following: by calling Mongo.close() -> then -> DBTCPConnector.close()
It's clear that only the current thread value is removed, but all threads used are not affected. ThreadLocal Javadoc:
so, calling .close is not enough. | ||||||||
| Comment by Jeffrey Yemin [ 03/Jul/12 ] | ||||||||
|
One other thing I should point out. Though this is a memory leak, it's a very small one. So long as you are calling com.mongodb.Mongo#close when the context is destroyed (which I see you are in your test program, at least), all the underlying Socket instances will be closed. | ||||||||
| Comment by Gert Vaartjes [ 03/Jul/12 ] | ||||||||
|
Ok sounds interesting | ||||||||
| Comment by Jeffrey Yemin [ 03/Jul/12 ] | ||||||||
|
Actually, with a bit of internal refactoring we can fix this without an API change. | ||||||||
| Comment by Gert Vaartjes [ 03/Jul/12 ] | ||||||||
|
New configuration for deploying to tomcat with maven Steps to reproduce issue are: 1) have tomcat-manager app installed for deploying apps to Tomcat
| ||||||||
| Comment by Scott Hernandez (Inactive) [ 03/Jul/12 ] | ||||||||
|
Given this zip what are the steps to reproduce? How did you deploy the webapp? How did you redeploy? Are you using maven for this or the web console? | ||||||||
| Comment by Jeffrey Yemin [ 03/Jul/12 ] | ||||||||
|
We could fix this with an API change, something like this in com.mongodb.Mongo:
Applications that cared could call this method at the end of every request. It's not an elegant solution, but I'm not sure we can do much better without a bigger API change. The methods com.mongodb.DB#requestStart and com.mongodb.DB#requestDone rely on this ThreadLocal, and there is no other way besides a ThreadLocal to implement the semantics of those operations. |