coinbase / temporal-ruby

Ruby SDK for Temporal
Apache License 2.0
219 stars 84 forks source link

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

Open prateekrsingh opened 1 year ago

prateekrsingh commented 1 year ago

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:increate_call'`

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

Any suggestions on how I can resolve this?

GalenkoEugene commented 1 year ago

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