AaronDT
AaronDT

Reputation: 4060

Flask Google Cloud App Engine: OSError: [Errno 98] Address already in use

I am trying to deploy a flask app on google cloud app engine. It runs smooth in my virtual environment locally but I get an 502 error running it in the cloud.

Now I am trying to debug my code on the cloud server, using debug mode and SSH into my instance. Using docker exec -it [ID] /bin/bash I am able to to access the root of my application. Now I upon running python app.py I get the following error:

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "app.py", line 479, in <module>
    app.run(port=8080)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 941, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple
    inner()
  File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner
    fd=fd)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server
    passthrough_errors, ssl_context, fd=fd)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__
    self.address_family), handler)
  File "/usr/local/lib/python3.6/socketserver.py", line 453, in __init__
    self.server_bind()
  File "/usr/local/lib/python3.6/http/server.py", line 136, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/local/lib/python3.6/socketserver.py", line 467, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

I've been trying to kill those processes listed when I run:

ps -fA | grep python

However, this does not solve the problem of the address being in use. Also changing the port in the app.run() does not solve the issue for me.

Upvotes: 4

Views: 2245

Answers (3)

Cain
Cain

Reputation: 262

I had a similar problem, it was caused by the Flask app also being run when the module was loaded, because I had

if __name__ == "__main__":
    app.run()

at the bottom. Note that the recent requirement to name your sever file "main.py" could cause this bug to emerge.

Upvotes: 4

AaronDT
AaronDT

Reputation: 4060

While I was not able to figure out how to "free" the running address, I solved the problem by starting another flask process by running it on a different port like so:

flask run --port=80

Upvotes: 1

Dmytro Lopushanskyy
Dmytro Lopushanskyy

Reputation: 1284

I think the problem is that you don't need to specialize the port for cloud. Google Cloud finds the port to run your app on its own. So instead of app.run(port=8080) just write app.run()

Upvotes: 2

Related Questions