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 #259

Open
taman9333 opened this issue Nov 30, 2024 · 1 comment
Open

RuntimeError - grpc cannot be used before and after forking #259

taman9333 opened this issue Nov 30, 2024 · 1 comment

Comments

@taman9333
Copy link

I am using Falcon in my Sinatra app & I am using mongodb & etcd.

However I have an issue, in preload.rb file I have initialized a singleton counter

require_relative '../services/counter_service'

CounterService.initialize_counter_range

and here is the counter_service.rb file

require 'securerandom'
require 'thread'
require 'etcdv3'
require_relative '../initializers/etcd'
class CounterService
RANGE_SIZE = 1000

  class << self
    def initialize_counter_range
      self.counter_range = get_counter_range
      self.counter = counter_range.first
    end

    def get_next_counter
      current_counter = counter
      if current_counter >= counter_range.last
      puts "Worker process #{Process.pid} Exhausted counter"
      self.counter_range = get_counter_range
      self.counter = counter_range.first
      current_counter = counter
      end
      self.counter += 1
      current_counter
    end

    private

    attr_accessor :counter_range, :counter
    attr_reader :counter_mutex

    def get_counter_range
      loop do
        current_value = EtcdClient.client.get(COUNTER_KEY).kvs.first&.value.to_i
        new_value = current_value + RANGE_SIZE

        txn = EtcdClient.client.transaction do |txn|
          txn.compare = [
            txn.value(COUNTER_KEY, :equal, current_value.to_s),
          ]
          txn.success = [
            txn.put(COUNTER_KEY, new_value.to_s)
          ]
        end

        if txn.succeeded
          puts "Instance #{ENV['SERVICE_NAME']} obtained counter range #{current_value} to #{new_value - 1}"
          return (current_value...new_value)
        end
      end
    end

    def counter_mutex
      @counter_mutex ||= Mutex.new
    end
  end
end

I got this error after first counter range being exhausted & trying to get next counter

Worker process 32 Exhausted counter
RuntimeError - grpc cannot be used before and after forking:
/usr/local/bundle/gems/grpc-1.40.0/src/ruby/lib/grpc/generic/client_stub.rb:49:in `initialize'
/usr/local/bundle/gems/grpc-1.40.0/src/ruby/lib/grpc/generic/client_stub.rb:49:in `new'
/usr/local/bundle/gems/grpc-1.40.0/src/ruby/lib/grpc/generic/client_stub.rb:49:in `setup_channel'
/usr/local/bundle/gems/grpc-1.40.0/src/ruby/lib/grpc/generic/client_stub.rb:104:in `initialize'
/usr/local/bundle/gems/grpc-1.40.0/src/ruby/lib/grpc/generic/service.rb:158:in `initialize'
/usr/local/bundle/gems/etcdv3-0.11.6/lib/etcdv3/auth.rb:13:in `new'
/usr/local/bundle/gems/etcdv3-0.11.6/lib/etcdv3/auth.rb:13:in `initialize'
counter_service.rb:46:in `block in get_counter_range'
counter_service.rb:45:in `loop'
counter_service.rb:45:in `get_counter_range'
counter_service.rb:30:in `block in get_next_counter'
counter_service.rb:21:in `synchronize'
counter_service.rb:21:in `get_next_counter'
@ioquatix
Copy link
Member

ioquatix commented Nov 30, 2024

You may check: grpc/grpc#33430

Maybe try: GRPC_ENABLE_FORK_SUPPORT=1 falcon serve ...

It's also possible you are pre-loading the gem which is causing this problem. Are you using preload.rb or a bundle gem group called preload?

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

No branches or pull requests

2 participants