Simple-Web-Server icon indicating copy to clipboard operation
Simple-Web-Server copied to clipboard

Port bind release if server process is manually killed

Open EChu16 opened this issue 7 years ago • 9 comments

The server provided works great and when I first start up the process, it works great. However, when I kill the process (Ctrl + z) and try to start the server again, I run into:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  bind: Address already in use

I looked into the server_http.hpp file and i see that the property reuse_address is set to true which to my understanding, should allow the same endpoint address to be reused again. I am using port 8080 and have no other processes that bind to that port. Is the server built with the intention of not allowing the same address being used or is it missing something to have a clean, graceful shutdown (releasing port/address)?

EChu16 avatar Mar 27 '17 14:03 EChu16

Interrupting a server while the server was managing a request (having a connected client) force the port to be in FIN_WAIT_2 state for the duration of the TCP timeout so the system can close the connection sanely.

Beside, [ctrl]-[z] doesnt kill the server but only put the process in "suspended" state (T). It still have the port open (even if it cant reply to a connection). Use [ctrl]-[c] instead ;)

sebt3 avatar Mar 27 '17 14:03 sebt3

Thanks for the fast reply! I wasn't aware of [ctrl]-[c] as an alternative to killing the server. I guess my new question now is: Where would be the best place to place graceful server shut down code? Just in case I accidentally shut down the server using [ctrl]-[z] :P

EChu16 avatar Mar 27 '17 14:03 EChu16

if you've used [ctrl]-[z], there's 2 options :

  1. type "fg" or "bg" to resume the process (in foreground or background)
  2. "kill -9 %1" to actually kill the process (would do as if you pressed [ctrl]-[c])

sebt3 avatar Mar 27 '17 14:03 sebt3

Actually, kill -9 %1 (or kill -KILL %1) kills the process without the ability to do any cleanup, and is NOT the same as Ctrl-C. To do that, use kill -INTR %1 (or kill -2 %1) instead.

You can tell that I prefer mnemonic names instead of signal numbers. :-)

On Mar 27, 2017, at 9:34 AM, Sébastien Huss [email protected] wrote:

if you've used [ctrl]-[z], there's 2 options :

type "fg" or "bg" to resume the process (in foreground or background) "kill -9 %1" to actually kill the process (would do as if you pressed [ctrl]-[c]) — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eidheim/Simple-Web-Server/issues/118#issuecomment-289472425, or mute the thread https://github.com/notifications/unsubscribe-auth/AAl2II86Yqc7rn-3ltuzyQfFR0ahLAV3ks5rp8jqgaJpZM4MqT6f.

314159 avatar Mar 27 '17 14:03 314159

Is there any reason why I would still be getting the "bind: Address already in use" error if I use the Ctrl-C? A team member of my project has found that it can still happen, although less frequently.

Do I need to put in code to catch sigint events to then safely close out the listener?

EChu16 avatar Mar 27 '17 14:03 EChu16

@314159 thanks for for the correction ;) @EChu16 I never had the issue myself (and I'm using this for over 6months). It shouldnt happen imho

sebt3 avatar Mar 27 '17 15:03 sebt3

CTRL-c is not an "alternative" to CTRL-z. Ctrl-z suspends the process, CTRL-c sends the SIGINT signal which will/should stop the process (except if the process has installed a signal handler).

As to why the server port is still bound, even if the process has been stopped correctly, well, it hasn't.

If you do not have installed a signal-handler to handle SIGKILL or SIGINT in your application, a CTRL-c it will simply shutdown the process, not leaving a chance to properly close the bound socket. The (linux)-kernel cleans the socket-port only some time later.

This has nothing to do with this library, all TCP-sockets on Linux behave like this.

pboettch avatar Mar 27 '17 16:03 pboettch

On Mar 27, 2017, at 11:06 AM, Patrick Boettcher [email protected] wrote:

...If you do not have installed a signal-handler to handle SIGKILL

You can’t install a signal handler to handle SIGKILL.

314159 avatar Mar 27 '17 16:03 314159

That's right.

pboettch avatar Mar 27 '17 16:03 pboettch