Skip to content

Commit

Permalink
Don't run scheduler when there is a pending exception. (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Jul 24, 2023
1 parent 3fad47a commit cd53615
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
17 changes: 17 additions & 0 deletions fixtures/child_process.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

module ChildProcess
def self.spawn(code)
lib_path = File.expand_path("../lib", __dir__)

IO.popen(["ruby", "-I#{lib_path}"], "r+", err: [:child, :out]) do |process|
process.write(code)
process.close_write

return process.read
end
end
end
5 changes: 4 additions & 1 deletion lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def initialize(parent = nil, selector: nil)
end

def scheduler_close
self.run
# If the execution context (thread) was handling an exception, we want to exit as quickly as possible:
unless $!
self.run
end
ensure
self.close
end
Expand Down
47 changes: 47 additions & 0 deletions test/fiber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright, 2022, by Samuel Williams.

require 'async/reactor'
require 'child_process'

describe Fiber do
with '.schedule' do
Expand All @@ -26,5 +27,51 @@

expect(sequence).to be == [0, 1, 2]
end

it 'correctly handles exceptions in process' do
buffer = ChildProcess.spawn(<<~RUBY)
require 'async'
scheduler = Async::Scheduler.new
Fiber.set_scheduler(scheduler)
Fiber.schedule do
sleep(1)
puts "Finished sleeping!"
end
raise "Boom!"
RUBY

expect(buffer).to be(:include?, "Boom!")
expect(buffer).not.to be(:include?, "Finished sleeping!")
end

it 'correctly handles exceptions' do
finished_sleeping = nil

thread = Thread.new do
# Stop the thread logging on exception:
Thread.current.report_on_exception = false

scheduler = Async::Scheduler.new
Fiber.set_scheduler(scheduler)

finished_sleeping = false

Fiber.schedule do
sleep(10)
finished_sleeping = true
end

raise "Boom!"
end

expect{thread.join}.to raise_exception(RuntimeError,
message: be == "Boom!"
)

expect(finished_sleeping).to be == false
end
end
end

0 comments on commit cd53615

Please sign in to comment.