Skip to content

Commit 9be68e7

Browse files
Flossyclaude
andcommitted
fix: resolve temp file leak and memory visibility issues in MavenNexusClassSource
- Change jarPathCache from Map to ConcurrentHashMap for proper memory visibility (#365) - Register temp file immediately after creation to enable cleanup on cancellation - Add temp file cleanup in CancellationException handler to prevent leaks (#363) Resolves #363, #365 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent fb787d7 commit 9be68e7

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/main/java/org/flossware/classloader/MavenNexusClassSource.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class MavenNexusClassSource implements ClassSource, AutoCloseable {
4848
private final AuthConfig authConfig;
4949
private final Map<String, byte[]> classCache;
5050
private final ConcurrentHashMap<String, FutureTask<JarFile>> jarFileCache;
51-
private final Map<String, Path> jarPathCache;
51+
private final ConcurrentHashMap<String, Path> jarPathCache;
5252
private final int connectTimeout;
5353
private final int readTimeout;
5454
private volatile boolean closed = false;
@@ -204,13 +204,15 @@ private JarFile ensureJarCached(String artifactKey, String jarUrl) throws IOExce
204204
@Override
205205
public JarFile call() throws IOException {
206206
Path tempJarPath = Files.createTempFile("jclassloader-nexus-", ".jar");
207+
// Register temp file immediately so it can be cleaned up on cancellation or close()
208+
jarPathCache.put(artifactKey, tempJarPath);
207209
try {
208210
downloadJarFile(jarUrl, tempJarPath);
209211
JarFile jarFile = new JarFile(tempJarPath.toFile());
210-
jarPathCache.put(artifactKey, tempJarPath);
211212
return jarFile;
212213
} catch (IOException e) {
213214
// Clean up temp file on failure
215+
jarPathCache.remove(artifactKey);
214216
try {
215217
Files.deleteIfExists(tempJarPath);
216218
} catch (IOException ignored) {
@@ -245,6 +247,15 @@ public JarFile call() throws IOException {
245247
throw new IOException("Interrupted while waiting for artifact download: " + artifactKey, e);
246248
} catch (CancellationException e) {
247249
jarFileCache.remove(artifactKey, existingTask);
250+
// Clean up temp file if it was created before cancellation
251+
Path tempPath = jarPathCache.remove(artifactKey);
252+
if (tempPath != null) {
253+
try {
254+
Files.deleteIfExists(tempPath);
255+
} catch (IOException ignored) {
256+
// Ignore cleanup errors
257+
}
258+
}
248259
throw new IOException("Artifact download was cancelled: " + artifactKey, e);
249260
}
250261
}

0 commit comments

Comments
 (0)