Skip to content

Commit

Permalink
tweaking locking to prevent concurrent modification
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Hawkins <shawkins@redhat.com>
  • Loading branch information
shawkins authored and manusa committed Jan 30, 2024
1 parent 7a70d8e commit ef9224a
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void onClose(Executor executor) {

BaseClient(final HttpClient httpClient, Config config, ExecutorSupplier executorSupplier,
KubernetesSerialization kubernetesSerialization) {
this.closable = Collections.newSetFromMap(new WeakHashMap<>());
this.closable = Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
this.closed = new CompletableFuture<>();
this.config = config;
this.httpClient = httpClient;
Expand Down Expand Up @@ -149,21 +150,25 @@ protected void setDerivedFields() {
}

@Override
public synchronized void close() {
public void close() {
if (closed.complete(null) && logger.isDebugEnabled()) {
logger.debug(
"The client and associated httpclient {} have been closed, the usage of this or any client using the httpclient will not work after this",
httpClient.getClass().getName());
}
httpClient.close();
closable.forEach(c -> {
List<AutoCloseable> toClose = null;
synchronized (closable) {
toClose = new ArrayList<>(closable);
closable.clear();
}
toClose.forEach(c -> {
try {
c.close();
} catch (Exception e) {
logger.warn("Error closing resource", e);
}
});
closable.clear();
if (this.executorSupplier != null) {
this.executorSupplier.onClose(executor);
this.executorSupplier = null;
Expand Down Expand Up @@ -412,14 +417,16 @@ public KubernetesSerialization getKubernetesSerialization() {
return kubernetesSerialization;
}

public synchronized void addToCloseable(AutoCloseable closeable) {
if (this.closed.isDone()) {
throw new KubernetesClientException("Client is already closed");
public void addToCloseable(AutoCloseable closeable) {
synchronized (closeable) {
if (this.closed.isDone()) {
throw new KubernetesClientException("Client is already closed");
}
this.closable.add(closeable);
}
this.closable.add(closeable);
}

public synchronized void removeFromCloseable(AutoCloseable closeable) {
public void removeFromCloseable(AutoCloseable closeable) {
this.closable.remove(closeable);
}

Expand Down

0 comments on commit ef9224a

Please sign in to comment.