This is an automated email from the ASF dual-hosted git repository.

chrisdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit c31817d8182b0985def5c5664a8687514a12ea8e
Author: Christofer Dutz <[email protected]>
AuthorDate: Sat Aug 15 14:05:07 2026 +0200

    fix(plc4j/opcua): Close the stream the key store is read from
    
    openKeyStore passed a fresh FileInputStream straight into
    KeyStore.load and never closed it, leaking a file handle for every
    key store and trust store a connection opens. The handle is only
    released when the garbage collector gets around to finalizing the
    stream.
    
    On Linux and macOS this stays invisible - a file can be unlinked
    while descriptors on it are still open - but Windows refuses to
    delete a file that is still held, which is how it surfaced:
    KeyStoreSelectionTest failed on the Windows CI builds because JUnit
    could not clean up its @TempDir. Which test reported it varied from
    run to run, since it depended on whether a GC had happened yet.
    
    Read the store inside a try-with-resources instead.
---
 .../org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java     | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git 
a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java
 
b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java
index 82ad6624ff..9f6ada33fb 100644
--- 
a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java
+++ 
b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/OpcuaDriverContext.java
@@ -43,6 +43,7 @@ import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
 import java.security.KeyPair;
 import java.security.KeyStore;
 import java.security.KeyStoreException;
@@ -349,7 +350,10 @@ public class OpcuaDriverContext {
         }
 
         KeyStore keyStore = KeyStore.getInstance(keyStoreType);
-        keyStore.load(new FileInputStream(serverKeyStore), password);
+        // The stream has to be closed explicitly - on Windows an open handle 
keeps the file locked.
+        try (InputStream inputStream = new FileInputStream(serverKeyStore)) {
+            keyStore.load(inputStream, password);
+        }
         return keyStore;
     }
 

Reply via email to