Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide appropriate mechanism for forking to make GRPC work #3213

Open
Nowaker opened this issue Aug 20, 2023 · 4 comments
Open

Provide appropriate mechanism for forking to make GRPC work #3213

Nowaker opened this issue Aug 20, 2023 · 4 comments
Labels

Comments

@Nowaker
Copy link

Nowaker commented Aug 20, 2023

Is your feature request related to a problem? Please describe.
I'm getting this error when trying to use GRPC in Puma in production deployment which is clustered:

grpc cannot be used between calls to GRPC.prefork and GRPC.postfork_child or GRPC.postfork_parent

To use GRPC, I must call GRPC.prefork and then GRPC.postfork_child or GRPC.postfork_parent depending on the context. However, with current Puma DSL, there is no way to know whether I should call postfork_child or postfork_parent because that information isn't exposed in any way. This isn't enough:

before_fork do
  GRPC.prefork
end

after_worker_fork do |index|
  GRPC.postfork_child # or GRPC.postfork_parent
end

Describe the solution you'd like
Two options:

  1. Second parameter in after_worker_fork to indicate whether the current process is a child or a parent.
  2. If parent is never used as a worker, introduce after_parent_fork where GRPC.postfork_parent would belong to.

Or, if my understanding is wrong, and there is a way to execute these three GRPC methods properly, a documentation update is needed, and, ideally, an example on how to handle GRPC forking in Puma.

Describe alternatives you've considered
None.

Additional context
Add any other context or screenshots about the feature request here.

@Nowaker
Copy link
Author

Nowaker commented Aug 20, 2023

I started looking at Puma source, and figured this is already possible. I think the language in documentation is confusing, and the only way to really know what is executed where is to look at Puma source.

I think this example should be incorporated into Puma's documentation as it's not possible to use GRPC in clustered Puma without this config.

is_mac = RUBY_PLATFORM.include?('darwin')

on_worker_fork do |index| # Hook name: before_worker_fork
  GRPC.prefork unless is_mac
end

after_worker_fork do |index|
  GRPC.postfork_parent unless is_mac
end

on_worker_boot do
  GRPC.postfork_child unless is_mac
end

@nateberkopec
Copy link
Member

Hey @Nowaker, I'm glad you were able to figure it out.

I'm not super happy with the names of our fork hooks either, but it's basically a historical artifact of how they were added on one-by-one over the years.

I wonder if a new set of aliases could make this clearer...

I'm also not super happy with how we don't have a transcription of the full config in DSL in the docs folder. I wonder if we could auto-generate that.

@zedbydade
Copy link

Hey @nateberkopec.

By "transcription," do you mean something like a dsl.md file where we'll have the comments from the dsl.rb file? If so, we can generate that by a script and add some configuration examples(Like the GRPC one).

@nateberkopec
Copy link
Member

By "transcription," do you mean something like a dsl.md file where we'll have the comments from the dsl.rb file? If so, we can generate that by a script and add some configuration examples(Like the GRPC one).

Yes, exactly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants