Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc-js: Handle race between bindAsync and (try|force)Shutdown #2590

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/grpc-js/package.json
@@ -1,6 +1,6 @@
{
"name": "@grpc/grpc-js",
"version": "1.9.4",
"version": "1.9.5",
"description": "gRPC Library for Node - pure JS implementation",
"homepage": "https://grpc.io/",
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",
Expand Down
23 changes: 23 additions & 0 deletions packages/grpc-js/src/server.ts
Expand Up @@ -161,6 +161,7 @@ export class Server {
>();
private sessions = new Map<http2.ServerHttp2Session, ChannelzSessionInfo>();
private started = false;
private shutdown = false;
private options: ChannelOptions;
private serverAddressString = 'null';

Expand Down Expand Up @@ -375,6 +376,10 @@ export class Server {
throw new Error('server is already started');
}

if (this.shutdown) {
throw new Error('bindAsync called after shutdown');
}

if (typeof port !== 'string') {
throw new TypeError('port must be a string');
}
Expand Down Expand Up @@ -485,6 +490,11 @@ export class Server {
http2Server.once('error', onError);

http2Server.listen(addr, () => {
if (this.shutdown) {
http2Server.close();
resolve(new Error('bindAsync failed because server is shutdown'));
return;
}
const boundAddress = http2Server.address()!;
let boundSubchannelAddress: SubchannelAddress;
if (typeof boundAddress === 'string') {
Expand Down Expand Up @@ -583,6 +593,11 @@ export class Server {
http2Server.once('error', onError);

http2Server.listen(address, () => {
if (this.shutdown) {
http2Server.close();
resolve({port: 0, count: 0});
return;
}
const boundAddress = http2Server.address() as AddressInfo;
const boundSubchannelAddress: SubchannelAddress = {
host: boundAddress.address,
Expand Down Expand Up @@ -637,6 +652,12 @@ export class Server {
) => {
// We only want one resolution result. Discard all future results
resolverListener.onSuccessfulResolution = () => {};
if (this.shutdown) {
deferredCallback(
new Error(`bindAsync failed because server is shutdown`),
0
);
}
if (addressList.length === 0) {
deferredCallback(
new Error(`No addresses resolved for port ${port}`),
Expand Down Expand Up @@ -707,6 +728,7 @@ export class Server {
}

this.started = false;
this.shutdown = true;

// Always destroy any available sessions. It's possible that one or more
// tryShutdown() calls are in progress. Don't wait on them to finish.
Expand Down Expand Up @@ -785,6 +807,7 @@ export class Server {

// Close the server if necessary.
this.started = false;
this.shutdown = true;

for (const { server: http2Server, channelzRef: ref } of this
.http2ServerList) {
Expand Down