Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[misc] Leaking socket file descriptors when using unix domain sockets…
… when path doesn't exist
  • Loading branch information
rusher committed Sep 2, 2022
1 parent 55e3c2a commit f631c4e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/org/mariadb/jdbc/client/impl/ConnectionHelper.java
Expand Up @@ -127,7 +127,17 @@ public static Socket connectSocket(final Configuration conf, final HostAddress h
conf.pipe() == null && conf.localSocket() == null
? new InetSocketAddress(hostAddress.host, hostAddress.port)
: null;
socket.connect(sockAddr, conf.connectTimeout());
try {
socket.connect(sockAddr, conf.connectTimeout());
} catch (IOException ioe) {
// ensure closing unix socket if wrong file
try {
socket.close();
} catch (IOException e) {
// eat
}
throw ioe;
}
}
return socket;

Expand Down
64 changes: 64 additions & 0 deletions src/test/java/org/mariadb/jdbc/integration/UnixsocketTest.java
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (c) 2012-2014 Monty Program Ab
// Copyright (c) 2015-2021 MariaDB Corporation Ab

package org.mariadb.jdbc.integration;

import static org.junit.jupiter.api.Assertions.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLNonTransientConnectionException;
import java.util.Properties;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;

public class UnixsocketTest extends Common {
private final boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");

@Test
public void testConnectWithUnixSocketWhenDBNotUp() throws IOException {
Assumptions.assumeTrue(!isWindows);
String url = "jdbc:mariadb://localhost:3306";
Properties properties = new Properties();
properties.setProperty("localSocket", "/tmp/not_valid_socket");
properties.setProperty("localSocketAddress", "localhost");

java.sql.Driver driver = new org.mariadb.jdbc.Driver();

Runtime rt = Runtime.getRuntime();
// System.out.println("netstat-apnx | grep " + ProcessHandle.current().pid());
String[] commands = {"/bin/sh", "-c", "netstat -apnx | grep " + ProcessHandle.current().pid()};
Process proc = rt.exec(commands);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
int initialLines = 0;
while (stdInput.readLine() != null) {
initialLines++;
}
proc.destroy();

for (int i = 0; i < 10; i++) {
assertThrows(
SQLNonTransientConnectionException.class,
() -> {
driver.connect(url, properties);
});
}
proc = rt.exec(commands);
stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
int finalLines = 0;
while (stdInput.readLine() != null) {
finalLines++;
}
proc.destroy();
assertEquals(
finalLines,
initialLines,
"Error Leaking socket file descriptors. initial :"
+ initialLines
+ " but ending with "
+ finalLines);
}
}

0 comments on commit f631c4e

Please sign in to comment.