Skip to content

Commit 1eea4b7

Browse files
committed
In server.bindAsync, call callback in process.nextTick
1 parent 157882d commit 1eea4b7

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

packages/grpc-js/src/server.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ export class Server {
268268
};
269269
}
270270

271+
const deferredCallback = (error: Error | null, port: number) => {
272+
process.nextTick(() => callback(error, port));
273+
}
274+
271275
const setupServer = (): http2.Http2Server | http2.Http2SecureServer => {
272276
let http2Server: http2.Http2Server | http2.Http2SecureServer;
273277
if (creds._isSecure()) {
@@ -386,7 +390,7 @@ export class Server {
386390
// We only want one resolution result. Discard all future results
387391
resolverListener.onSuccessfulResolution = () => {};
388392
if (addressList.length === 0) {
389-
callback(new Error(`No addresses resolved for port ${port}`), 0);
393+
deferredCallback(new Error(`No addresses resolved for port ${port}`), 0);
390394
return;
391395
}
392396
let bindResultPromise: Promise<BindResult>;
@@ -409,26 +413,26 @@ export class Server {
409413
if (bindResult.count === 0) {
410414
const errorString = `No address added out of total ${addressList.length} resolved`;
411415
logging.log(LogVerbosity.ERROR, errorString);
412-
callback(new Error(errorString), 0);
416+
deferredCallback(new Error(errorString), 0);
413417
} else {
414418
if (bindResult.count < addressList.length) {
415419
logging.log(
416420
LogVerbosity.INFO,
417421
`WARNING Only ${bindResult.count} addresses added out of total ${addressList.length} resolved`
418422
);
419423
}
420-
callback(null, bindResult.port);
424+
deferredCallback(null, bindResult.port);
421425
}
422426
},
423427
(error) => {
424428
const errorString = `No address added out of total ${addressList.length} resolved`;
425429
logging.log(LogVerbosity.ERROR, errorString);
426-
callback(new Error(errorString), 0);
430+
deferredCallback(new Error(errorString), 0);
427431
}
428432
);
429433
},
430434
onError: (error) => {
431-
callback(new Error(error.details), 0);
435+
deferredCallback(new Error(error.details), 0);
432436
},
433437
};
434438

0 commit comments

Comments
 (0)