8241988: DatagramSocket incorrectly caches the first set of socket options

Reviewed-by: msheppar, dfuchs, alanb
This commit is contained in:
Chris Hegarty 2020-04-03 07:16:35 +01:00
parent a76f0f78ad
commit 61940fe4ce
3 changed files with 242 additions and 27 deletions

View file

@ -1483,8 +1483,8 @@ public class DatagramSocket implements java.io.Closeable {
return getImpl().getOption(name);
}
private static Set<SocketOption<?>> options;
private static boolean optionsSet = false;
private volatile Set<SocketOption<?>> options;
private final Object optionsLock = new Object();
/**
* Returns a set of the socket options supported by this socket.
@ -1498,18 +1498,22 @@ public class DatagramSocket implements java.io.Closeable {
* @since 9
*/
public Set<SocketOption<?>> supportedOptions() {
synchronized(DatagramSocket.class) {
if (optionsSet) {
Set<SocketOption<?>> options = this.options;
if (options != null)
return options;
synchronized (optionsLock) {
options = this.options;
if (options != null)
return options;
}
try {
DatagramSocketImpl impl = getImpl();
options = Collections.unmodifiableSet(impl.supportedOptions());
} catch (IOException e) {
options = Collections.emptySet();
}
optionsSet = true;
return options;
return this.options = options;
}
}
}

View file

@ -808,24 +808,4 @@ public class MulticastSocket extends DatagramSocket {
} // synch p
} //synch ttl
} //method
private static Set<SocketOption<?>> options;
private static boolean optionsSet = false;
@Override
public Set<SocketOption<?>> supportedOptions() {
synchronized (MulticastSocket.class) {
if (optionsSet) {
return options;
}
try {
DatagramSocketImpl impl = getImpl();
options = Collections.unmodifiableSet(impl.supportedOptions());
} catch (SocketException ex) {
options = Collections.emptySet();
}
optionsSet = true;
return options;
}
}
}