-
Notifications
You must be signed in to change notification settings - Fork 247
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
Comments
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 |
Thank you for your reply. 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! |
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
I want to catch exception which is occurred in handler of
Listen
like this.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.
The text was updated successfully, but these errors were encountered: