Defer async client channel creation until first use - #226
Open
andystaples wants to merge 3 commits into
Open
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7bc5f597-1596-460d-bfe6-738676344ef0
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a Python 3.13 compatibility issue where constructing AsyncTaskHubGrpcClient (and dependents like DurableFunctionsClient) could fail with RuntimeError: There is no current event loop in thread 'MainThread' by deferring grpc.aio channel + stub creation until the first async RPC.
Changes:
- Deferred SDK-owned async gRPC channel/stub creation in
AsyncTaskHubGrpcClientuntil first RPC via a new lazy_get_stub()helper, and madeclose()safe when the client was never used. - Updated async-client construction tests to create the channel under an active event loop and added a regression test covering “construct with no current loop, then use under
asyncio.run()”. - Documented the user-visible fix in the core SDK and Azure Functions provider changelogs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
durabletask/client.py |
Lazily creates the SDK-owned grpc.aio channel/stub on first RPC and guards close/recreate paths when the channel was never created. |
tests/durabletask/test_client.py |
Adjusts construction tests for lazy initialization and adds a regression test for “no current event loop” construction. |
CHANGELOG.md |
Adds an Unreleased FIXED entry for the core SDK describing the construction/runtime loop fix. |
azure-functions-durable/CHANGELOG.md |
Adds an Unreleased FIXED entry for the Functions durable client construction failure after a loop was closed/cleared. |
berndverst
reviewed
Jul 29, 2026
berndverst
left a comment
Member
There was a problem hiding this comment.
One low-severity documentation requirement remains; the lazy async-channel implementation itself looks sound.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7bc5f597-1596-460d-bfe6-738676344ef0
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7bc5f597-1596-460d-bfe6-738676344ef0
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
tests/durabletask/test_client.py:229
install_resilient_test_stubs()now forces async clients to create a stub by callingclient._get_stub(). If a recreate task runs afterclient.close()has set_closing=True(e.g., in close/recreate race tests),_get_stub()raisesRuntimeError("The client is closed"), which then gets logged as a recreate failure. Guarding this helper against_closingavoids spurious exceptions/log noise during shutdown while still ensuring the stub exists for normal resiliency tests.
def wrap_if_needed():
if is_async:
client._get_stub()
if not isinstance(client._stub, wrapper_cls):
client._stub = wrapper_cls(client._stub, client._resiliency_interceptor)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a Python 3.13 failure where constructing
AsyncTaskHubGrpcClientorDurableFunctionsClientcould raise:This surfaced in the Azure Functions CI for #220 after an async test ran between two synchronous durable-client construction tests.
Root cause
AsyncTaskHubGrpcClienteagerly created itsgrpc.aiochannel in__init__. On Python 3.13,pytest-asyncioclears the current event loop after an async test. The next synchronous client construction therefore asked gRPC to bind a channel when no current loop existed.The same sequence is possible outside tests when an async client is constructed after
asyncio.run()or otherwise outside the event loop that will perform its RPCs. Creating a replacement default loop would only hide the exception and could bind the channel to the wrong loop.Fix
grpc.aiochannel and stub creation until the first RPC.asyncio.run().Validation