502 Bad Gateway

I’m fairly new to building servers so I apologize and I have had no real issue for quite some time until recently… I am getting a 502 Bad Gateway but I cannot figure out why?

The following is my logs after I make my POST to my server which should return some data.

proxy[3d8d9930b0d578] atl [info] Starting machine
app[3d8d9930b0d578] atl [info] [ 0.038562] PCI: Fatal: No config space access function found
app[3d8d9930b0d578] atl [info] INFO Starting init (commit: 15238e9)...
app[3d8d9930b0d578] atl [info] INFO Preparing to run: `./App serve --env production --hostname 0.0.0.0 --port 8080` as vapor
app[3d8d9930b0d578] atl [info] INFO [fly api proxy] listening at /.fly/api
app[3d8d9930b0d578] atl [info] 2023/11/06 14:14:15 listening on [fdaa:3:611:a7b:e5:ab13:f724:2]:22 (DNS: [fdaa::3]:53)
proxy[3d8d9930b0d578] atl [info] machine started in 429.456525ms
app[3d8d9930b0d578] atl [info] [ NOTICE ] Server starting on http://0.0.0.0:8080
proxy[3d8d9930b0d578] atl [info] machine became reachable in 306.377276ms
proxy[3d8d9930b0d578] atl [error] could not complete HTTP request to instance: connection closed before message completed
app[3d8d9930b0d578] atl [info] INFO Main child exited with signal (with signal 'SIGILL', core dumped? false)
app[3d8d9930b0d578] atl [info] INFO Starting clean up.
app[3d8d9930b0d578] atl [info] WARN hallpass exited, pid: 306, status: signal: 15 (SIGTERM)
app[3d8d9930b0d578] atl [info] 2023/11/06 14:14:16 listening on [fdaa:3:611:a7b:e5:ab13:f724:2]:22 (DNS: [fdaa::3]:53)
app[3d8d9930b0d578] atl [info] [ 2.276343] reboot: Restarting system
runner[3d8d9930b0d578] atl [info] machine did not have a restart policy, defaulting to restart
app[3d8d9930b0d578] atl [info] [ 0.035782] PCI: Fatal: No config space access function found
app[3d8d9930b0d578] atl [info] INFO Starting init (commit: 15238e9)...
app[3d8d9930b0d578] atl [info] INFO Preparing to run: `./App serve --env production --hostname 0.0.0.0 --port 8080` as vapor
app[3d8d9930b0d578] atl [info] INFO [fly api proxy] listening at /.fly/api
app[3d8d9930b0d578] atl [info] 2023/11/06 14:14:18 listening on [fdaa:3:611:a7b:e5:ab13:f724:2]:22 (DNS: [fdaa::3]:53)
app[3d8d9930b0d578] atl [info] [ NOTICE ] Server starting on http://0.0.0.0:8080

Am I running out of memory? Is it other issues that I’m not seeing here? Should I be concerned for other network calls that may be made?

Update #1: This appears to potentially be an intermittent issue but I’m concerned about it’s cause. I was able to send a POST request to the endpoint using Postman, then when I tried it from in the iOS application it 502 Bad Gateway returned. I then tried again via Postman, same issue 502 Bad Gateway.

Hi,

My guess would be that it is running out of memory (or some kind of resource) and hence requests may succeed intermittently. Since the logs say: could not complete HTTP request to instance: connection closed. Which would in turn trigger a 502 response:

It then restarts, you make another request, and the issue happens again.

It looks like Swift, with the mention of vapor. I don’t have any experience with Swift (someone else on here may :thinking:)

I’d suggest checking the metrics in the dashboard to look for any spikes. You could try temporarily increasing the size of the VM (CPU/RAM), trying the request again, and see if you still see the issue. It may simply need a more powerful machine.

There are commercial/free Swift monitoring tools. I had a google. You could maybe try one of those too to see if that reports any debug/reason for the crash.

Your application is crashing due to an illegal instruction:

INFO Main child exited with signal (with signal 'SIGILL', core dumped? false)

It might be worth checking some Vapor resources to see if others have encountered this, you may need to build the actual app in some way that avoids using invalid/illegal instructions.

  • Daniel

What exactly is an invalid / illegal instruction? Is this something that is Swift related or related to Fly itself.

It’s related to how your app is built/compiled, most likely.

https://support.sas.com/documentation/onlinedoc/ccompiler/doc700/html/lr1/z2005346.htm

You can google for SIGILL to read a bit more about what it is and what can cause it. I did find a few matches by googling “sigill swift vapor”.

  • Daniel

If it were to be an issue with how the application is built / compiled - wouldn’t it be application wide and not just on this one single endpoint?

Additionally, I ran some local POSTs while hosting this on my local machine. I’ve had no issues with 2 back to back POST requests.

Hi!

Maybe only that endpoint hits the code path that contains the illegal instruction.

The payload might be different, and your local machine’s configuration and capabilities might be different - so this may not necessarily be conclusive.

This thread has some tips on how to compile your app with more verbose stack trace output in case of crashes.

  • Daniel

Is there a method, via terminal (flyctl) to monitor the servers response to things much like I can do from the Xcode debug window?

Payload would be the same, machine would be different in terms of specs but the docker would the the configuration. With that said, I upgraded the machines memory from 256 to 512 - still getting the same 502 gateway error.