Kill thread local leak

This commit modifies InjectorImpl to prevent a thread local leak in
certain web containers. This leak can arise when starting a node client
inside such a web container. The underlying issue is that the
ThreadLocal instance was created via an anonymous class. Such an
anonymous class has an implicit reference back to the InjectorImpl in
which it was created. The solution here is to not use an anonymous class
but instead just create the reference locally and set it on the thread
local.

Relates #17921
This commit is contained in:
Jason Tedor 2016-04-22 08:05:33 -04:00
parent a1c9025eaa
commit 21b1da1bea
1 changed files with 6 additions and 7 deletions

View File

@ -84,12 +84,7 @@ class InjectorImpl implements Injector, Lookups {
if (parent != null) {
localContext = parent.localContext;
} else {
localContext = new ThreadLocal<Object[]>() {
@Override
protected Object[] initialValue() {
return new Object[1];
}
};
localContext = new ThreadLocal<>();
}
}
@ -867,13 +862,17 @@ class InjectorImpl implements Injector, Lookups {
return getProvider(type).get();
}
final ThreadLocal<Object[]> localContext;
private final ThreadLocal<Object[]> localContext;
/**
* Looks up thread local context. Creates (and removes) a new context if necessary.
*/
<T> T callInContext(ContextualCallable<T> callable) throws ErrorsException {
Object[] reference = localContext.get();
if (reference == null) {
reference = new Object[1];
localContext.set(reference);
}
if (reference[0] == null) {
reference[0] = new InternalContext();
try {