Simple-Web-Server
Simple-Web-Server copied to clipboard
Port bind release if server process is manually killed
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)?
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 ;)
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
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])
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.
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?
@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
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.
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.
That's right.