Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ public void handleEvent(UdpEvent udpEvent) {
}
}

/**
* Stop the keep-alive scheduler without waiting so the UDP channel can be closed
* before a subsequent write() hits a closed channel.
*/
public void stopScheduler() {
executorService.shutdown();
}

public void stop() {
ExecutorServiceManager.shutdownAndAwaitTermination(executorService, esName);
if (dnsExecutorService != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class BackupServer implements AutoCloseable {

private BackupManager backupManager;

private Channel channel;
// volatile: Dekker pairing with shutdown so close() observes bind and start() observes close.
private volatile Channel channel;

private volatile boolean shutdown = false;

Expand All @@ -52,6 +53,11 @@ public void initServer() {
}
}

public boolean isBound() {
Channel ch = channel;
return ch != null && ch.isActive();
}

private void start() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
try {
Expand All @@ -77,6 +83,11 @@ public void initChannel(NioDatagramChannel ch)

logger.info("Backup server started, bind port {}", port);

// close() may have raced bind (Dekker). Close now so closeFuture.sync() cannot hang.
if (shutdown) {
channel.close();
}

channel.closeFuture().sync();
if (shutdown) {
logger.info("Shutdown backup BackupServer");
Expand All @@ -95,14 +106,16 @@ public void initChannel(NioDatagramChannel ch)
public void close() {
logger.info("Closing backup server...");
shutdown = true;
backupManager.stop();
// Stop keep-alive first (no wait) so it cannot write() after the channel is closed.
backupManager.stopScheduler();
if (channel != null) {
try {
channel.close().await(10, TimeUnit.SECONDS);
} catch (Exception e) {
logger.warn("Closing backup server failed.", e);
}
}
backupManager.stop();
ExecutorServiceManager.shutdownAndAwaitTermination(executor, name);
logger.info("Backup server closed.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -46,7 +47,10 @@ public void tearDown() {
@Test(timeout = 60_000)
public void test() throws InterruptedException {
backupServer.initServer();
// wait for the server to start so channel is assigned before close() is called
Thread.sleep(1000);
long deadline = System.currentTimeMillis() + 15_000L;
while (!backupServer.isBound() && System.currentTimeMillis() < deadline) {
Thread.sleep(50);
}
Assert.assertTrue("BackupServer UDP channel did not bind", backupServer.isBound());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ public static int chooseRandomPort(int min, int max) {
}

private static boolean checkPortAvailable(int port) throws IOException {
try (java.net.ServerSocket ss = new java.net.ServerSocket(port)) {
ss.setReuseAddress(true);
try (java.net.ServerSocket ss = new java.net.ServerSocket(port);
java.net.DatagramSocket ds = new java.net.DatagramSocket(port)) {
return true;
} catch (IOException e) {
return false;
Expand Down
Loading