Skip to content

Commit

Permalink
Check for completed futures before setting dispatcher and yielding th…
Browse files Browse the repository at this point in the history
…e fiber
  • Loading branch information
jeffschoner-stripe committed Nov 2, 2021
1 parent 9896447 commit 31a6c8f
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/temporal/workflow/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,24 +227,32 @@ def wait_for(*futures, &unblock_condition)
blocked = true

if futures.any?
should_yield = true
futures.each do |future|
dispatcher.register_handler(future.target, Dispatcher::WILDCARD) do
if blocked && future.finished?
blocked = false
fiber.resume
if futures.all?(&:finished?)
blocked = false
else
should_yield = true
futures.each do |future|
dispatcher.register_handler(future.target, Dispatcher::WILDCARD) do
if blocked && future.finished?
# Because this block can run for any dispatch, ensure the fiber is only
# resumed one time by checking if it's already been unblocked.
blocked = false
fiber.resume
end
end
end
end
end

if unblock_condition
unless unblock_condition.call
if unblock_condition.call
blocked = false
else
should_yield = true

dispatcher.register_handler(Dispatcher::WILDCARD, Dispatcher::WILDCARD) do
# Because this block can run for any dispatch, ensure the fiber is only
# resumed one time by checking if it's alive before resuming.
# resumed one time by checking if it's already been unblocked.
if blocked && unblock_condition.call
blocked = false
fiber.resume
Expand All @@ -253,9 +261,7 @@ def wait_for(*futures, &unblock_condition)
end
end

if should_yield
Fiber.yield
end
Fiber.yield if should_yield

return
end
Expand Down

0 comments on commit 31a6c8f

Please sign in to comment.