Skip to content

Google::Cloud::Vision::ImageAnnotator does not exit from the process after all client code is executed. #2826

@NathanBockrath

Description

@NathanBockrath

Hello, Google Tech Support;

When we try to run a simple example using the ruby gem with google-cloud-vision (0.32.2), we see that the script/program hangs and does not return to the shell prompt after completing all of it's image related tasks.

Similar examples run from CSharp or Java do not show this behavior.

Here is the template info:

Environment details

  • OS: Windows 10 Enterprise Version 10.0.16299 Build 16299

  • Ruby version:

$ ruby --version
ruby 2.5.0p0 (2017-12-25 revision 61468) [x64-mingw32]

  • Gem name and version:

$ gem list | grep google
google-api-client (0.27.0)
google-cloud-asset (0.1.1)
google-cloud-bigquery (1.10.0)
google-cloud-bigquery-data_transfer (0.2.3)
google-cloud-bigtable (0.2.0)
google-cloud-container (0.3.0)
google-cloud-core (1.2.7)
google-cloud-dataproc (0.2.2)
google-cloud-datastore (1.4.4)
google-cloud-dialogflow (0.2.3)
google-cloud-dlp (0.8.0)
google-cloud-dns (0.29.4)
google-cloud-env (1.0.5)
google-cloud-error_reporting (0.30.5)
google-cloud-vision (0.32.2)
google-gax (1.5.0, 1.4.0)
google-protobuf (3.6.1 x64-mingw32)
googleapis-common-protos (1.3.7)
googleapis-common-protos-types (1.0.2)
googleauth (0.8.0, 0.6.7)
grpc-google-iam-v1 (0.6.9)

Steps to reproduce

  1. set credentials variable ( in Git Bash):

$ env | grep GOOGLE
GOOGLE_APPLICATION_CREDENTIALS=/c/Users/nbockrath/.credentials/key.json

  1. attempt to run following code:

Code example

# example
#!/usr/bin/env ruby
require 'google/cloud/vision'
require 'pathname'

def dbg(msg)
  puts "#{Time.now.inspect} : #{msg}"
end

# Demonstrate the Google OCR system with a few image files.
class Demo
  def ocr_text_count
    1000
  end

  def ocr_timeout
    60
  end

  def image_list(image_path)
    my_images = Dir.glob(image_path + '/*')
    my_images.reject { |img| File.directory?(img) }
  end

  def do_multiple_ocr(image_path)
    annotator = Google::Cloud::Vision::ImageAnnotator.new(timeout: ocr_timeout)
    annotator.text_detection(images: image_list(image_path),
                             async: false,
                             max_results: ocr_text_count)
             .responses.map do |res|
               { 'image_text' => res.text_annotations.first.description }
             end
  end

  def print_texts(texts)
    texts.each_with_index do |text, index|
      dbg "#{index}: TEXT:\n#{text['image_text']}\n"
    end
  end

  def run
    @home_dir = ENV['HOME']
    @image_path = @home_dir + '/demo/image'
    texts = do_multiple_ocr(@image_path)
    print_texts(texts)
  end
end

def write_pid_file
  pid_filename = "#{Pathname.pwd}/demo.pid"
  dbg "Write PID (#{Process.pid}) to file: #{pid_filename}"
  pid_file = File.open(pid_filename, 'w')
  pid_file.write(Process.pid.to_s)
  pid_file.close
end

if $PROGRAM_NAME == __FILE__
  dbg 'DEMO.LAUNCH STARTED.'
  Demo.new.run
  puts "\n\n"
  dbg  'DEMO.LAUNCH COMPLETED.'

  write_pid_file

  # Windows process kill attempt:
  dbg "taskkill /im #{Process.pid} /f /t >nul 2>&1"

  # The following suppresses all the program output to the console.
  #   system("taskkill /im #{Process.pid} /f /t")

  # Native ruby form:
  #   Process.kill('QUIT', Process.pid)

  # Try something stupid to make it exit.
  exit 

  # It doesn't exit. Do we need a way to clean up the annotation client when done?
end

Output looks like the following:

$ ./demo.rb
2019-01-17 16:32:39 -0600 : DEMO.LAUNCH STARTED.
2019-01-17 16:33:03 -0600 : 0: TEXT:
About Store
Gmail Images
Sign in
Google
Google Search
I'm Feeling Lucky
Advertising Business
Privacy Terms Settings


2019-01-17 16:33:03 -0600 : DEMO.LAUNCH COMPLETED.
2019-01-17 16:33:03 -0600 : Write PID (14604) to file: C:/Users/nbockrath.KABI/git/googleocr/demo.pid
2019-01-17 16:33:03 -0600 : taskkill /im 14604 /f /t >nul 2>&1


Note that the Git Bash prompt does not return after the pid file is created.
Inserting exit at end of script has no effect.
Running in a thread and sending the QUIT signal to it when we detect it finished has no effect.

Hitting Ctrl-C from the git bash window: No effect.

The process/task running the code is seen to be idle in the Task Manager.

So, why isn't the prompt being returned? Did we miss something important in our setup?

Thanks!

Metadata

Metadata

Assignees

Labels

api: visionIssues related to the Cloud Vision API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions