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
15 changes: 9 additions & 6 deletions xds/src/main/java/io/grpc/xds/XdsServerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public void uncaughtException(Thread t, Throwable e) {
private final CountDownLatch internalTerminationLatch = new CountDownLatch(1);
private final SettableFuture<Exception> initialStartFuture = SettableFuture.create();
private boolean initialStarted;
// Must be accessed in syncContext.
// Guards the forceful-shutdown work in shutdownNow(), independently of the shutdown AtomicBoolean
// above, so it isn't skipped when shutdown()
private boolean shutdownNowed;
private ScheduledHandle restartTimer;
private ObjectPool<XdsClient> xdsClientPool;
private XdsClient xdsClient;
Expand Down Expand Up @@ -408,16 +412,15 @@ public void run() {

@Override
public Server shutdownNow() {
if (!shutdown.compareAndSet(false, true)) {
return this;
}
shutdown();
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!delegate.isShutdown()) {
delegate.shutdownNow();
if (shutdownNowed) {
return;
}
internalShutdown();
shutdownNowed = true;
delegate.shutdownNow();
initialStartFuture.set(new IOException("server is forcefully shut down"));
}
});
Expand Down
56 changes: 56 additions & 0 deletions xds/src/test/java/io/grpc/xds/XdsServerWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,62 @@ public void run() {
}
}

@Test
public void shutdownNow_afterShutdown_stillUnblocksStartThread() throws Exception {
final SettableFuture<Server> start = SettableFuture.create();
Executors.newSingleThreadExecutor()
.execute(
new Runnable() {
@Override
public void run() {
try {
start.set(xdsServerWrapper.start());
} catch (Exception ex) {
start.setException(ex);
}
}
});
assertThat(xdsClient.ldsResource.get(5, TimeUnit.SECONDS))
.isEqualTo("grpc/server?udpa.resource.listening_address=0.0.0.0:1");
xdsServerWrapper.shutdown();
xdsServerWrapper.shutdownNow();
try {
start.get(5, TimeUnit.SECONDS);
fail("should have thrown but not");
} catch (ExecutionException ex) {
assertThat(ex).hasCauseThat().isInstanceOf(IOException.class);
assertThat(ex).hasCauseThat().hasMessageThat().isEqualTo("server is forcefully shut down");
}
}

@Test
public void shutdownNow_calledTwice_forcefullyShutsDownDelegateOnce() throws Exception {
final SettableFuture<Server> start = SettableFuture.create();
Executors.newSingleThreadExecutor()
.execute(
new Runnable() {
@Override
public void run() {
try {
start.set(xdsServerWrapper.start());
} catch (Exception ex) {
start.setException(ex);
}
}
});
assertThat(xdsClient.ldsResource.get(5, TimeUnit.SECONDS))
.isEqualTo("grpc/server?udpa.resource.listening_address=0.0.0.0:1");
xdsServerWrapper.shutdownNow();
xdsServerWrapper.shutdownNow();
try {
start.get(5, TimeUnit.SECONDS);
fail("should have thrown but not");
} catch (ExecutionException ex) {
assertThat(ex).hasCauseThat().isInstanceOf(IOException.class);
}
verify(mockServer, times(1)).shutdownNow();
}

@Test
public void initialStartIoException() throws Exception {
final SettableFuture<Server> start = SettableFuture.create();
Expand Down
Loading