diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e61703978..0e5892e77 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -105,7 +105,7 @@ commons-lang3 = { module = "org.apache.commons:commons-lang3", version = "3.20.0 commons-logging = { module = "commons-logging:commons-logging", version = "1.3.5" } commons-math3 = { module = "org.apache.commons:commons-math3", version = "3.0" } commons-net = { module = "commons-net:commons-net", version = "3.9.0" } -commons-pool2 = { module = "org.apache.commons:commons-pool2", version = "2.3" } +commons-pool2 = { module = "org.apache.commons:commons-pool2", version = "2.13.1" } commons-text = { module = "org.apache.commons:commons-text", version = "1.15.0" } commons-vfs2 = { module = "org.apache.commons:commons-vfs2", version = "2.10.0" } derby = { module = "org.apache.derby:derby", version.ref = "derby" } diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index e21cee856..ed10943a4 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -1481,11 +1481,6 @@ - - - - - @@ -1579,12 +1574,12 @@ - - - + + + - - + + diff --git a/server/src/main/java/com/mirth/connect/connectors/file/FileConnector.java b/server/src/main/java/com/mirth/connect/connectors/file/FileConnector.java index f1882e20e..5a0cd3fc1 100644 --- a/server/src/main/java/com/mirth/connect/connectors/file/FileConnector.java +++ b/server/src/main/java/com/mirth/connect/connectors/file/FileConnector.java @@ -1,11 +1,6 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Mirth Corporation +// SPDX-FileCopyrightText: 2026 Tony Germano package com.mirth.connect.connectors.file; @@ -13,6 +8,7 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.time.Duration; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -48,6 +44,7 @@ public class FileConnector { private boolean validateConnection; private int maxTotalConnections = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL; private boolean keepConnectionOpen = false; + private boolean closeConnectionsAfterUse = false; private String maxIdleTime; public FileConnector(String channelId, ConnectorProperties connectorProperties, Connector connector) { @@ -69,6 +66,7 @@ public FileConnector(String channelId, ConnectorProperties connectorProperties, this.validateConnection = fileDispatcherProperties.isValidateConnection(); this.keepConnectionOpen = fileDispatcherProperties.isKeepConnectionOpen(); this.maxIdleTime = fileDispatcherProperties.getMaxIdleTime(); + this.closeConnectionsAfterUse = !keepConnectionOpen; } if (connector instanceof DestinationConnector) { @@ -242,11 +240,23 @@ protected void disconnect() { */ protected void destroyConnection(FileSystemConnection connection, FileSystemConnectionOptions fileSystemOptions) throws Exception { if (connection != null) { - ObjectPool pool = getConnectionPool(fileSystemOptions); - pool.invalidateObject(connection); - - synchronized (connections) { - connections.remove(connection); + try { + ObjectPool pool = getConnectionPool(fileSystemOptions); + if (closeConnectionsAfterUse) { + /* + * The pool keeps no idle connections in this mode, so returnObject destroys the + * connection and creates a replacement only for a thread waiting to borrow. + * invalidateObject would create none here, since addObject respects maxIdle, and + * a waiting thread could block forever. + */ + pool.returnObject(connection); + } else { + pool.invalidateObject(connection); + } + } finally { + synchronized (connections) { + connections.remove(connection); + } } } } @@ -267,28 +277,34 @@ private synchronized ObjectPool getConnectionPool(FileSyst ObjectPool pool = pools.get(key); if (pool == null) { - GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(maxTotalConnections); config.setMinIdle(0); // The makes the evictor thread run, if active, no matter how few objects are in the pool - config.setTimeBetweenEvictionRunsMillis(1000); // How often the evictor thread checks for objects to evict + config.setTimeBetweenEvictionRuns(Duration.ofSeconds(1)); // How often the evictor thread checks for objects to evict if (keepConnectionOpen) { int connectionMaxIdleTime = NumberUtils.toInt(maxIdleTime); if (connectionMaxIdleTime == 0) { - // Disable the eviction thread. https://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/BaseGenericObjectPool.html#setTimeBetweenEvictionRunsMillis-long- - config.setTimeBetweenEvictionRunsMillis(0); + // Disable the eviction thread. A non-positive interval means no evictor runs. + config.setTimeBetweenEvictionRuns(Duration.ZERO); } else { - config.setMinEvictableIdleTimeMillis(connectionMaxIdleTime); + config.setMinEvictableIdleDuration(Duration.ofMillis(connectionMaxIdleTime)); } } else { // Always evict objects on the next check by the evictor thread - config.setMinEvictableIdleTimeMillis(1); + config.setMinEvictableIdleDuration(Duration.ofMillis(1)); + } + + if (closeConnectionsAfterUse) { + // Never keep idle connections, so that destroyConnection can return them to be destroyed + config.setMaxIdle(0); } if (isValidateConnection()) { config.setTestOnBorrow(true); - config.setTestOnReturn(true); + // Returned connections are always destroyed when they are not kept open + config.setTestOnReturn(!closeConnectionsAfterUse); } pool = new GenericObjectPool(fileSystemConnectionFactory, config); diff --git a/server/src/test/java/com/mirth/connect/connectors/file/FileConnectorConnectionPoolTest.java b/server/src/test/java/com/mirth/connect/connectors/file/FileConnectorConnectionPoolTest.java new file mode 100644 index 000000000..822196552 --- /dev/null +++ b/server/src/test/java/com/mirth/connect/connectors/file/FileConnectorConnectionPoolTest.java @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Tony Germano + +package com.mirth.connect.connectors.file; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.commons.pool2.PooledObject; +import org.apache.commons.pool2.impl.DefaultPooledObject; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.mirth.connect.connectors.file.filesystems.FileSystemConnection; +import com.mirth.connect.connectors.file.filesystems.FileSystemConnectionFactory; + +/** + * Checks how FileConnector uses its connection pool when a File Writer does not keep connections + * open, so that a commons-pool2 upgrade cannot change it unnoticed. + */ +public class FileConnectorConnectionPoolTest { + + private CountingFactory factory; + private FileConnector fileConnector; + private FileSystemConnectionOptions options; + + @Before + public void setUp() { + options = new FileSystemConnectionOptions(false, "user", "password", null); + factory = new CountingFactory(options); + + FileDispatcherProperties properties = new FileDispatcherProperties(); + properties.setKeepConnectionOpen(false); + + fileConnector = new FileConnector("channelId", properties, null) { + @Override + protected FileSystemConnectionFactory getFileSystemConnectionFactory(FileSystemConnectionOptions fileSystemOptions) { + return factory; + } + }; + } + + @After + public void tearDown() throws Exception { + fileConnector.doStop(); + } + + @Test + public void destroyConnectionDoesNotOpenAnotherConnection() throws Exception { + FileSystemConnection connection = fileConnector.getConnection(options); + fileConnector.destroyConnection(connection, options); + + assertEquals(1, factory.created.get()); + verify(connection).destroy(); + } + + @Test + public void destroyConnectionForgetsConnectionWhenFactoryFails() throws Exception { + FileSystemConnection connection = fileConnector.getConnection(options); + factory.fail.set(true); + + // Creating a connection after destroying one would fail, as when the server is down + try { + fileConnector.destroyConnection(connection, options); + } catch (Exception e) { + // Only the state of the connector matters here + } + + // A forgotten connection is not disconnected again + fileConnector.disconnect(); + verify(connection, never()).disconnect(); + } + + @Test + public void getConnectionReplacesDisconnectedConnection() throws Exception { + factory.disconnectNext.set(true); + + FileSystemConnection connection = fileConnector.getConnection(options); + + assertEquals(2, factory.created.get()); + assertSame(factory.last, connection); + verify(factory.first).destroy(); + + // Only the replacement is tracked as an open connection + fileConnector.disconnect(); + verify(factory.first, never()).disconnect(); + verify(connection).disconnect(); + } + + private static class CountingFactory extends FileSystemConnectionFactory { + final AtomicInteger created = new AtomicInteger(); + final AtomicBoolean fail = new AtomicBoolean(); + final AtomicBoolean disconnectNext = new AtomicBoolean(); + volatile FileSystemConnection first; + volatile FileSystemConnection last; + + CountingFactory(FileSystemConnectionOptions options) { + super(FileScheme.FTP, options, "localhost", 21, false, false, 0); + } + + @Override + public String getPoolKey() { + return "test"; + } + + @Override + public PooledObject makeObject() throws Exception { + if (fail.get()) { + throw new IOException("Connection refused"); + } + FileSystemConnection connection = mock(FileSystemConnection.class); + when(connection.isConnected()).thenReturn(!disconnectNext.getAndSet(false)); + when(connection.isValid()).thenReturn(true); + if (first == null) { + first = connection; + } + last = connection; + created.incrementAndGet(); + return new DefaultPooledObject<>(connection); + } + } +}