Skip to content

Commit

Permalink
Don't use self.logger for logging.
Browse files Browse the repository at this point in the history
The `@logger` should only be used for carrying logger state between parent
and child tasks. But using it directly for logging is likely to produce
inconsistent output because the methods of the task might not execute in the
scope of the fiber itself. In the future, we hope it would be possible to
remove this variable completely and instead have a more direct relationship
between parent and child fiber locals.
  • Loading branch information
ioquatix committed Apr 10, 2021
1 parent 5d5f587 commit d0546a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
13 changes: 5 additions & 8 deletions lib/async/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def initialize(parent = nil, selector: self.class.selector, logger: nil)
end

attr :scheduler
attr :logger

# @reentrant Not thread safe.
def block(blocker, timeout)
Expand Down Expand Up @@ -133,10 +134,6 @@ def fiber(&block)
end
end

def logger
@logger || Console.logger
end

def to_s
"\#<#{self.description} #{@children&.size || 0} children (#{stopped? ? 'stopped' : 'running'})>"
end
Expand Down Expand Up @@ -165,7 +162,7 @@ def async(*arguments, **options, &block)
# - Avoid scheduler overhead if no blocking operation is performed.
task.run(*arguments)

# logger.debug "Initial execution of task #{fiber} complete (#{result} -> #{fiber.alive?})..."
# Console.logger.debug "Initial execution of task #{fiber} complete (#{result} -> #{fiber.alive?})..."
return task
end

Expand Down Expand Up @@ -208,7 +205,7 @@ def finished?
# @param timeout [Float | nil] the maximum timeout, or if nil, indefinite.
# @return [Boolean] whether there is more work to do.
def run_once(timeout = nil)
# logger.debug(self) {"@ready = #{@ready} @running = #{@running}"}
# Console.logger.debug(self) {"@ready = #{@ready} @running = #{@running}"}

if @ready.any?
# running used to correctly answer on `finished?`, and to reuse Array object.
Expand Down Expand Up @@ -256,7 +253,7 @@ def run_once(timeout = nil)
interval = timeout
end

# logger.info(self) {"Selecting with #{@children&.size} children with interval = #{interval ? interval.round(2) : 'infinite'}..."}
# Console.logger.info(self) {"Selecting with #{@children&.size} children with interval = #{interval ? interval.round(2) : 'infinite'}..."}
if monitors = @selector.select(interval)
monitors.each do |monitor|
monitor.value.resume
Expand Down Expand Up @@ -293,7 +290,7 @@ def run(*arguments, **options, &block)
return initial_task
ensure
@scheduler&.clear!
logger.debug(self) {"Exiting run-loop because #{$! ? $! : 'finished'}."}
Console.logger.debug(self) {"Exiting run-loop because #{$! ? $! : 'finished'}."}
end

def stop(later = true)
Expand Down
16 changes: 7 additions & 9 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ def initialize(reactor, parent = Task.current?, logger: nil, finished: nil, **op
@result = nil
@finished = finished

@logger = logger || parent&.instance_variable_get(:@logger)
@logger = logger || @parent.logger

@fiber = make_fiber(&block)
end

attr :logger

if Fiber.current.respond_to?(:backtrace)
def backtrace(*arguments)
@fiber&.backtrace(*arguments)
Expand All @@ -96,10 +98,6 @@ def to_s
"\#<#{self.description} (#{@status})>"
end

def logger
@logger || Console.logger
end

# @attr ios [Reactor] The reactor the task was created within.
attr :reactor

Expand Down Expand Up @@ -242,9 +240,9 @@ def fail!(exception = nil, propagate = true)
raise
elsif @finished.nil?
# If no one has called wait, we log this as an error:
logger.error(self) {$!}
Console.logger.error(self) {$!}
else
logger.debug(self) {$!}
Console.logger.debug(self) {$!}
end
end

Expand All @@ -264,15 +262,15 @@ def make_fiber(&block)
begin
@result = yield(self, *arguments)
@status = :complete
# logger.debug(self) {"Task was completed with #{@children.size} children!"}
# Console.logger.debug(self) {"Task was completed with #{@children.size} children!"}
rescue Stop
stop!
rescue StandardError => error
fail!(error, false)
rescue Exception => exception
fail!(exception, true)
ensure
# logger.debug(self) {"Task ensure $!=#{$!} with #{@children.size} children!"}
# Console.logger.debug(self) {"Task ensure $!=#{$!} with #{@children.size} children!"}
finish!
end
end
Expand Down

0 comments on commit d0546a4

Please sign in to comment.