Skip to content

Commit 5396672

Browse files
committed
Kill thread local leak
This commit backports commit 21b1da1 from master to 1.7. Relates elastic#17921
1 parent 74f6eb1 commit 5396672

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/main/java/org/elasticsearch/common/inject/InjectorImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ class InjectorImpl implements Injector, Lookups {
6262
if (parent != null) {
6363
localContext = parent.localContext;
6464
} else {
65-
localContext = new ThreadLocal<Object[]>() {
66-
protected Object[] initialValue() {
67-
return new Object[1];
68-
}
69-
};
65+
localContext = new ThreadLocal<>();
7066
}
7167
}
7268

@@ -817,13 +813,17 @@ public <T> T getInstance(Class<T> type) {
817813
return getProvider(type).get();
818814
}
819815

820-
final ThreadLocal<Object[]> localContext;
816+
private final ThreadLocal<Object[]> localContext;
821817

822818
/**
823819
* Looks up thread local context. Creates (and removes) a new context if necessary.
824820
*/
825821
<T> T callInContext(ContextualCallable<T> callable) throws ErrorsException {
826822
Object[] reference = localContext.get();
823+
if (reference == null) {
824+
reference = new Object[1];
825+
localContext.set(reference);
826+
}
827827
if (reference[0] == null) {
828828
reference[0] = new InternalContext();
829829
try {

0 commit comments

Comments
 (0)