From 504dc08556e22aff4ea8083aecd422bbde2eab67 Mon Sep 17 00:00:00 2001 From: Denis Joubert Date: Mon, 18 Dec 2017 11:43:43 +0100 Subject: [PATCH 01/14] use file leak detector lib 1.10 --- .gitignore | 1 + pom.xml | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 37cf854..9ae0d7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target work +.idea diff --git a/pom.xml b/pom.xml index b30753f..fbcd442 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ file-leak-detector CloudBees File Leak Detector Plugin Monitors and detects file handle leaks in Jenkins - 1.5-SNAPSHOT + 1.6-SNAPSHOT hpi https://wiki.jenkins-ci.org/display/JENKINS/File+Leak+Detector+Plugin @@ -18,7 +18,7 @@ org.kohsuke file-leak-detector - 1.6 + 1.10 jar-with-dependencies @@ -29,6 +29,10 @@ org.kohsuke asm3 + + org.kohsuke + asm5 + From a48cbca34d5eebc082b97a672181e7fac38a39bb Mon Sep 17 00:00:00 2001 From: Denis Joubert Date: Mon, 18 Dec 2017 14:45:11 +0100 Subject: [PATCH 02/14] update pom & jelly --- file-leak-detector.iml | 202 ++++++++++++++++++ pom.xml | 13 +- .../FileHandleDump/_notRunning.jelly | 3 +- src/main/resources/index.jelly | 1 + 4 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 file-leak-detector.iml diff --git a/file-leak-detector.iml b/file-leak-detector.iml new file mode 100644 index 0000000..113d564 --- /dev/null +++ b/file-leak-detector.iml @@ -0,0 +1,202 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index fbcd442..f7b78af 100644 --- a/pom.xml +++ b/pom.xml @@ -3,17 +3,22 @@ org.jenkins-ci.plugins plugin - 1.466 + 3.2 com.cloudbees.jenkins.plugins file-leak-detector CloudBees File Leak Detector Plugin Monitors and detects file handle leaks in Jenkins - 1.6-SNAPSHOT + 1.6 hpi https://wiki.jenkins-ci.org/display/JENKINS/File+Leak+Detector+Plugin + + 2.7.3 + 8 + + org.kohsuke @@ -25,10 +30,6 @@ args4j args4j - - org.kohsuke - asm3 - org.kohsuke asm5 diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump/_notRunning.jelly b/src/main/resources/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump/_notRunning.jelly index 32e5f17..f082705 100644 --- a/src/main/resources/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump/_notRunning.jelly +++ b/src/main/resources/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump/_notRunning.jelly @@ -1,3 +1,4 @@ + @@ -64,4 +65,4 @@ - \ No newline at end of file + diff --git a/src/main/resources/index.jelly b/src/main/resources/index.jelly index dbd3ea3..a791782 100644 --- a/src/main/resources/index.jelly +++ b/src/main/resources/index.jelly @@ -1,3 +1,4 @@ + From 42525939c6ba107b49b0248f335a94717693e420 Mon Sep 17 00:00:00 2001 From: Denis Joubert Date: Thu, 21 Dec 2017 16:16:52 +0100 Subject: [PATCH 03/14] address comment from Oleg --- .gitignore | 1 + file-leak-detector.iml | 202 ----------------------------------------- pom.xml | 2 +- 3 files changed, 2 insertions(+), 203 deletions(-) delete mode 100644 file-leak-detector.iml diff --git a/.gitignore b/.gitignore index 9ae0d7c..386dcd4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target work .idea +file-leak-detector.iml diff --git a/file-leak-detector.iml b/file-leak-detector.iml deleted file mode 100644 index 113d564..0000000 --- a/file-leak-detector.iml +++ /dev/null @@ -1,202 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index f7b78af..f9426a8 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ file-leak-detector CloudBees File Leak Detector Plugin Monitors and detects file handle leaks in Jenkins - 1.6 + 1.6-SNAPSHOT hpi https://wiki.jenkins-ci.org/display/JENKINS/File+Leak+Detector+Plugin From 03c4147675c88bf63b257f790e99897490d1fea3 Mon Sep 17 00:00:00 2001 From: Denis Joubert Date: Thu, 21 Dec 2017 16:59:26 +0100 Subject: [PATCH 04/14] add a Jenkinsfile --- Jenkinsfile | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..d88a3c1 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,2 @@ +// Build the plugin using https://github.com/jenkins-infra/pipeline-library +buildPlugin() From e3f1f5e82fc74d111f01fe7e1db617d889b6b4c7 Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Thu, 5 Apr 2018 16:31:54 -0400 Subject: [PATCH 05/14] Update to file-leak-detector:1.11 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index f9426a8..5d29bf9 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ org.kohsuke file-leak-detector - 1.10 + 1.11 jar-with-dependencies @@ -32,7 +32,7 @@ org.kohsuke - asm5 + asm6 From 2e71e6daba4430b6958f2b808692eef7b43c95a7 Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Thu, 5 Apr 2018 16:32:24 -0400 Subject: [PATCH 06/14] Update wiki link to wiki.jenkins.io --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 5d29bf9..cedc0df 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ Monitors and detects file handle leaks in Jenkins 1.6-SNAPSHOT hpi - https://wiki.jenkins-ci.org/display/JENKINS/File+Leak+Detector+Plugin + https://wiki.jenkins.io/display/JENKINS/File+Leak+Detector+Plugin 2.7.3 From 8036e816c2fc397e48134163efa1107892254503 Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Thu, 5 Apr 2018 16:40:40 -0400 Subject: [PATCH 07/14] Update to parent pom 3.7 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index cedc0df..3a77f89 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jenkins-ci.plugins plugin - 3.2 + 3.7 com.cloudbees.jenkins.plugins From 698ba37ba4ecad3fca6f4737e5f835a3598da5bc Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Fri, 6 Apr 2018 13:28:33 -0400 Subject: [PATCH 08/14] Add some basic tests --- .../FileHandleDumpTest.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java diff --git a/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java new file mode 100644 index 0000000..66b1ce9 --- /dev/null +++ b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java @@ -0,0 +1,147 @@ +/* + * The MIT License + * + * Copyright 2018 CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.cloudbees.jenkins.plugins.file_leak_detector; + +import com.gargoylesoftware.htmlunit.Page; +import hudson.model.ManagementLink; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.channels.Pipe; +import java.nio.channels.Selector; +import java.util.Arrays; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.JenkinsRule.WebClient; + +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; + +public class FileHandleDumpTest { + + @Rule + public JenkinsRule r = new JenkinsRule(); + + @Rule + public TemporaryFolder f = new TemporaryFolder(); + + @Test + public void detectFileLeak() throws Exception { + activateFileLeakDetector(); + File leakyFile = f.newFile("leaky-file"); + try (InputStream unused = new FileInputStream(leakyFile)) { + assertThat("There should be a file opened by this method", getOpenDescriptors(), hasItem(allOf( + containsString(leakyFile.getPath() + " by thread:"), + containsString("FileHandleDumpTest.detectFileLeak(") + ))); + } + assertThat("There should not be a file opened by this method", getOpenDescriptors(), not(hasItem(allOf( + containsString(leakyFile.getPath() + " by thread:"), + containsString("FileHandleDumpTest.detectFileLeak(") + )))); + } + + @Test + public void detectPipeLeak() throws Exception { + activateFileLeakDetector(); + Pipe p = null; + try { + p = Pipe.open(); + assertThat("There should be a pipe sink and source channel opened by this method", getOpenDescriptors(), allOf( + hasItem(allOf( + containsString("Pipe Sink Channel by thread:"), + containsString("FileHandleDumpTest.detectPipeLeak(") + )), hasItem(allOf( + containsString("Pipe Source Channel by thread:"), + containsString("FileHandleDumpTest.detectPipeLeak(") + )))); + } finally { + if (p != null) { + p.sink().close(); + p.source().close(); + } + } + assertThat("There should not be a pipe sink or source channel opened by this method", getOpenDescriptors(), not(anyOf( + hasItem(allOf( + containsString("Pipe Sink Channel by thread:"), + containsString("FileHandleDumpTest.detectPipeLeak(") + )), hasItem(allOf( + containsString("Pipe Source Channel by thread:"), + containsString("FileHandleDumpTest.detectPipeLeak(") + ))))); + } + + @Test + public void detectSelectorLeak() throws Exception { + activateFileLeakDetector(); + try (Selector unused = Selector.open()) { + assertThat("There should be a selector opened by this method", getOpenDescriptors(), hasItem(allOf( + containsString("selector by thread:"), + containsString("FileHandleDumpTest.detectSelectorLeak(") + ))); + } + assertThat("There should not be a selector opened by this method", getOpenDescriptors(), not(hasItem(allOf( + containsString("selector by thread:"), + containsString("FileHandleDumpTest.detectSelectorLeak(") + )))); + } + + /** + * Activates the file-leak-detector library. Only activates once for the + * whole test suite, but is called in every test so they can run in any + * order, and cannot not be called until Jenkins starts. + */ + private void activateFileLeakDetector() throws Exception { + FileHandleDump fileHandleDump = ManagementLink.all().get(FileHandleDump.class); + fileHandleDump.doActivate(""); + } + + /** + * @return a list of open file detectors as detected by the file-leak-detector + * library. Items in the list have the following format: + *
{@code
+     *  by thread: on 
+     *     
+     * }
+ */ + private List getOpenDescriptors() throws Exception { + FileHandleDump fileHandleDump = ManagementLink.all().get(FileHandleDump.class); + WebClient wc = r.createWebClient(); + Page p = wc.goTo(fileHandleDump.getUrlName(), "text/plain"); + String descriptorDump = p.getWebResponse().getContentAsString(); + String[] descriptors = descriptorDump.split("#\\d+ "); + // First element is similar to "6 descriptors are open", so we exclude it. + return Arrays.asList(descriptors).subList(1, descriptors.length); + } + +} From d250a19c191e44919efe7389d9b11ea096ed0fdd Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Fri, 6 Apr 2018 14:22:43 -0400 Subject: [PATCH 09/14] Remove unused import --- .../jenkins/plugins/file_leak_detector/FileHandleDumpTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java index 66b1ce9..c20bbc7 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java @@ -28,7 +28,6 @@ import hudson.model.ManagementLink; import java.io.File; import java.io.FileInputStream; -import java.io.IOException; import java.io.InputStream; import java.nio.channels.Pipe; import java.nio.channels.Selector; From 3cc9fdcb1ff4cff675e65b2874f19075a429e7bf Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Mon, 9 Apr 2018 12:38:30 -0400 Subject: [PATCH 10/14] Revert changes to .gitignore --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 386dcd4..37cf854 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ target work -.idea -file-leak-detector.iml From f92223db2afee59ce47cbf580b7cffcef2f07d4c Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Wed, 11 Apr 2018 11:44:18 -0400 Subject: [PATCH 11/14] Simplify try/finally block --- .../plugins/file_leak_detector/FileHandleDumpTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java index c20bbc7..d2c8d12 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java @@ -73,9 +73,8 @@ public void detectFileLeak() throws Exception { @Test public void detectPipeLeak() throws Exception { activateFileLeakDetector(); - Pipe p = null; + Pipe p = Pipe.open(); try { - p = Pipe.open(); assertThat("There should be a pipe sink and source channel opened by this method", getOpenDescriptors(), allOf( hasItem(allOf( containsString("Pipe Sink Channel by thread:"), @@ -85,10 +84,8 @@ public void detectPipeLeak() throws Exception { containsString("FileHandleDumpTest.detectPipeLeak(") )))); } finally { - if (p != null) { - p.sink().close(); - p.source().close(); - } + p.sink().close(); + p.source().close(); } assertThat("There should not be a pipe sink or source channel opened by this method", getOpenDescriptors(), not(anyOf( hasItem(allOf( From a776c821bad971001fae849bcec8372119376adf Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Wed, 11 Apr 2018 11:51:56 -0400 Subject: [PATCH 12/14] Don't change the version in the pom manually --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3a77f89..a551609 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ file-leak-detector CloudBees File Leak Detector Plugin Monitors and detects file handle leaks in Jenkins - 1.6-SNAPSHOT + 1.5-SNAPSHOT hpi https://wiki.jenkins.io/display/JENKINS/File+Leak+Detector+Plugin From 1604710beceac969dfabf481046f770d68775b6a Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Wed, 11 Apr 2018 13:14:55 -0400 Subject: [PATCH 13/14] Use JNR instead of JNA --- .../jenkins/plugins/file_leak_detector/FileHandleDump.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump.java b/src/main/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump.java index 29b4286..4c320cd 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDump.java @@ -78,7 +78,7 @@ public HttpResponse doActivate(@QueryParameter String opts) throws Exception { args.add(new File(System.getProperty("java.home"),"bin/java")) .add("-jar") .add(Which.jarFile(Main.class)) - .add(PosixAPI.get().getpid()) + .add(PosixAPI.jnr().getpid()) .add(Util.fixEmpty(opts)); Process p = new ProcessBuilder(args.toCommandArray()) From b325d60d6dbc6d8cd470b871ba9e1b0bc41201f3 Mon Sep 17 00:00:00 2001 From: Devin Nusbaum Date: Thu, 12 Apr 2018 17:21:51 -0400 Subject: [PATCH 14/14] Ignore pipe test on Windows until the library is updated --- .../plugins/file_leak_detector/FileHandleDumpTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java index d2c8d12..b3107eb 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/file_leak_detector/FileHandleDumpTest.java @@ -25,6 +25,7 @@ package com.cloudbees.jenkins.plugins.file_leak_detector; import com.gargoylesoftware.htmlunit.Page; +import hudson.Functions; import hudson.model.ManagementLink; import java.io.File; import java.io.FileInputStream; @@ -33,6 +34,7 @@ import java.nio.channels.Selector; import java.util.Arrays; import java.util.List; +import org.junit.Assume; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -72,6 +74,8 @@ public void detectFileLeak() throws Exception { @Test public void detectPipeLeak() throws Exception { + // TODO: https://github.com/kohsuke/file-leak-detector/issues/36 + Assume.assumeFalse("Pipes are not detected correctly on Windows", Functions.isWindows()); activateFileLeakDetector(); Pipe p = Pipe.open(); try {