Skip to content

Wait for activity completions on worker shutdown #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions temporalio/worker/_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ async def drain_poll_queue(self) -> None:
except temporalio.bridge.worker.PollShutdownError:
return

# Only call this after run()/drain_poll_queue() have returned. This will not
# raise an exception.
async def wait_all_completed(self) -> None:
running_tasks = [v.task for v in self._running_activities.values() if v.task]
if running_tasks:
await asyncio.gather(*running_tasks, return_exceptions=False)

def _cancel(
self, task_token: bytes, cancel: temporalio.bridge.proto.activity_task.Cancel
) -> None:
Expand Down
7 changes: 7 additions & 0 deletions temporalio/worker/_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,13 @@ async def raise_on_shutdown():
for task in tasks:
task.cancel()

# If there's an activity worker, we have to let all activity completions
# finish. We cannot guarantee that because poll shutdown completed
# (which means activities completed) that they got flushed to the
# server.
if self._activity_worker:
await self._activity_worker.wait_all_completed()

# Do final shutdown
try:
await self._bridge_worker.finalize_shutdown()
Expand Down