Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed race condition with addressList #1855

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2705,13 +2705,15 @@ private SocketFactory getSocketFactory() throws IOException {
*/
private InetSocketAddress getInetAddressByIPPreference(String hostName,
int portNumber) throws IOException, SQLServerException {
InetSocketAddress addr = InetSocketAddress.createUnresolved(hostName, portNumber);
for (int i = 0; i < addressList.size(); i++) {
addr = new InetSocketAddress(addressList.get(i), portNumber);
if (!addr.isUnresolved())
return addr;
synchronized (addressList) {
InetSocketAddress addr = InetSocketAddress.createUnresolved(hostName, portNumber);
for (int i = 0; i < addressList.size(); i++) {
addr = new InetSocketAddress(addressList.get(i), portNumber);
if (!addr.isUnresolved())
return addr;
}
return addr;
}
return addr;
}

/**
Expand Down Expand Up @@ -2794,17 +2796,19 @@ private Socket getSocketByIPPreference(String hostName, int portNumber, int time
* Boolean switch for IPv6 first
*/
private void fillAddressList(InetAddress[] addresses, boolean ipv6first) {
addressList.clear();
if (ipv6first) {
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
addressList.add(addr);
synchronized (addressList) {
addressList.clear();
if (ipv6first) {
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
addressList.add(addr);
}
}
}
} else {
for (InetAddress addr : addresses) {
if (addr instanceof Inet4Address) {
addressList.add(addr);
} else {
for (InetAddress addr : addresses) {
if (addr instanceof Inet4Address) {
addressList.add(addr);
}
}
}
}
Expand Down