facebook/tornado — Issue #434: socket.error: [Errno 98] Address already in use
Author: pkienzle
Created: 2012-01-10 17:36:57+00:00
State: open

I'm getting the following in netutil.py when opening a port:

File "/home/pkienzle/pyenv/socket/lib/python2.7/site-packages/tornado-2.1.1-py2.7.egg/tornado/netutil.py", line 267, in bind_sockets
sock.bind(sockaddr)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

The issue is that socket.getaddrinfo() is returning duplicate entries for localhost on my server (python 2.7.2, ubuntu 11.10):

>>> import socket
>>> print socket.getaddrinfo('localhost',8014,0,socket.SOCK_STREAM,0,33)
[(2, 1, 6, '', ('127.0.0.1', 8014)), (2, 1, 6, '', ('127.0.0.1', 8014))]

My solution was to use set() to make the list of address unique:

for res in set(socket.getaddrinfo(address, port, family, socket.SOCK_STREAM,
0, flags)):