How to shutdown socket server in ghci?

I wrote a web server, say a webserver package, and can run it in ghci with:

:main localhost 8000 

If I Ctrl-C and run this again, I get

 *** Exception: bind: resource busy (Address already in use) 

So the socket seems to be connected to my ghci session.

How can I release the binding of this port so that I can :reload and start it again without leaving ghci?

+4
source share
2 answers

This usually happens if the core server implementation does not set the REUSE_ADDR parameter on the socket.

Usually, if you stop the server suddenly, the operating system keeps this old server server in 2MSL state for several minutes to prevent new servers from appearing on this port from accidentally receiving old messages destined for the previous connection. If you set REUSE_ADDR when trying to bind a port, you indicate that you want to reuse it before the 2MSL period expires.

The only way to fix this is to change the underlying web server code that you use to set the REUSE_ADDR parameter before binding the listening socket.

+3
source

Are you on Linux? If so, I think you need to use lsof to find out which process is using the given port, and then just kill the PID. I already had a problem with Flask in Python, so I think the same thing here. You leave ghci open, kill the process, and then restart ghci. This is not an elegant method, but if it works, that’s good!

0
source

All Articles