From a616acac9e93c0633a77f3f83328e95c0a88ba1b Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 12 Jun 2024 13:21:56 -0500 Subject: [PATCH] Issue #11909 - duplicate --modules= can trigger ConcurrentModificationException + Reworked tracking of enabled modules to not trigger change in "sources" Set if the module is already enabled. --- .../org/eclipse/jetty/start/StartArgs.java | 8 ++++--- .../org/eclipse/jetty/start/MainTest.java | 24 +++++++++++++++++++ .../src/test/resources/dist-home/start.ini | 1 - .../resources/jetty-logging.properties | 0 .../overdeclared-modules/start.d/config.ini | 1 + 5 files changed, 30 insertions(+), 4 deletions(-) delete mode 100644 jetty-core/jetty-start/src/test/resources/dist-home/start.ini create mode 100644 jetty-core/jetty-start/src/test/resources/overdeclared-modules/resources/jetty-logging.properties create mode 100644 jetty-core/jetty-start/src/test/resources/overdeclared-modules/start.d/config.ini diff --git a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java index 083c87cb5f39..d9f54b049842 100644 --- a/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java +++ b/jetty-core/jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java @@ -1426,9 +1426,11 @@ private void selectModules(String source, List moduleNames) { for (String moduleName : moduleNames) { - modules.add(moduleName); - Set set = sources.computeIfAbsent(moduleName, k -> new HashSet<>()); - set.add(source); + if (modules.add(moduleName)) + { + Set set = sources.computeIfAbsent(moduleName, k -> new HashSet<>()); + set.add(source); + } } } diff --git a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java index 98051e391eb8..2b8d29468773 100644 --- a/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java +++ b/jetty-core/jetty-start/src/test/java/org/eclipse/jetty/start/MainTest.java @@ -30,6 +30,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -183,4 +184,27 @@ public void testJvmArgExpansion() throws Exception ); assertThat(commandLine, containsString(expectedExpansion)); } + + @Test + public void testModulesDeclaredTwice() throws Exception + { + List cmdLineArgs = new ArrayList<>(); + + Path homePath = MavenPaths.findTestResourceDir("dist-home"); + Path basePath = MavenPaths.findTestResourceDir("overdeclared-modules"); + cmdLineArgs.add("jetty.home=" + homePath); + cmdLineArgs.add("user.dir=" + basePath); + + Main main = new Main(); + + cmdLineArgs.add("--module=main"); + + // The "main" module is enabled in both ... + // 1) overdeclared-modules/start.d/config.ini + // 2) command-line + // This shouldn't result in an error + StartArgs args = main.processCommandLine(cmdLineArgs.toArray(new String[0])); + + assertThat(args.getSelectedModules(), hasItem("main")); + } } diff --git a/jetty-core/jetty-start/src/test/resources/dist-home/start.ini b/jetty-core/jetty-start/src/test/resources/dist-home/start.ini deleted file mode 100644 index 4cae2dd2f88f..000000000000 --- a/jetty-core/jetty-start/src/test/resources/dist-home/start.ini +++ /dev/null @@ -1 +0,0 @@ ---module=main diff --git a/jetty-core/jetty-start/src/test/resources/overdeclared-modules/resources/jetty-logging.properties b/jetty-core/jetty-start/src/test/resources/overdeclared-modules/resources/jetty-logging.properties new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/jetty-core/jetty-start/src/test/resources/overdeclared-modules/start.d/config.ini b/jetty-core/jetty-start/src/test/resources/overdeclared-modules/start.d/config.ini new file mode 100644 index 000000000000..32c205126083 --- /dev/null +++ b/jetty-core/jetty-start/src/test/resources/overdeclared-modules/start.d/config.ini @@ -0,0 +1 @@ +--modules=main \ No newline at end of file