Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku app runs locally but gets H12 timeout error (uses a package)

Similar questions have been asked but the H12 seems to be caused by many things and none of the answers apply here. I have built python apps with heroku before but now I'm using a package structure per Miguel Grinberg's Flask Mega-Tutorial and I can't figure out what's wrong.

My app is python / Flask / SQLAlchemy with a Heroku postgres database. The app fully works locally. When I push to Heroku, I get an H12.

Here is my app structure:

  rootappdirectory\
      app\
        static\
        templates\
        __init__.py
        views.py
        models.py
      run.py
      [plus flask / venv files]

run.py looks like this:

import os
from flask import Flask

from app import app
app.run()

And app/__init__.py looks like this:

(a bunch of imports)

app = Flask(__name__)
db = SQLAlchemy(app)

login_manager = LoginManager()
(a bunch of login_manager stuff)

from app import views, models

My Procfile is web: gunicorn run:app

I'm using the heroku database locally and remotely. The app works perfectly on my local machine (on 0.0.0.0:5000). But when I push to heroku and run the app, here's the logs:

2013-04-15T06:50:27.165532+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/favicon.ico host=floating-driftwood-6203.herokuapp.com fwd="24.6.48.240" dyno=web.1 connect=1ms service=30007ms status=503 bytes=0
2013-04-15T06:50:34.908756+00:00 app[web.1]: 2013-04-15 06:50:34 [2] [CRITICAL] WORKER TIMEOUT (pid:65)
2013-04-15T06:50:34.914436+00:00 app[web.1]: 2013-04-15 06:50:34 [2] [CRITICAL] WORKER TIMEOUT (pid:65)
2013-04-15T06:50:34.918114+00:00 app[web.1]: 2013-04-15 06:50:34 [66] [INFO] Booting worker with pid: 66
2013-04-15T06:50:35.083182+00:00 app[web.1]:  * Running on http://127.0.0.1:5000/
2013-04-15T06:51:04.216671+00:00 app[web.1]: 2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT (pid:66)
2013-04-15T06:51:04.223440+00:00 app[web.1]: 2013-04-15 06:51:04 [2] [CRITICAL] WORKER TIMEOUT (pid:66)
2013-04-15T06:51:04.229350+00:00 app[web.1]: 2013-04-15 06:51:04 [67] [INFO] Booting worker with pid: 67

I've played with different options - at first I got a "connection in use" error which I fixed by going into debug=False, but frankly I'd rather be in debug=True! There seems to be a problem with the fact that my main app is not in the root directory?

Thanks in advance.

like image 512
Michael Levinson Avatar asked Apr 15 '13 17:04

Michael Levinson


1 Answers

The issue is that run.py unguardedly calls app.run - this actually calls werkzeug.serving.run_simple which starts a sub-process to handle incoming requests ... which you don't want to do when running under gunicorn (since gunicorn will handle the process management for you).

Simply add an if __name__ == "__main__" guard before your app.run call and everything should work:

# run.py
if __name__ == "__main__":
    app.run()
like image 129
Sean Vieira Avatar answered Oct 06 '22 01:10

Sean Vieira