http-server icon indicating copy to clipboard operation
http-server copied to clipboard

Close server port on exit

Open ngocdaothanh opened this issue 3 years ago • 3 comments

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
  • https://github.com/http-party/http-server/issues/632
  • https://github.com/http-party/http-server/issues/802
  • https://stackoverflow.com/questions/40270182/node-child-process-doesnt-release-port-even-after-its-killed-by-parent
Contributor checklist
  • [x] The pull request is being made against the master branch
Maintainer checklist
  • [ ] Assign a version triage tag

ngocdaothanh avatar Jun 23 '22 00:06 ngocdaothanh

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

ngocdaothanh avatar Jun 23 '22 00:06 ngocdaothanh

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.

ngocdaothanh avatar Jun 24 '22 01:06 ngocdaothanh

This pull request has been inactive for 360 days

github-actions[bot] avatar Jun 19 '23 12:06 github-actions[bot]