8000 Better error handling when scheduler is closed. (#240) · socketry/async@7713289 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7713289

Browse files
authored
Better error handling when scheduler is closed. (#240)
1 parent 5423657 commit 7713289

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

lib/async/scheduler.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
module Async
1818
# Handles scheduling of fibers. Implements the fiber scheduler interface.
1919
class Scheduler < Node
20+
class ClosedError < RuntimeError
21+
def initialize(message = "Scheduler is closed!")
22+
super
23+
end
24+
end
25+
2026
# Whether the fiber scheduler is supported.
2127
# @public Since `stable-v1`.
2228
def self.supported?
@@ -233,7 +239,7 @@ def run_once(timeout = nil)
233239

234240
# Run the reactor until all tasks are finished. Proxies arguments to {#async} immediately before entering the loop, if a block is provided.
235241
def run(...)
236-
Kernel::raise RuntimeError, 'Reactor has been closed' if @selector.nil?
242+
Kernel::raise ClosedError if @selector.nil?
237243

238244
initial_task = self.async(...) if block_given?
239245

@@ -260,6 +266,8 @@ def run(...)
260266
# @returns [Task] The task that was scheduled into the reactor.
261267
# @deprecated With no replacement.
262268
def async(*arguments, **options, &block)
269+
Kernel::raise ClosedError if @selector.nil?
270+
263271
task = Task.new(Task.current? || self, **options, &block)
264272

265273
# I want to take a moment to explain the logic of this.

lib/async/task.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def initialize(message = "execution expired")
5959
#
6060
# @public Since `stable-v1`.
6161
class Task < Node
62+
class FinishedError < RuntimeError
63+
def initialize(message = "Cannot create child task within a task that has finished execution!")
64+
super
65+
end
66+
end
67+
6268
# @deprecated With no replacement.
6369
def self.yield
6470
Fiber.scheduler.transfer
@@ -164,7 +170,7 @@ def run(*arguments)
164170

165171
# Run an asynchronous task as a child of the current task.
166172
def async(*arguments, **options, &block)
167-
raise "Cannot create child task within a task that has finished execution!" if self.finished?
173+
raise FinishedError if self.finished?
168174

169175
task = Task.new(self, **options, &block)
170176

test/async/reactor.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,17 @@
267267
expect(reactor.to_s).to be =~ /stopped/
268268
end
269269
end
270+
271+
it "validates scheduler assignment" do
272+
# Assign the scheduler:
273+
reactor = self.reactor
274+
275+
# Close the previous scheduler:
276+
Async {}
277+
278+
expect do
279+
# The reactor is closed:
280+
reactor.async {}
281+
end.to raise_exception(Async::Scheduler::ClosedError)
282+
end
270283
end

0 commit comments

Comments
 (0)
0