Rails 5 server hangs when receives multiple requests at once
Asked Answered
N

2

6

My development Rails 5 server with Puma keeps freezing and hanging when sending multiple requests at one time from my separate frontend app to the Rails API. There is no error, it just hangs on the POST requests. When I try to kill the server with CTRL + C, nothing happens. I have to manually kill the port.

I've tried setting config.eager_load=true in development.rb. I've tried adding config.allow_concurrency in application.rb. I've Googled relentlessly to no avail. I am sending around 5 requests concurrently from frontend, so I believe this amount of requests is causing it, but I don't know for sure.

Has anyone else experienced this or have an idea of what needs to be done here? I can usually get all the requests coming back to the frontend successfully around 3-4 times, then the server just freezes.

It especially occurs after I change any one line of code in any file in the project while the server is running.

Nievesniflheim answered 19/4, 2018 at 7:38 Comment(1)
Honestly, this sounds more like a deadlock or a never-ending loop than a server issue. Granted, the server might prevent CRTL+C from killing the process (as the signal would be recognized and handled specifically), but I think the hanging part might be rooted in a loop or a deadlock somewhere in your code.Footpound
N
2

It's been nearly 2 years but I finally happened to stumble upon what had been causing my issue.

Basically it boiled down to a method in my code not being thread-safe. Since my current_user variable was only accessible from my controller, I had a before_action on my base controller to assign the current user to User.current so that I could access the current user globally via User.current, not just in my controllers.

So PLEASE make sure you're not dynamically updating classes like I this in your controllers. It is not thread-safe. I ended up following this thread-safe solution instead for my particular case: https://mcmap.net/q/244398/-access-current_user-in-model

Nievesniflheim answered 1/3, 2020 at 7:40 Comment(0)
P
0

What is your puma configuration? How many threads and workers(Puma workers not rails workers).

Ensure that your puma has enough threads, and that your db pool is large enough. Changing a line of code should not cause your server to get exhausted in resources. Are you using a watcher like watchman?

Polynesian answered 19/4, 2018 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.