Skip to content

Commit

Permalink
Reject runServer promise if the HTTP server can't start listening
Browse files Browse the repository at this point in the history
Summary:
Changelog:

* **Breaking:** Reject `runServer` promise if the HTTP server can't start listening.

Previously the only way to handle an error at this point (e.g. when the requested port is busy) would have been through an `onError` callback and not through the call stack. Here we keep `onError` working but also reject the `runServer` promise if it is still pending. This is a breaking change for integrators who rely on `runServer` resolving "succesfully" when such an error occurs.

Reviewed By: robhogan

Differential Revision: D35849395

fbshipit-source-id: aecc8326db34a219d2d88506379cb7c00f55dfd4
  • Loading branch information
motiz88 authored and facebook-github-bot committed Apr 25, 2022
1 parent 8f475e5 commit f39af2c
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions packages/metro/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,19 @@ exports.runServer = async (
} else {
httpServer = http.createServer(serverApp);
}

httpServer.on('error', error => {
if (onError) {
onError(error);
}
end();
});

return new Promise(
(
resolve: (result: HttpServer | HttpsServer) => void,
reject: mixed => mixed,
) => {
httpServer.on('error', error => {
if (onError) {
onError(error);
}
reject(error);
end();
});

httpServer.listen(config.server.port, host, () => {
if (onReady) {
onReady(httpServer);
Expand Down Expand Up @@ -331,11 +331,6 @@ exports.runServer = async (
// timeout of 120 seconds to respond to a request.
httpServer.timeout = 0;

httpServer.on('error', error => {
end();
reject(error);
});

httpServer.on('close', () => {
end();
});
Expand Down

0 comments on commit f39af2c

Please sign in to comment.