SelectorTest may fail with ConcurrentModificationException
Synchronization is added w.r.t. sockets ArrayList to avoid ConcurrentModificationException
Author: tedyu
Reviewers: Viktor Somogyi Rajini Sivaram
Closes from tedyu/trunk
@@ -41,6 +41,7 @@ class EchoServer extends Thread {
private final ServerSocket serverSocket;
private final List<Thread> threads;
private final List<Socket> sockets;
+ private volatile boolean closing = false;
private final SslFactory sslFactory;
private final AtomicBoolean renegotiate = new AtomicBoolean();
@@ -71,8 +72,12 @@ class EchoServer extends Thread {
@Override
public void run() {
try {
- while (true) {
+ while (!closing) {
final Socket socket = serverSocket.accept();
+ synchronized (sockets) {
+ if (closing) {
+ break;
+ }
sockets.add(socket);
Thread thread = new Thread() {
@Override
@@ -106,17 +111,21 @@ class EchoServer extends Thread {
thread.start();
threads.add(thread);
}
+ }
} catch (IOException e) {
// ignore
}
}
public void closeConnections() throws IOException {
+ synchronized (sockets) {
for (Socket socket : sockets)
socket.close();
}
+ }
public void close() throws IOException, InterruptedException {
+ closing = true;
this.serverSocket.close();
closeConnections();
for (Thread t : threads)
