Skip to content

Close worker background event loops - #256

Merged
andystaples merged 1 commit into
mainfrom
andystaples-fix-worker-event-loop-leak
Aug 13, 2026
Merged

Close worker background event loops#256
andystaples merged 1 commit into
mainfrom
andystaples-fix-worker-event-loop-leak

Conversation

@andystaples

Copy link
Copy Markdown
Contributor

Why this is needed

TaskHubGrpcWorker.start() creates an asyncio event loop on its background thread but never closes it. Stopped workers therefore retain loop resources until cyclic garbage collection, which can emit a delayed ResourceWarning: unclosed event loop during unrelated code.

This surfaced as the intermittent Python 3.11 failure in PR #252 when garbage collection ran inside a warning-capture block.

Changes

  • use asyncio.run() to own and close the worker thread's event loop
  • add deterministic regression coverage that retains the loop and verifies it is closed after the thread exits
  • document the resource leak fix in the core changelog

Validation

  • python -m pytest tests\durabletask\test_worker_resiliency.py tests\durabletask\test_worker_concurrency_loop.py tests\durabletask\test_worker_concurrency_loop_async.py --quiet
  • python -m nox -s core_tests-3.11 -- tests\durabletask\test_worker_resiliency.py -k worker_start --quiet
  • python -m flake8 durabletask
  • python -m flake8 tests\durabletask

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: adb27bb3-082b-4553-9941-7672749d7424
Copilot AI lite review requested due to automatic review settings August 10, 2026 18:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a resource-leak issue in the core SDK worker implementation by ensuring the background-thread asyncio event loop is properly closed, preventing delayed ResourceWarning: unclosed event loop emissions after worker shutdown.

Changes:

  • Switch the worker thread loop ownership to asyncio.run() so the event loop is created and deterministically closed when the worker thread exits.
  • Add a regression test that captures the worker thread’s event loop and asserts it is closed after the thread completes.
  • Document the fix in the core CHANGELOG.md under FIXED.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
durabletask/worker.py Uses asyncio.run() in the worker background thread so the event loop is closed on thread exit.
tests/durabletask/test_worker_resiliency.py Adds a regression test to verify the background event loop is closed after the worker thread exits.
CHANGELOG.md Adds a user-facing changelog entry describing the event loop resource leak fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +191 to +195
worker.start()
worker._runLoop.join(timeout=1.0)

assert len(event_loops) == 1
assert event_loops[0].is_closed() is True

@berndverst Bernd Verst (berndverst) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the event-loop lifecycle change, surrounding worker restart/stop behavior, regression coverage, compatibility, performance, changelog, and validation results. The change is correct, non-breaking, and appropriately closes worker-owned event-loop resources.

@andystaples
andystaples merged commit 3d5f912 into main Aug 13, 2026
28 of 30 checks passed
@andystaples
andystaples deleted the andystaples-fix-worker-event-loop-leak branch August 13, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants