100

when i setup application.py, it shows that socket.error: [Errno 98] Address already in use.

Traceback (most recent call last):
File "application.py", line 121, in <module>
main()
File "application.py", line 117, in main
http_server.listen(options.port)
File "/usr/local/lib/python2.7/site-packages/tornado-3.1-py2.7.egg/tornado/tcpserver.py", line 117, in listen
sockets = bind_sockets(port, address=address)
File "/usr/local/lib/python2.7/site-packages/tornado-3.1-py2.7.egg/tornado/netutil.py", line 90, in bind_sockets
sock.bind(sockaddr)
File "/usr/local/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
emuu
  • 1,059
  • 2
  • 8
  • 4
  • You need to provide more information about the application. – Radio- Jul 22 '13 at 05:10
  • 2
    If you know the address it is trying to use, then you can find out what else is holding that address with `netstat`. – Luke Jul 22 '13 at 05:11
  • Change the `address` and provide us more code please. – Victor Castillo Torres Jul 22 '13 at 05:13
  • @emuu: http://stackoverflow.com/questions/4465959/python-errno-98-address-already-in-use – thinker3 Sep 04 '13 at 13:40
  • 40
    ```sudo lsof -t -i tcp:8000 | xargs kill -9``` – wadadaaa Nov 01 '17 at 08:31
  • What is 'application.py' exactly? – WY Hsu Jul 12 '18 at 06:19
  • I'm coding another server app and the same trouble happened. If you want to restart your server immediately and listen on the port you should set the REUSE flag as here `s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)` I executed `netstat -e -an | grep 1089` command and found out that the connection had not closed. It was alive in FIN_WAIT2 state The socket was shutdown, but not closed yet. The socket stayed in FIN_WAIT2 state. You may check it yourself try to start the app, kill the app, and check connections. – Orlov Const Oct 28 '21 at 08:14
  • killing is the best way to get ride of this problem. – Jalil Nourmohammadi Khiarak Dec 06 '22 at 23:36

1 Answers1

163

There is obviously another process listening on the port. You might find out that process by using the following command:

$ lsof -i :8000

or change your tornado app's port. tornado's error info not Explicitly on this.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
the5fire
  • 2,376
  • 3
  • 15
  • 10
  • Me too :D And, it's not possible to use that solution, @NeDark, in every case. – Ignacio Tiraboschi May 20 '16 at 20:05
  • This is working for me to check what program is using that address – panoet Sep 12 '16 at 12:11
  • 2
    This answer didn't work under my case where a socket is created by Python application and th application is forced exit. Please follow the link and see the answer by ayoub laaziz – Yushan ZHANG Mar 27 '17 at 08:52
  • 46
    Worked for me with `sudo` and port 5000 (for a Flask app), e.g. `sudo lsof -i :5000` to detect the process listening on the port, and then `sudo kill -9 ` – Inverbis Aug 08 '17 at 15:30
  • +1 It is in any case helpful to "rule out" this particular cause of the problem, and in general a very helpful thing to know about. – Christian Neverdal Mar 21 '18 at 09:31
  • 5
    Nothing shows up after I execute this command (even `sudo lsof -i :8000`), but still I am getting the same error. :( – ytu Jul 03 '18 at 03:50
  • @ytu Try `sudo lsof -i | grep "py"`. – mazunki Mar 25 '19 at 19:14
  • 1
    I am using sudo didnt show anything op. – ScipioAfricanus Jul 15 '19 at 19:13
  • 9
    For those facing this issue with nothing coming up from `netstat` or `lsof`, if you are testing/restarting a script that makes a call to [`socket.bind()`](https://docs.python.org/3/library/socket.html#socket.socket.bind), you may find that the address may still be in use for a while even after killing the involved process : checkout this [answer](https://stackoverflow.com/a/4466035/2529954). – EricLavault Oct 25 '19 at 20:24
  • Is there an equivalent command for ubuntu? – jstm Nov 03 '20 at 19:51