5

Following this tutorial, everything works locally. After I deploy my app to Heroku and visit the app on the browser I get a 503 Error and the message:

Application Error An error occurred in the application and your page could not be served. Please try again in a few moments. If you are the application owner, check your logs for details.

The logs say:

2015-09-08T16:31:53.976824+00:00 heroku[web.1]: State changed from crashed to starting
2015-09-08T16:31:56.174376+00:00 heroku[web.1]: Starting process with command `mywebsite`
2015-09-08T16:31:59.312461+00:00 app[web.1]: Listening on port: 39461
2015-09-08T16:32:56.471550+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-09-08T16:32:56.471550+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-09-08T16:32:57.390752+00:00 heroku[web.1]: Process exited with status 137
2015-09-08T16:32:57.404208+00:00 heroku[web.1]: State changed from starting to crashed
2015-09-08T16:32:57.645135+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=boiling-eyrie-6897.herokuapp.com request_id=ec26... fwd="xx.xxx.xxx.xxx" dyno= connect= service= status=503 bytes=
2015-09-08T16:32:58.233774+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=boiling-eyrie-6897.herokuapp.com request_id=ef40...fwd="xx.xxx.xxx.xxx" dyno= connect= service= status=503 bytes=

I understand what the errors are, but how can such a tiny tutorial app be causing a boot timeout (R10)?

How can I debug this better and fix the app so it runs?

sargas
  • 5,820
  • 7
  • 50
  • 69
  • Is it possible your app is binding the right port, but only on the local connection loop and not on all of them (which would make the platform not detect the port as bound, and crash the app). – Damien MATHIEU Sep 09 '15 at 11:35
  • @DamienMATHIEU Sorry, I'm not sure of what you are saying. What does "on the local connection loop and not on all of them" mean? – sargas Sep 09 '15 at 16:18
  • If your app is only listening on `localhost`, the process watcher cannot detect the port being bound. You need to bind on all available network connections. See https://github.com/heroku/go-getting-started/blob/master/cmd/go-getting-started/main.go#L27 – Damien MATHIEU Sep 09 '15 at 17:18
  • 1
    @DamienMATHIEU That was exactly the case. Code had `http.ListenAndServe("127.0.0.1:"+port, nil)` where it must be `http.ListenAndServe(":"+port, nil)` please submit this as an answer. – sargas Sep 10 '15 at 15:06
  • 1
    Funny how you get a down vote because when asking a beginner's question. I don't want to judge why people down vote well formatted questions like this regardless of it being stupid for their level. At least leave a comment with the reason for the down vote or edit the question. – sargas Oct 01 '15 at 22:12
  • I upvoted you to compensate :) – Damien MATHIEU Oct 02 '15 at 07:43

2 Answers2

9

When you deploy an app through heroku, it does not allow you to specify the port number.
In other words, you can not specify your web service's port number as 8000 or something else, heroku decides the port number in runtime.
so, you can not use the following code:

    log.Fatal(http.ListenAndServe(":8000", router))

What you can do is, getting the runtime port of heroku.
In short, just use the following code:

    log.Fatal(http.ListenAndServe(":" + os.Getenv("PORT"), router))
Celik
  • 2,311
  • 2
  • 32
  • 54
7

Your app needs to listen to all network connections. If it only listens on localhost, heroku's process watcher will not be able to detect that you bound the port, nor send requests to your app.

That means instead of:

http.ListenAndServe("127.0.0.1:"+port, nil)

You need to call:

http.ListenAndServe(":"+port, nil)

See also the heroku getting started with Go app: https://github.com/heroku/go-getting-started/blob/master/cmd/go-getting-started/main.go#L27

Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94