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

Exception doesn't propagete #362

Closed
userlocalhost opened this issue Jan 9, 2016 · 3 comments
Closed

Exception doesn't propagete #362

userlocalhost opened this issue Jan 9, 2016 · 3 comments

Comments

@userlocalhost
Copy link

I want to catch exception which is occurred in handler of Listen like this.

begin
  listen = Listen.to '.' do |mod, add, rem|
    raise
  end
  listen.start

  ### some processing
  sleep

rescue Exception => e
  ### Couldn't catch exception
end

When I execute this and modify a file in the current directory, an exception message is put out to STDERR.
But I couldn't catch exception.

Please tell me how I can do it.

@e2
Copy link
Contributor

e2 commented Jan 9, 2016

The block's code is called from a different thread, so you'll need a Queue to collect the exception and raise it in the main thread.

What you probably want is this (it should work - I haven't checked):

require 'thread'

begin
  exceptions = Queue.new
  listen = Listen.to '.' do |mod, add, rem|
    begin
      raise
    rescue Exception => e
      exceptions << e
      listen.stop # to prevent more events/callbacks
      Thread.main.wakeup # to interrupt the 'sleep' below
    end
  end

  listen.start

  ### some processing

  sleep # will be interrupted by `wakeup` above

  raise exceptions.pop unless exceptions.empty?

rescue Exception => e
  # handling
end

@userlocalhost
Copy link
Author

Thank you for your reply.
I could catch an exception on the main thread as below.

begin
  exceptions = Queue.new
  listen = Listen.to '.' do |mod, add, rem|
    begin
      raise
    rescue Exception => e
      exceptions << e
      Thread.main.wakeup # to interrupt the 'sleep' below
    end
  end

  listen.start

  ### some processing

  sleep # will be interrupted by  above

  # calling this in the block of listener causes hangup in my environment
  listen.stop

  raise exceptions.pop unless exceptions.empty?
rescue Exception => e
  # handling
end

The problem was settled by your favor. Thank you!

@github0013
Copy link

This is an old issue, but I thought this is worth mentioning.

# need an exception not extended from StandardError
# otherwise the exception will be rescued inside Listen
class StopError < Interrupt; end

begin
  previous = Thread.abort_on_exception
  # https://ruby-doc.org/core-2.6.3/Thread.html#method-c-abort_on_exception-3D
  Thread.abort_on_exception = true

  dir = __dir__
  puts "*"*20
  puts "add some file @ #{File.expand_path dir}"
  puts "*"*20
  Listen.to(dir) do |modified, added, removed|
    raise StopError
  end.start

  sleep

rescue StopError => ex
  puts "rescued"
ensure
  Thread.abort_on_exception = previous
end

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

3 participants