From 48c27e3fea758fe3907890364eb7a0b59c12ad48 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Mon, 22 Jun 2020 22:54:35 +0100 Subject: [PATCH] Fix for memory leak In SmbSession Fixes https://github.com/spring-projects/spring-integration-extensions/issues/225 If SmbShare instantiates a BaseContext during construction then the context will be closed on calling close(). Without this there's a ThreadLocal memory leak --- .../integration/smb/session/SmbSession.java | 2 +- .../integration/smb/session/SmbShare.java | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java index fbd15d0..0428d9c 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java @@ -406,7 +406,7 @@ public class SmbSession implements Session { @Override public void close() { - this.smbShare.doClose(); + this.smbShare.close(); } /** diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java index 79d4e27..d323e74 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbShare.java @@ -28,6 +28,7 @@ import org.springframework.util.Assert; import org.springframework.util.StringUtils; import jcifs.CIFSContext; +import jcifs.CIFSException; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.context.SingletonContext; @@ -49,6 +50,8 @@ public class SmbShare extends SmbFile { private final AtomicBoolean useTempFile = new AtomicBoolean(false); + private final AtomicBoolean closeContext = new AtomicBoolean(false); + /** * @deprecated as of release 1.1.0, use {@link #SmbShare(SmbConfig)} instead. * @param url do not use @@ -98,6 +101,8 @@ public class SmbShare extends SmbFile { new PropertyConfiguration(_props)).withCredentials( new NtlmPasswordAuthenticator( _smbConfig.getDomain(), _smbConfig.getUsername(), _smbConfig.getPassword()))); + + this.closeContext.set(true); } public void init() throws NestedIOException { @@ -142,11 +147,25 @@ public class SmbShare extends SmbFile { } /** - * Set the open state to closed. - * Note: jcifs.smb.SmbFile defines a package-protected method close(). + * @deprecated use {@link #close()} instead. */ + @Deprecated void doClose() { + close(); + } + + @Override + public synchronized void close() { this.open.set(false); + if (this.closeContext.get()) { + try { + getContext().close(); + } + catch (CIFSException e) { + logger.error("Unable to close share: " + this); + } + } + super.close(); } public String newTempFileSuffix() {