mirror of https://github.com/grpc/grpc-java.git
netty: prevent interruption during bind from leaking channel
Fixes #6850
This commit is contained in:
parent
0b4503e4b2
commit
2c250ace52
|
|
@ -400,6 +400,36 @@ public abstract class AbstractTransportTest {
|
||||||
server2.start(new MockServerListener());
|
server2.start(new MockServerListener());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void serverStartInterrupted() throws Exception {
|
||||||
|
client = null;
|
||||||
|
|
||||||
|
// Just get free port
|
||||||
|
server.start(serverListener);
|
||||||
|
int port = -1;
|
||||||
|
SocketAddress addr = server.getListenSocketAddress();
|
||||||
|
if (addr instanceof InetSocketAddress) {
|
||||||
|
port = ((InetSocketAddress) addr).getPort();
|
||||||
|
}
|
||||||
|
assumeTrue("transport is not using InetSocketAddress", port != -1);
|
||||||
|
server.shutdown();
|
||||||
|
|
||||||
|
server = Iterables.getOnlyElement(newServer(port, Arrays.asList(serverStreamTracerFactory)));
|
||||||
|
boolean success;
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
try {
|
||||||
|
server.start(serverListener);
|
||||||
|
success = true;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
success = false;
|
||||||
|
} finally {
|
||||||
|
Thread.interrupted(); // clear interruption
|
||||||
|
}
|
||||||
|
assumeTrue("apparently start is not impacted by interruption, so nothing to test", !success);
|
||||||
|
// second time should not throw, as the first time should not have bound to the port
|
||||||
|
server.start(serverListener);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void openStreamPreventsTermination() throws Exception {
|
public void openStreamPreventsTermination() throws Exception {
|
||||||
server.start(serverListener);
|
server.start(serverListener);
|
||||||
|
|
|
||||||
|
|
@ -242,12 +242,10 @@ class NettyServer implements InternalServer, InternalWithLogId {
|
||||||
});
|
});
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
ChannelFuture future = b.bind(address);
|
ChannelFuture future = b.bind(address);
|
||||||
try {
|
// We'd love to observe interruption, but if interrupted we will need to close the channel,
|
||||||
future.await();
|
// which itself would need an await() to guarantee the port is not used when the method returns.
|
||||||
} catch (InterruptedException ex) {
|
// See #6850
|
||||||
Thread.currentThread().interrupt();
|
future.awaitUninterruptibly();
|
||||||
throw new RuntimeException("Interrupted waiting for bind");
|
|
||||||
}
|
|
||||||
if (!future.isSuccess()) {
|
if (!future.isSuccess()) {
|
||||||
throw new IOException("Failed to bind", future.cause());
|
throw new IOException("Failed to bind", future.cause());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue