Skip to content

Commit

Permalink
Ensure that defer_stop resets state. (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Aug 8, 2024
1 parent 87d2b0b commit 8ff44eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,23 @@ def defer_stop
# - false: defer_stop has been called and we are not stopping.
# - true: defer_stop has been called and we will stop when exiting the block.
if @defer_stop.nil?
# If we are not deferring stop already, we can defer it now:
@defer_stop = false

begin
# If we are not deferring stop already, we can defer it now:
@defer_stop = false

yield
rescue Stop
# If we are exiting due to a stop, we shouldn't try to invoke stop again:
@defer_stop = nil
raise
ensure
defer_stop = @defer_stop

# We need to ensure the state is reset before we exit the block:
@defer_stop = nil

# If we were asked to stop, we should do so now:
if @defer_stop
@defer_stop = nil
if defer_stop
raise Stop, "Stopping current task (was deferred)!"
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,18 @@ def sleep_forever
child_task.stop
expect(child_task).to be(:stopped?)
end

it "can defer stop and exit normally" do
child_task = reactor.async do |task|
task.defer_stop do
# Nothing.
end
end

reactor.run_once(0)

expect(child_task.stop_deferred?).to be == nil
end
end

with "failing task" do
Expand Down

0 comments on commit 8ff44eb

Please sign in to comment.