Python [Errno 98] Address already in use
In my Python socket program, I sometimes need to interrupt it with Ctrl-C . When I do this, it does close the connection using socket.close() . However, when I try to reopen it I have to wait what seems like a minute before I can connect again. How does one correctly close a socket? Or is this intended?
16 Answers 16
Yes, this is intended. Here you can read a detailed explanation. It is possible to override this behavior by setting the SO_REUSEADDR option on a socket. For example:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Strange. I’ve called this right before binding and the error is still occurring. I must be making a mistake somewhere else.
Nevermind, I had a bizarre race condition when repeatedly binding and shutting down on separate threads for my automated tests.
IMPORTANT NOTE: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) works, BUT you should use that right after you create the socket. It will not work after .bind() !
$ ps -fA | grep python 501 81211 12368 0 10:11PM ttys000 0:03.12 python -m SimpleHTTPServer $ kill 81211
Unfortunately, it doesn’t always work. This was my immediate thought when I ran into this problem, but there is no python process to kill in my case.
Do not use kill -9. It wouldn’t work in this case anyway as the user stated that there is no python process to kill.
This happens because you trying to run service at the same port and there is an already running application. it can happen because your service is not stopped in the process stack. you just have to kill those processes.
There is no need to install anything here is the one line command to kill all running python processes.
for Linux based OS:
kill -9 $(ps -A | grep python | awk '')
kill -9 (ps -A | grep python | awk '')
If you use a TCPServer , UDPServer or their subclasses in the socketserver module, you can set this class variable (before instantiating a server):
socketserver.TCPServer.allow_reuse_address = True
This causes the init (constructor) to:
if self.allow_reuse_address: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
A simple solution that worked for me is to close the Terminal and restart it.
Nothing worked for me except running a subprocess with this command, before calling HTTPServer((», 443), myHandler) :
Of course this is only for linux-like OS!
This will show you the error. The process number (eg.35225) containing your python file is the error.
This will kill the error process and your problem will be solved.
First of all find the python process ID using this command
You will get a pid number by naming of your python process on second column
Then kill the process using this command
fuser -k (port_number_you_are _trying_to_access)/TCP
example for flask: fuser -k 5000/tcp
Also, remember this error arises when you interput by ctrl+z. so to terminate use ctrl+c
I faced similar error at odoo server and resolved that with these simple following steps:
You will get a pid number. Now copy the pid number from second column of terminal output.
The terminal will restart and then the command
Do nothing just wait for a couple of minutes and it will get resolved. It happens due to the slow termination of some processes, and that’s why it’s not even showing in the running processes list.
I had the same problem (Err98 Address already in use) on a Raspberry Pi running python for a EV charging manager for a Tesla Wall Connector. The software had previously been fine but it stopped interrogating the solar inverter one day and I spent days thinking it was something I’d done in python. Turns out the root cause was the Wifi modem assigning a new dynamic IP to the solar inverter as as result of introducing a new smart TV into my home. I changed the python code to reflect the new IP address that I found from the wifi modem and bingo, the issue was fixed.
Got this error after I ran my code while programming a Pico W via Thonny. At the command line just do a socket.reset() to clear the issue.
The cleanest way to make the socket immediately reusable is to follow the recommendation to first shutdown the client end (socket) of a connection, and make sure the server’s end shuts down last (through exception handling if needed).
This might well mean that the server end runs forever.
This is not a problem if that «forever» loop pauses execution, e.g. read from socket.
How you «break» that «forever» loop is up to you as server admin, as long as there are no clients (apart from obvious system level exceptions)
Ubuntu error with apache: (98)Address already in use
It seems port 80 is already taken. Use another port or try netstat ( grep the result to select only the row with value 80 in it), ps and kill to see what application occupies the port and shut it down.
In all cases killing the process may not work, as the process using the port 80 will get restarted and doesn’t allow to use the port. So what can be done is to change the port for apache, if that doesn’t matter.
Two things are to be changed for that:
- Open /etc/apache2/ports.conf with any text editor and change the value of the entry Listen 80 to the desired port (e.g. Listen 8080 ).
- Change the entry for to the same port number you gave in the /etc/apache2/ports.conf file in /etc/apache2/sites-enabled/000-default (e.g. ).
Make sure that you don’t have the command Listen 80 in more than one place.
In my case, I was getting the same error and the reason was that this command was both in ports.conf and sites-enabled/000-default.
Seems so obvious now that Ive found the answer, but I had this exact issue. Good call posting this answer as well.
In my case it was nginx (’cause I have it on my server).
sudo service nginx stop sudo service apache2 start
sudo netstat -tulpn| grep :80
Disable whatever virtualhost is binding to port 80 that you don’t want to (nginx?). It’s in /etc/nginx/sites-enabled or /etc/apache2/sites-enabled
sudo kill -9 -2321 (pid) Restart BT Done. no need to make changes in conf. file.
This is due to port 80 is shifted used by other service.
Check any service is using 80 still
sudo netstat -tulpn| grep :80
When you restart or start your server via terminal you might have forgot to add sudo before the command.
Use sudo /etc/init.d/apache2 reload instead of /etc/init.d/apache2 reload
I had the same issue with a very different cause. I am running Apache 2.4.7 with PHP 5.5.6 on CentOS 6.5.
I messed up php.ini by having BOTH output_handler=ob_gzhandler AND zlib.output_compression=On (either one, not both, can be set).
So on restarting Apache, it binds to port 80 but nothing else happens. It looks like it is running but php’s error locked it up somewhere.
The clue was to check «php -v» . when I saw it not returning anything (it wrote the error to error_log), I fixed php.ini and Apache was happy again.
I got this error on a fresh install of Ubuntu 12.10 when starting apache2.
It’s a bug in the apache2. It gets hung in the background. Here is my walkthrough to where the bugs might be in the software.
el@titan:~$ sudo service apache2 start * Starting web server apache2 (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Action 'start' failed. The Apache error log may have more information. [fail]
Address already in Use? What could be using it? Check it out:
el@titan:~$ grep -ri listen /etc/apache2 /etc/apache2/apache2.conf:# supposed to determine listening ports for incoming connections, and which /etc/apache2/apache2.conf:# Include list of ports to listen on and which to use for name based vhosts /etc/apache2/ports.conf:Listen 80 /etc/apache2/ports.conf: Listen 443 /etc/apache2/ports.conf: Listen 443
That means apache2 is preventing apache2 from starting. Bizarre. This will confirm:
el@titan:~$ ps -ef | grep apache2 root 1146 954 0 15:51 ? 00:00:00 /bin/sh /etc/rc2.d/S91apache2 start root 1172 1146 0 15:51 ? 00:00:00 /bin/sh /usr/sbin/apache2ctl start root 1181 1172 0 15:51 ? 00:00:00 /usr/sbin/apache2 -k start root 1193 1181 0 15:51 ? 00:00:00 /bin/bash /usr/share/apache2/ask-for-passphrase 127.0.1.1:443 RSA el 5439 5326 0 16:23 pts/2 00:00:00 grep --color=auto apache2
Yes, in this case apache2 is running, I was trying to start apache2 a second time on the same port.
What confuses me is that service reports that apache2 is NOT running:
el@titan:~$ sudo service apache2 status Apache2 is NOT running.
And when you query apache2ctl for its status, it hangs.
root@titan:~# /usr/sbin/apache2ctl status **hangs until Ctrl-C is pressed.
So Ubuntu seems to be having trouble managing apache2 on bootup. Time to stop apache2:
root@titan:~# /usr/sbin/apache2ctl stop httpd (no pid file) not running
A big clue! You try to stop apache2 and it lost the process id! So Ubuntu can’t stop apache2 because it doesn’t know where it is!
You would think a reboot would fix it, but it doesn’t because apache2 starts on boot and hangs. The normal boot process for apache2 is not working right.
So How to fix it?
I was able to fix this by analyzing the ps command output. Notice that the ps command tells us that that process was started by «/etc/rc2.d/S91apache2 start».
That is the offending program that needs a swift kick.
/etc/rc2.d/S91apache2 is the symbolic link used to start apache2 for you when the computer starts. For some reason it seems to be starting apache2 and then hangs. So we’ll have to tell it to not do that.
So go take a look at that /etc/rc2.d/S91apache2 .
el@titan:/etc/rc2.d$ ls -l lrwxrwxrwx 1 root root 17 Nov 7 21:45 S91apache2 -> ../init.d/apache2*
It’s a symbolic link that we don’t want it to be there. Do this to prevent apache2 from starting on boot:
root@titan:~# sudo update-rc.d -f apache2 remove Removing any system startup links for /etc/init.d/apache2 . /etc/rc0.d/K09apache2 /etc/rc1.d/K09apache2 /etc/rc2.d/S91apache2 /etc/rc3.d/S91apache2 /etc/rc4.d/S91apache2 /etc/rc5.d/S91apache2 /etc/rc6.d/K09apache2
Reboot the computer to make sure apache2 doesn’t start and hang. Ok good. Now you COULD put apache2 back the way it was, but that would make it fail again.
root@titan:~$ sudo update-rc.d apache2 defaults //(don't do this) Adding system startup for /etc/init.d/apache2 . /etc/rc0.d/K20apache2 -> ../init.d/apache2 /etc/rc1.d/K20apache2 -> ../init.d/apache2 /etc/rc6.d/K20apache2 -> ../init.d/apache2 /etc/rc2.d/S20apache2 -> ../init.d/apache2 /etc/rc3.d/S20apache2 -> ../init.d/apache2 /etc/rc4.d/S20apache2 -> ../init.d/apache2 /etc/rc5.d/S20apache2 -> ../init.d/apache2
Instead, start the apache2 like this:
sudo service apache2 start
And the apache2 is back up and serving pages again. There seems to be some serious bugs with apache2/Ubuntu 12.10 that causes apache2 to start and hang. This is a workaround, I suppose the fix is to get newer versions of apache2 and Ubuntu and hope for the best.