Socket error: address is already in use

I have a CherryPy script that I often run to start the server. Today I had to start and stop it several times in order to fix some errors in the configuration file, and I think the socket did not close completely, because when I tried to start it again, I got this problem:

[23/Mar/2015:14:08:00] ENGINE Listening for SIGHUP. [23/Mar/2015:14:08:00] ENGINE Listening for SIGTERM. [23/Mar/2015:14:08:00] ENGINE Listening for SIGUSR1. [23/Mar/2015:14:08:00] ENGINE Bus STARTING CherryPy Checker: The Application mounted at '' has an empty config. [23/Mar/2015:14:08:00] ENGINE Started monitor thread 'Autoreloader'. [23/Mar/2015:14:08:00] ENGINE Started monitor thread '_TimeoutMonitor'. [23/Mar/2015:14:08:00] ENGINE Error in HTTP server: shutting down Traceback (most recent call last): File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/process/servers.py", line 188, in _start_http_thread self.httpserver.start() File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1848, in start raise socket.error(msg) error: No socket could be created 

I edited CherryPy wsgiserver2.py to see socket details .error and error.strerror was

 98 (98, 'Address already in use') Address already in use 

Meanwhile, my socket is built as:

 af = 2 socktype = 1 proto = 6 canonname = '' sa = ('0.0.0.0', 2112) self.bind(af, socktype, proto) 

(this is not the exact code, but the fact that the values ​​at the start of the error)

I checked netstat and did not see anything listening on port 2112, which can cause a problem and how can I diagnose it?

Thanks!

+6
source share
3 answers

You can try the following

 from socket import * sock=socket() sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # then bind 

From the docs:

The SO_REUSEADDR flag tells the kernel to reuse the local socket in the TIME_WAIT state without waiting for its natural timeout to expire.

Here is the full explanation:

Running the example several times with too little delay between execution can lead to this error:

socket.error: [Errno 98] Address already in use

This is because the previous execution left the socket in TIME_WAIT state and cannot be reused repeatedly.

There is a socket flag to set to prevent this, socket.SO_REUSEADDR:

 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((HOST, PORT)) 
+14
source

You can find the process and kill it by doing:

 ps aux | grep python 

find the process identifier and stop it manually by doing:

 sudo kill -9 PID 

replace the PID with your PID.

I often have to do this when testing with Flask / CherryPy. It would be interesting to see if there is an easier way (for example, to prevent it in the first place).

+12
source

It is much easier to do this:

Check the PID (: 5000 is the host, since I worked on 127.0.0.1β–Ί000):
$ lsof -i :5000
Then kill him:
$ sudo kill -9 PID

+9
source

Source: https://habr.com/ru/post/984147/


All Articles