-
Notifications
You must be signed in to change notification settings - Fork 53
Fix ThreadLocal memory leak in ParallelScatterZipCreator causing OOM #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
6
commits into
master
Choose a base branch
from
copilot/fix-oom-problems-plexus-archiver
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0c4a50
Initial plan
Copilot ea41cd5
Fix ThreadLocal memory leak in ParallelScatterZipCreator usage
Copilot bb6e150
Add test to verify ThreadLocal memory leak prevention
Copilot 099dd45
Fix thread naming in Java 21+ virtual thread implementation
Copilot bb9d08f
Use fixed thread pool size to limit ThreadLocal instances
Copilot 32da3be
Bound Java 21 virtual thread pool to prevent unbounded ThreadLocal ac…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ThreadFactory; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
|
|
@@ -32,7 +33,11 @@ public class ConcurrentJarCreatorExecutorServiceFactory { | |
| static ExecutorService createExecutorService(int poolSize) { | ||
| int poolCount = POOL_COUNTER.incrementAndGet(); | ||
| AtomicInteger threadCounter = new AtomicInteger(); | ||
| return Executors.newThreadPerTaskExecutor( | ||
| Thread.ofVirtual().name("plx-arch-" + poolCount + "-" + threadCounter.incrementAndGet()).factory()); | ||
| ThreadFactory threadFactory = r -> { | ||
| return Thread.ofVirtual() | ||
| .name("plx-arch-" + poolCount + "-" + threadCounter.incrementAndGet()) | ||
| .unstarted(r); | ||
| }; | ||
| return Executors.newThreadPerTaskExecutor(threadFactory); | ||
|
||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
src/test/java/org/codehaus/plexus/archiver/zip/ThreadLocalLeakTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package org.codehaus.plexus.archiver.zip; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
|
|
||
| import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; | ||
| import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Test to verify that ThreadLocal memory leaks are prevented when creating | ||
| * multiple jar files sequentially, which was the root cause of the OOM issue | ||
| * reported in https://github.com/codehaus-plexus/plexus-archiver/issues/xxx | ||
| */ | ||
| class ThreadLocalLeakTest { | ||
|
|
||
| @TempDir | ||
| File tempDir; | ||
|
|
||
| /** | ||
| * This test creates multiple jar files to simulate the scenario where | ||
| * ThreadLocal values would accumulate in thread pools. The fix ensures | ||
| * that threads are terminated quickly after completing tasks, which | ||
| * cleans up ThreadLocal values. | ||
| */ | ||
| @Test | ||
| void testMultipleJarCreationsDoNotLeakMemory() throws Exception { | ||
| // Create a source file to add to jars | ||
| File sourceFile = new File(tempDir, "test.txt"); | ||
| Files.write(sourceFile.toPath(), "test content".getBytes()); | ||
|
|
||
| // Create multiple jars sequentially | ||
| for (int i = 0; i < 10; i++) { | ||
| createJar(new File(tempDir, "test-" + i + ".jar"), sourceFile); | ||
| } | ||
|
|
||
| // If we got here without OOM, the test passed | ||
| assertTrue(true, "Multiple jar creations completed successfully without OOM"); | ||
| } | ||
|
|
||
| private void createJar(File outputFile, File sourceFile) throws Exception { | ||
| ConcurrentJarCreator zipCreator = | ||
| new ConcurrentJarCreator(Runtime.getRuntime().availableProcessors()); | ||
|
|
||
| ZipArchiveEntry entry = new ZipArchiveEntry(sourceFile.getName()); | ||
| entry.setMethod(ZipArchiveEntry.DEFLATED); | ||
| entry.setSize(sourceFile.length()); | ||
| entry.setTime(sourceFile.lastModified()); | ||
|
|
||
| zipCreator.addArchiveEntry( | ||
| entry, | ||
| () -> { | ||
| try { | ||
| return Files.newInputStream(sourceFile.toPath()); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }, | ||
| true); | ||
|
|
||
| try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(outputFile)) { | ||
| zos.setEncoding("UTF-8"); | ||
| zipCreator.writeTo(zos); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot reducing the timeout here seems not really useful, a threadpool is all about reusing threads and making them to timeout immediately makes it quite useless.
Instead one should take care that the poolsize is limited if we really get 1500 instances something feels fishy. A corepool size equal to the CPU count seems a good upper bound.
Apart from that one might want to adjust the ParallelScatterZipCreator to allow reclaim some of its used memory.