6 months ago
Hi!
I have an express application with an express server. I'm seeing that the server sometimes restarts out of no where. I disabled the "Serverless" option in the config so the server never goes to sleep but that didn't seem to help. I was following this https://station.railway.com/questions/bad-request-error-502-0cc25a69 thread and it also mentioned a PORT bigger than 5000 so I set it to 8888 but no luck. Also added the timeouts and responded with 499 on BadRequestError but no luck either. I also thought it might be too many requests and the memory or CPU spikes but CPU usage is always below 0.1 and memory always below 240mb. There are no error logs before the server restarts and I only see this right before it restarts.
{ "requestId": "cqxKLlcGR36qUepnCx5-qw", "timestamp": "2025-09-11T01:28:45.331264326Z", "method": "POST", "path": "/graphql", "host": "cyclone.vivato.tech", "httpStatus": 502, "upstreamProto": "", "downstreamProto": "HTTP/2.0", "responseDetails": "", "totalDuration": 138, "upstreamAddress": "", "clientUa": "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/22G100 Instagram 396.1.0.47.81 (iPhone17,1; iOS 18_6_2; es_LA; es; scale=3.00; 1206x2622; IABMV/1; 787013352)", "upstreamRqDuration": 138, "txBytes": 109, "rxBytes": 3973, "srcIp": "198.41.227.15", "edgeRegion": "us-east4-eqdc4a", "upstreamErrors": "[{\"deploymentInstanceID\":\"4e68cc92-8e37-4fe3-abf1-d33857c36203\",\"duration\":138,\"error\":\"connection closed unexpectedly\"}]" }
I'm on the Pro plan
Attachments
7 Replies
6 months ago
Hey there! We've found the following might help you get unblocked faster:
If you find the answer from one of these, please let us know by solving the thread!
6 months ago
It seems to happen more and more and its random. The app can be working correctly and then it restarts.
6 months ago
Node.js defaults to closing idle connections after 5 seconds, but Railway's proxy expects them to stay open for ~60 seconds. When your server closes the connection early, Railway throws a 502 "connection closed unexpectedly" error.
Try these in your server file:
const server = app.listen(PORT, '0.0.0.0', () => {...});
//add these 2 lines below
server.keepAliveTimeout = 65000; // Keep connections alive for 65 seconds
server.headersTimeout = 66000; // Headers timeout slightly higher
Hope this helps.
6 months ago
Thanks for the response, I already have those 2 lines of code in my server plus this one
server.setTimeout(120000);
But still seeing the issue
6 months ago
your app is probably running on port 8080, thats what railpack defaults to, if you arent using your own docker image
6 months ago
Hi,
I am have also the same Problem and it occurs randomly.
at Microsoft.Extensions.Http.Resilience.ResilienceHandler.<>c.<<SendAsync>b__3_0>d.MoveNext() --extra text
info: System.Net.Http.HttpClient.HttpClientWrapper.ClientHandler[100] --extra text
Sending HTTP request POST http://domain.railway.internal:8080/api/v1/ --extra text
warn: Polly[3] --extra text
Execution attempt. Source: 'HttpClientWrapper-my-pipeline//Retry', Operation Key: '', Result: 'Connection refused (domain.internal:8080)', Handled: 'True', Attempt: '1', Execution Time: 1.4775ms --extra text
System.Net.Http.HttpRequestException: Connection refused (domain.internal:8080) --extra text
6 months ago
The “perfect fix” is to make sure the app looks alive to Railway 100% of the time. That means binding to the correct port, replying fast to health checks, and never letting a request hang or crash the process. Once those are in place, restarts will only happen when you want them to.