4
votes

I wrote a webserver with, say, the webserver package, and can start it in ghci with:

:main localhost 8000

If I Ctrl-C it and run that again, I get

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

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

How can I free that port binding so that I can :reload and start it again without quitting ghci?

2

2 Answers

3
votes

This usually happens if the underlying server implementation does not set the REUSE_ADDR option on the socket.

Usually if you terminate a server abruptly, the operating system keeps that server's old port in the 2MSL state for a couple of minutes in order to prevent new servers on that port from accidentally receiving old messages intended for the previous connection. If you set REUSE_ADDR when attempting to bind the port, you specify that you wish to forcefully reuse it before the 2MSL period is over.

The only way you can fix this is to modify the code for the underlying web server that you are using to set the REUSE_ADDR option before binding the listening socket.

0
votes

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