Comments (10)
To me it seems like a bug. If there is an exception, i need to be able to catch it.
But I do not mind using something different. Fiber.scheduler.raise
is a hack anyway.
My goal is to be able to stop a Fiber from another Fiber.
(If possible using the ruby built in methods, to be compatible with other Fiber scheduler implementations)
And I still do not know how to do it. See #263
from async.
Am I doing something wrong?
A rescue
without an explicit exception class will only catch exceptions that are descendants of StandardError
. In your example, MyException
is a descendant of Exception
, which is the lowest-level exception class. To be able to rescue your exception, you can do any of the following:
rescue MyException => e
- which will rescue onlyMyException
rescue Exception => e
- which will rescue all exceptions- define the custom exception as
class MyException < StandardError
from async.
Sorry my family has been sick I have not had time to review this issue.
from async.
Yep, that was it. Not a bug at all then, just rescue
behaving differently than I expected. Tyvm.
from async.
Thanks for the solution!
Usually i do not find such surprises using ruby.
Do you know why ruby behaves this way?
from async.
Thanks, if possible, are you able to make a MVP failing test case as a PR? This will help me greatly.
from async.
I am confused, here is a shorter version:
require 'async'
Fiber.set_scheduler(Async::Scheduler.new)
class MyException < Exception
end
@fiber = Fiber.schedule do
begin
sleep 1
rescue => e
puts e.inspect
end
end
Fiber.schedule do
sleep 0.1
Fiber.scheduler.raise(@fiber, MyException)
end
Am I doing something wrong?
from async.
The output of the above snippet is:
/Users/thomas/.sandbox/lib/ruby/gems/3.2.0/gems/async-2.6.3/lib/async/scheduler.rb:132:in `transfer': MyException (MyException)
from /Users/thomas/.sandbox/lib/ruby/gems/3.2.0/gems/async-2.6.3/lib/async/scheduler.rb:132:in `block'
from /Users/thomas/.sandbox/lib/ruby/gems/3.2.0/gems/async-2.6.3/lib/async/scheduler.rb:154:in `kernel_sleep'
from -:10:in `sleep'
from -:10:in `block in <main>'
from /Users/thomas/.sandbox/lib/ruby/gems/3.2.0/gems/async-2.6.3/lib/async/task.rb:160:in `block in run'
from /Users/thomas/.sandbox/lib/ruby/gems/3.2.0/gems/async-2.6.3/lib/async/task.rb:330:in `block in schedule'
I can create a PR for this and @ all involved if you'd like. It looks like Fiber.scheduler.raise(@fiber, MyException)
passes down to Fiber#raise
on @fiber
, so you could also probably shorten that line to simply @fiber.raise(MyException)
as I believe Fiber#raise
is the intended way to raise a Fiber
level exception. I've confirmed that the output is the same.
Regarding the issue itself, while the backtrace notes where the Fiber
is currently blocking, I'm not sure if an externally raised exception is bound by that scope, so I'm not sure if you're supposed to be able to catch it this way.
from async.
It gets weirder, oddly enough. Your example works just fine the moment you remove the custom exception class. It turns out the fact that the caller is a Fiber
might not even be relevant.
This cannot be caught:
require 'async'
Fiber.set_scheduler(Async::Scheduler.new)
class CustomException < Exception
end
fibera = Fiber.schedule do
begin
sleep 5
rescue => e
puts("exception caught")
end
end
Fiber.scheduler.raise(fibera, CustomException)
This can, and outputs "exception caught":
require 'async'
Fiber.set_scheduler(Async::Scheduler.new)
fibera = Fiber.schedule do
begin
sleep 5
rescue => e
puts("exception caught")
end
end
Fiber.scheduler.raise(fibera, RuntimeError)
I'm using Ruby 3.2. I can create the PR if you'd like. Asking because I'm not the OP.
from async.
Do you know why ruby behaves this way?
This design allows lower-level exceptions, such as Interrupt
and SystemExit
(which are raised on SIGINT
and SIGTERM
signals respectively) to propagate to the top-level and be handled centrally by either custom app code or Ruby's default handlers.
from async.
Related Issues (20)
- Unhandled Exception does not stop program running HOT 10
- [Enhanchment] Make alternate exception reporting (the colorful one) opt in. HOT 3
- [Question] How to stop a fiber? HOT 4
- Dynamic concurrency limiter / adaptive semaphore HOT 3
- Configure log level specificly for Async HOT 4
- Segmentation fault HOT 5
- ActiveRecord best practices support or documentation HOT 1
- [Question] What difference between `Async` and `Sync` HOT 1
- bundle error HOT 1
- Catch all Async errors and report to Sentry? (or other error reporting)
- How to wait for `.schedule`'d fibers to finish? HOT 7
- Properly managing interrupts (works for async v 1.31, "breaks" for async v 2.5.6) HOT 3
- Tasks signaling Conditions leave suspended Fibers behind HOT 8
- Error reporting difference between Sync{} and Async{}.wait HOT 2
- Understanding the difference of Sync usage within Falcon HOT 3
- macOS: `Errno::EINVAL: Invalid argument - IO_Event_Selector_KQueue_io_wait:IO_Event_Selector_KQueue_Waiting_register` HOT 18
- Stopping remaining tasks upon completion of one task HOT 2
- Blocking subprocess (popen3) HOT 10
- Sleep Hook HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async.