Getting a few "instance refused connection" when rails app first loads

We’re moving a rails app from Heroku to fly.io. We noticed that:

  1. The app seems to restart frequently
  2. When the app first starts up, we get a handful of these errors: “instance refused connection. is your app listening on 0.0.0.0:3000? make sure it is not only listening on 127.0.0.1 (hint: look at your startup logs, servers often print the address they are listening on)”

I got help from fly.io support to resolve both, so I’ll post the answers here in case it helps others…

The reason the app restarts frequently is that we have the following in our fly.toml:

  auto_stop_machines = true
  auto_start_machines = true

These settings allow fly.io to automatically spin machines down when load is lower, and spin them back up when there’s a need.

The reason for the “instance refused connection” errors is that the fly.io router begins routing requests to the machine right away, before Puma / Rails gets a chance to start up. The workaround here is to use the following settings in fly.toml:

[[http_service.checks]]
  # Pick an appropriate wait period for your app, and add some buffer.
  grace_period = "10s"

More details:

1 Like

For reference, I saw the following other threads that may have been about the same issue. Those threads are all closed so I can’t add to them:

1 Like

Wow this is great work. We can probably do better by default for Rails apps here.

I’m not seeing that with other Rails apps, is there anything unusual about your startup that you are aware of?

Quick question: when you get “instance refused connection” in the logs, are you also seeing errors from clients / browsers? The proxy will log that error while it does retries, so if all you’re seeing is logs and no client errors, you can safely ignore those.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.