google-cloud-ruby: Thread error in fork() with Rails on MacOS

Running MacOS Mojave.

When I run the example explained here:

https://cloud.google.com/text-to-speech/docs/reference/libraries#client-libraries-install-ruby

I get this error:

objc[80556]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called. objc[80556]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.

On this line:

response = client.synthesize_speech synthesis_input, voice, audio_config

Console exits… I’ve already tried exporting this:

export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES

But that doesn’t seem to make any difference.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 22 (11 by maintainers)

Most upvoted comments

I had the same issue with google translate api, solution was to remove spring from Gemfile

You can also stop spring

bin/spring stop

Thanks @blowmage , that really helps - playing around more if I make it into a class and call it from the console it throws the error, but if I instead instantiate it within a controller it seems to run fine.

Do you know if there is a workaround where I can get this working within the context of a class that can be run from the console or runner as I intend to mainly use it in that way?

Made into basic class:

require "google/cloud/vision"

class ImageTagJob
  def perform
    detect_obj = image_annotator.label_detection image: 'https://avatars2.githubusercontent.com/u/4891353?s=460&v=4'

    detect_obj.responses.each do |response|
      response.localized_object_annotations.each do |object|
        puts "#{object.name} (confidence: #{object.score})"
      end
    end
  end

  private

  def image_annotator
    @image_annotator ||= Google::Cloud::Vision::ImageAnnotator.new
  end
end

Called in rails C: same error message

Put into a controller or similar,

class WelcomeController < ApplicationController
  def index
    ImageTagJob.new.perform
  end
end

works fine

@Rosie-Brigham If you are calling rails runner on an app that is using Spring, then that command forks the rails process before running the script. This error is raised when the GRPC gem has been instantiated on a different running process other than the current process. So it looks like your application is instantiating a GRPC client during application boot, which means every GRPC client is unable to be used in a forked process like the one rails runner creates. This is why calling the script directly does not raise the error.

@blowmage No vendored gems. bundle doctor says everything is OK. I do have spring in my Gemfile. I commented this out did bundle install and now it’s working!!! Thanks a lot for your assistance!

When I start a new project with the same Gemfile and Gemfile.lock, it does work. I run the example in the link I posted and it doesn’t give the

objc[8992]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.

error message. Both tested from console.