Skip to content

Commit

Permalink
Opt-in to task failures using $DEBUG only.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 14, 2024
1 parent 6874cb9 commit afe95f2
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,14 @@ def completed!(result)
@status = :completed
end

# This is a very tricky aspect of tasks to get right. I've modelled it after `Thread` but it's slightly different in that the exception can propagate back up through the reactor. If the user writes code which raises an exception, that exception should always be visible, i.e. cause a failure. If it's not visible, such code fails silently and can be very difficult to debug.
def failed!(exception = false, propagate = true)
# State transition into the failed state.
def failed!(exception = false)
@result = exception
@status = :failed

if exception
if propagate
raise exception
elsif @finished.nil?
# If no one has called wait, we log this as a warning:
Console::Event::Failure.for(exception).emit(self, "Task may have ended with unhandled exception.", severity: :warn)
else
Console::Event::Failure.for(exception).emit(self, severity: :debug)
if $DEBUG
Fiber.blocking do
$stderr.puts "Task #{self} failed:", exception.full_message
end
end
end
Expand Down Expand Up @@ -407,9 +402,12 @@ def schedule(&block)
rescue Stop
stopped!
rescue StandardError => error
failed!(error, false)
failed!(error)
rescue Exception => exception
failed!(exception, true)
failed!(exception)

# This is a critical failure, we should stop the reactor:
raise
ensure
# Console.info(self) {"Task ensure $! = #{$!} with #{@children&.size.inspect} children!"}
finish!
Expand Down

0 comments on commit afe95f2

Please sign in to comment.