Sara
Sara

Reputation: 623

OSError: [Errno 98] Address already in use

I have written a simple REST API using flask web framework. It is working as expected on my local machine.

I tried deploying the same to Google App Engine. When I run it, I get the following error.

 "/opt/python3.9/lib/python3.9/socketserver.py", line 475, in server_activate      self.socket.listen(self.request_queue_size)  OSError: [Errno 98] Address already in use

app.yaml

runtime: python39

requirements.txt

Flask==1.1.2
firebase-admin==4.5.3
pyfcm==1.5.1

main.py

import flask
import firebase_admin
import pyfcm 


from flask import Flask, request, jsonify
from firebase_admin import credentials, firestore, initialize_app
from pyfcm import FCMNotification


# Initialize Flask App
app = flask.Flask(__name__)
# app.config["DEBUG"] = True

cred = credentials.Certificate('key.json')
default_app = initialize_app(cred)
db = firestore.client()


@app.route('/', methods=['GET'])
def home():
    return "<h1>Welcome</h1>"

app.run()

The command sudo netstat -nlp | grep 1 produces this output:

tcp        0      0 127.0.0.1:8998          0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:65001           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      117/sshd

Are these valid listeners? How can I find out what these processes are about?

Trace is as follows.

File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
      worker.init_process()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
      super().init_process()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/base.py", line 119, in init_process
      self.load_wsgi()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
      self.wsgi = self.app.wsgi()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
      self.callable = self.load()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
      return self.load_wsgiapp()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
      return util.import_app(self.app_uri)
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/util.py", line 358, in import_app
      mod = importlib.import_module(module)
    File "/opt/python3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
    File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 790, in exec_module
    File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
    File "/srv/main.py", line 111, in <module>
      app.run()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 990, in run
      run_simple(host, port, self, **options)
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/werkzeug/serving.py", line 1052, in run_simple
      inner()
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/werkzeug/serving.py", line 996, in inner
      srv = make_server(
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/werkzeug/serving.py", line 847, in make_server
      return ThreadedWSGIServer(
    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/werkzeug/serving.py", line 740, in __init__
      HTTPServer.__init__(self, server_address, handler)
    File "/opt/python3.9/lib/python3.9/socketserver.py", line 452, in __init__
      self.server_bind()
    File "/opt/python3.9/lib/python3.9/http/server.py", line 138, in server_bind
      socketserver.TCPServer.server_bind(self)
    File "/opt/python3.9/lib/python3.9/socketserver.py", line 466, in server_bind
      self.socket.bind(self.server_address)
  OSError: [Errno 98] Address already in use

Upvotes: 0

Views: 9821

Answers (1)

vitooh
vitooh

Reputation: 4262

The best to explain it is to look at example from google. The app.run() should be only for local testing. The example has following comment:

if __name__ == '__main__':
    # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. This
    # can be configured by adding an `entrypoint` to app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)

So it's enough to add similar if statement before app.run(). I have replicated your code on my side and I got same error. After adding the if statement working fine on app engine.

Upvotes: 1

Related Questions