Skip to content

Commit

Permalink
fix factory leak (threads and files)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahgittin committed Aug 31, 2021
1 parent ce68404 commit b5b988b
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions client/src/main/java/io/cloudsoft/winrm4j/client/WinRmClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class WinRmClient implements AutoCloseable {
private final Locale locale;
private final Map<String, String> environment;
private final PayloadEncryptionMode payloadEncryptionMode;
private final WinRm service;
private AsyncHttpEncryptionAwareConduitFactory factoryToCleanup;

// Can be changed throughout object's lifetime, but deprecated
private String operationTimeout;
Expand Down Expand Up @@ -199,6 +201,8 @@ public Builder(String endpoint, String authenticationScheme) {
}

WinRmClient(WinRmClientBuilder builder) {
boolean cleanupFactory = builder.endpointConduitFactory == null;

this.workingDirectory = builder.workingDirectory;
this.locale = builder.locale;
this.operationTimeout = toDuration(builder.operationTimeout);
Expand All @@ -213,12 +217,16 @@ public Builder(String endpoint, String authenticationScheme) {
this.cleanupContext = true;
}

WinRm service = getService(builder);
service = getService(builder);
retryingHandler = new RetryingProxyHandler(service, builder.failureRetryPolicy);
this.winrm = (WinRm) Proxy.newProxyInstance(WinRm.class.getClassLoader(),
new Class[] {WinRm.class, BindingProvider.class},
retryingHandler);
this.payloadEncryptionMode = builder.payloadEncryptionMode();

if (cleanupFactory) {
this.factoryToCleanup = builder.endpointConduitFactory;
}
}

/**
Expand Down Expand Up @@ -577,10 +585,16 @@ public void disconnect() {

@Override
public void close() {
if (context == null) return;
boolean isBusRunning = context.getBus().getState() != BusState.SHUTDOWN;
if (isBusRunning && cleanupContext) {
context.getBus().shutdown(true);
if (factoryToCleanup!=null && !factoryToCleanup.isShutdown()) {
factoryToCleanup.shutdown();
factoryToCleanup = null;
}

if (context!=null && cleanupContext) {
boolean isBusRunning = context.getBus().getState() != BusState.SHUTDOWN;
if (isBusRunning) {
context.getBus().shutdown(true);
}
}
}

Expand Down

0 comments on commit b5b988b

Please sign in to comment.