Python Forum
[split] Flask OSError: [Errno 98] Address already in use
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Flask OSError: [Errno 98] Address already in use
#1
we have tried running flask app from a NAS shared file system on 2 different servers.
Server-1 had reboot so due to this the flask app was stopped automatically in server-1 and in server-2 flask app is up and running.

when we brough up flask app in server-1 getting failed with below "Address already in use" error even though in server-1 port(5000)
is not getting used by any other process.

Please suggest the solution and will be much appreciated.
it has something to do with the fact that this directory is "shared" by the 2 servers and they are writing to it at the same time.

ERROR:
File "flaskenv/lib64/python3.6/site-packages/werkzeug/serving.py", line 1030, in run_simples.bind(server_address)
OSError: [Errno 98] Address already in use
flaskenv/lib64/python3.6/site-packages/flask_sqlalchemy/__init__.py:873:
FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.
Set it to True or False to suppress this warning.'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and'
Yoriz write Nov-11-2021, 11:41 AM:
Please start a new thread in future to ask a question.
I have moved this into its own thread for you this time.
Reply
#2
Can you show how you're determining that there are no other processes listening on the port?
Reply
#3
(Nov-11-2021, 12:42 PM)ndc85430 Wrote: Can you show how you're determining that there are no other processes listening on the port?
I have verified with below steps:
netstat -anp |grep 5000
lsof -i tcp:5000
Reply
#4
I mean, it really sounds like a network problem. I disagree when you say it's about a shared directory - why would a web server have knowledge of that?

Does netstat show TCP sockets without -t? Is there more than one interface on the machine? Are the commands looking at all of them (I forget what netstat does by default)?

Out of curiosity, are you on an older Unix that doesn't have ss?
Reply
#5
(Nov-11-2021, 05:06 PM)ndc85430 Wrote: I mean, it really sounds like a network problem. I disagree when you say it's about a shared directory - why would a web server have knowledge of that?

Does netstat show TCP sockets without -t? Is there more than one interface on the machine? Are the commands looking at all of them (I forget what netstat does by default)?

Out of curiosity, are you on an older Unix that doesn't have ss?

• I mean, it really sounds like a network problem.

<VB> I agree that it sounds like that but with no evidence that the port is actually in use that is why I am pointing to the shared filesystem

• I disagree when you say it's about a shared directory - why would a web server have knowledge of that?

<VB> So a little more background. The directory that hosts this flask app is shared between 2 web servers on different servers. So I was wondering if flask was somehow creating a file that was causing the second webserver to not start as it sees that it is already “running” even though it is on a different server.
The 2 servers are started with their unique host names
source /autohome/www/cgi-bin/Common/flaskenv/bin/activate
cd /autohome/www/cgi-bin/Common
echo "Flask Env Variables has exported"
export FLASK_APP=Glcodes/main.py
export FLASK_DEBUG=True
flask run --host=$(hostname -s).uhc.com

• Does netstat show TCP sockets without -t?

<VB> Yes showing tcp sockets without -t

# netstat -a | grep 5000
tcp 0 0 localhost:55000 localhost:38960 ESTABLISHED
tcp 0 0 localhost:38960 localhost:55000 ESTABLISHED


• Is there more than one interface on the machine?

<VB> No just the primary interface and the standard lo


• Are the commands looking at all of them (I forget what netstat does by default)?

<VB> Shows network status, To display active sockets for each protocol or routing table information.
lsof -i -P -n | grep LISTEN
netstat -tulpn | grep LISTEN
ss -tulpn | grep LISTEN

• Out of curiosity, are you on an older Unix that doesn't have ss?

<VB> We have ss on this server

<VB>
*******Maybe add this to the comments as well

This is part of the code in site-packages/werkzeug/serving.py that seems to be causing the issue. If I comment out the s.bind line the application starts with no issues

if use_reloader:
# If we're not running already in the subprocess that is the
# reloader we want to open up a socket early to make sure the
# port is actually available.
if not is_running_from_reloader():
if port == 0 and not can_open_by_fd:
raise ValueError(
"Cannot bind to a random port with enabled "
"reloader if the Python interpreter does "
"not support socket opening by fd."
)

# Create and destroy a socket so that any exceptions are
# raised before we spawn a separate Python interpreter and
# lose this ability.
address_family = select_address_family(hostname, port)
server_address = get_sockaddr(hostname, port, address_family)
s = socket.socket(address_family, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(server_address)
if hasattr(s, "set_inheritable"):
s.set_inheritable(True)

# If we can open the socket by file descriptor, then we can just
# reuse this one and our socket will survive the restarts.
if can_open_by_fd:
os.environ["WERKZEUG_SERVER_FD"] = str(s.fileno())
s.listen(LISTEN_QUEUE)
log_startup(s)
else:
s.close()
if address_family == af_unix:
_log("info", "Unlinking %s" % server_address)
os.unlink(server_address)

# Do not use relative imports, otherwise "python -m werkzeug.serving"
# breaks.
from ._reloader import run_with_reloader

run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
else:
inner()
Reply
#6
(Nov-12-2021, 05:45 PM)venkateshbalagiri Wrote:
(Nov-11-2021, 05:06 PM)ndc85430 Wrote: I mean, it really sounds like a network problem. I disagree when you say it's about a shared directory - why would a web server have knowledge of that?

Does netstat show TCP sockets without -t? Is there more than one interface on the machine? Are the commands looking at all of them (I forget what netstat does by default)?

Out of curiosity, are you on an older Unix that doesn't have ss?

• I mean, it really sounds like a network problem.

<VB> I agree that it sounds like that but with no evidence that the port is actually in use that is why I am pointing to the shared filesystem

• I disagree when you say it's about a shared directory - why would a web server have knowledge of that?

<VB> So a little more background. The directory that hosts this flask app is shared between 2 web servers on different servers. So I was wondering if flask was somehow creating a file that was causing the second webserver to not start as it sees that it is already “running” even though it is on a different server.
The 2 servers are started with their unique host names
source /autohome/www/cgi-bin/Common/flaskenv/bin/activate
cd /autohome/www/cgi-bin/Common
echo "Flask Env Variables has exported"
export FLASK_APP=Glcodes/main.py
export FLASK_DEBUG=True
flask run --host=$(hostname -s).uhc.com

• Does netstat show TCP sockets without -t?

<VB> Yes showing tcp sockets without -t

# netstat -a | grep 5000
tcp 0 0 localhost:55000 localhost:38960 ESTABLISHED
tcp 0 0 localhost:38960 localhost:55000 ESTABLISHED


• Is there more than one interface on the machine?

<VB> No just the primary interface and the standard lo


• Are the commands looking at all of them (I forget what netstat does by default)?

<VB> Shows network status, To display active sockets for each protocol or routing table information.
lsof -i -P -n | grep LISTEN
netstat -tulpn | grep LISTEN
ss -tulpn | grep LISTEN

• Out of curiosity, are you on an older Unix that doesn't have ss?

<VB> We have ss on this server

<VB>
*******Maybe add this to the comments as well

This is part of the code in site-packages/werkzeug/serving.py that seems to be causing the issue. If I comment out the s.bind line the application starts with no issues

if use_reloader:
# If we're not running already in the subprocess that is the
# reloader we want to open up a socket early to make sure the
# port is actually available.
if not is_running_from_reloader():
if port == 0 and not can_open_by_fd:
raise ValueError(
"Cannot bind to a random port with enabled "
"reloader if the Python interpreter does "
"not support socket opening by fd."
)

# Create and destroy a socket so that any exceptions are
# raised before we spawn a separate Python interpreter and
# lose this ability.
address_family = select_address_family(hostname, port)
server_address = get_sockaddr(hostname, port, address_family)
s = socket.socket(address_family, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(server_address)
if hasattr(s, "set_inheritable"):
s.set_inheritable(True)

# If we can open the socket by file descriptor, then we can just
# reuse this one and our socket will survive the restarts.
if can_open_by_fd:
os.environ["WERKZEUG_SERVER_FD"] = str(s.fileno())
s.listen(LISTEN_QUEUE)
log_startup(s)
else:
s.close()
if address_family == af_unix:
_log("info", "Unlinking %s" % server_address)
os.unlink(server_address)

# Do not use relative imports, otherwise "python -m werkzeug.serving"
# breaks.
from ._reloader import run_with_reloader

run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
else:
inner()
Reply
#7
Could you please check my response on your asked queries.
Reply
#8
(Nov-17-2021, 01:23 PM)venkateshbalagiri Wrote: Could you please check my response on your asked queries.
Reply
#9
Could you please update this thread.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] flask 500 internal server error DarthTensor 3 556 Nov-11-2021, 06:10 AM
Last Post: DarthTensor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020