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

Close server port on exit #823

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ngocdaothanh
Copy link

@ngocdaothanh ngocdaothanh commented Jun 23, 2022

When http-server is spawned as a child process, when it exits (uppon SIGINT/SIGTERM), in certain conditions (depending on platforms and the platform conditions, it's hard to reliably reproduce), the port may still be open.

When the parent process tries to run http-server on the same port again, it will fail as the port is still open.

This PR fixes the problem by closing the port on exit.

Relevant issues
Contributor checklist
  • The pull request is being made against the master branch
Maintainer checklist
  • Assign a version triage tag

}).on('SIGINT', function () {
process.emit('SIGINT');
});
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the block for Windows to the bottom, so that the reading order becomes natural.

server.close();
logger.info(chalk.red('http-server stopped.'));
process.exit();
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored from the common code below.

});
}
function stopServer() {
server.close();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main purpose of this PR is to add this server.close(); line.

@ngocdaothanh
Copy link
Author

To maintainers:
Would you please kindly release a new version, after merging this PR?

@ngocdaothanh
Copy link
Author

When http-server is spawned as a child process, when it exits (uppon SIGINT/SIGTERM), in certain conditions (depending on platforms and the platform conditions, it's hard to reliably reproduce), the port may still be open.

I've updated the code to set up "CTRL-C" hook, before printing out "Hit CTRL-C to stop the server".

It's because the parent process relies on "Hit CTRL-C to stop the server" (or "Available on: ...") to know that http-server is ready. But at this time in the old code, the "CTRL-C" hook hasn't been set up yet. If the parent process kills http-server at this time, the port will still be open.

@github-actions
Copy link

This pull request has been inactive for 360 days

@github-actions github-actions bot added the stale label Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant