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

RuntimeError: grpc cannot be used before and after forking #211

Open
prateekrsingh opened this issue Jan 10, 2023 · 1 comment
Open

RuntimeError: grpc cannot be used before and after forking #211

prateekrsingh opened this issue Jan 10, 2023 · 1 comment

Comments

@prateekrsingh
Copy link

I am using the following config in my deployed environments -
Rails.application.configure do config.cache_classes = true config.eager_load = true end
If I call Temporal.start_worflow with above configuration, I get -
RuntimeError: grpc cannot be used before and after forking from /bundle/ruby/2.6.0/gems/grpc-1.50.0-x86_64-linux/src/ruby/lib/grpc/generic/client_stub.rb:493:in create_call'`

However if both cache_classes and eager_load is set to false, it works.

Any suggestions on how I can resolve this?

@GalenkoEugene
Copy link

RuntimeError: grpc cannot be used before and after forking

Description:
When using the Puma server with multiple workers, I encountered the grpc that cannot be used before and after forking error. This issue occurs when there is more than one worker in Puma. The error arises because the grpc library is not designed to be safely used before and after the forking process, which happens in a multi-process environment.

Cause:
Puma uses the fork mechanism to create multiple worker processes for handling concurrent requests. However, the grpc library may encounter conflicts if it is initialized before and after forking, leading to the mentioned error.

Solution:
To address this issue, move the initialization of grpc inside the on_worker_boot block in the Puma configuration (config/puma.rb). The on_worker_boot block ensures that the grpc library is initialized in each worker process separately, avoiding conflicts and enabling smooth functioning in a multi-worker environment.

# config/puma.rb

on_worker_boot do
  Temporal.configure do |config|
    config.host = 'localhost'
    config.port = 7233
    config.namespace = 'ruby-samples'
    config.task_queue = 'hello-world'
    config.credentials = GRPC::Core::ChannelCredentials.new # :this_channel_is_insecure does not work for me
  end
end

By moving the grpc initialization inside on_worker_boot, each worker process will have its own isolated instance of grpc, resolving the grpc cannot be used before and after forking error and ensuring proper functioning in a multi-worker environment.

or just waiting for this PR with solution

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

No branches or pull requests

2 participants