You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tw...@apache.org on 2022/10/30 20:58:13 UTC
[mina-sshd] 01/02: Use ThreadLocal.remove() instead of ThreadLocal.set(null)
This is an automated email from the ASF dual-hosted git repository.
twolf pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
commit 4037290a0849abc65dececd66ac1a6bdc5ba1cd9
Author: Thomas Wolf <tw...@apache.org>
AuthorDate: Sun Oct 30 00:07:05 2022 +0200
Use ThreadLocal.remove() instead of ThreadLocal.set(null)
ThreadLocal.set(null) keeps a reference to the ThreadLocal instance in
the thread, which can lead to (small) memory leaks when threads are
re-used, for instance via thread pools.
---
.../sshd/common/util/threads/ThreadUtils.java | 26 +++++++++++++---------
.../apache/sshd/sftp/client/fs/SftpFileSystem.java | 2 +-
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java b/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java
index d06da9f69..b93e81ed5 100644
--- a/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java
+++ b/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java
@@ -60,12 +60,15 @@ public final class ThreadUtils {
* @see {@link #isInternal()}
*/
public static <V> V runAsInternal(Callable<V> code) throws Exception {
- Boolean initial = IS_INTERNAL_THREAD.get();
- try {
- IS_INTERNAL_THREAD.set(Boolean.TRUE);
+ if (isInternalThread()) {
return code.call();
- } finally {
- IS_INTERNAL_THREAD.set(initial);
+ } else {
+ try {
+ IS_INTERNAL_THREAD.set(Boolean.TRUE);
+ return code.call();
+ } finally {
+ IS_INTERNAL_THREAD.remove();
+ }
}
}
@@ -82,12 +85,15 @@ public final class ThreadUtils {
* @see {@link #isInternal()}
*/
public static <T, V> V runAsInternal(T param, IOFunction<? super T, V> code) throws IOException {
- Boolean initial = IS_INTERNAL_THREAD.get();
- try {
- IS_INTERNAL_THREAD.set(Boolean.TRUE);
+ if (isInternalThread()) {
return code.apply(param);
- } finally {
- IS_INTERNAL_THREAD.set(initial);
+ } else {
+ try {
+ IS_INTERNAL_THREAD.set(Boolean.TRUE);
+ return code.apply(param);
+ } finally {
+ IS_INTERNAL_THREAD.remove();
+ }
}
}
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystem.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystem.java
index 47f999758..ae8049285 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystem.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/fs/SftpFileSystem.java
@@ -298,7 +298,7 @@ public class SftpFileSystem
if (!pool.offer(delegate)) {
delegate.close();
}
- wrappers.set(null);
+ wrappers.remove();
}
}