[sdk/python] Fix mocks issue when passing a resource more than once #6479
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.
This change avoids
RuntimeError: There is no current event loop in thread '<thread_name>'
errors when passing a resource as an input multiple times when using mocks.The problem is that when using mocks, we deserialize the gRPC inputs before passing them to the user's mock methods. Deserializing inputs doesn't typically require an event loop, however, during deserialization of resource references, we end up creating some instances of
Future
, which does require an event loop to be present for the current thread. If this is done multiple times for a resource, it's possible thatdeserialize_properties
will be called on an asyncio thread that doesn't yet have an event loop, resulting in the error being raised.The error does not occur when only passing the resource reference once because typically the thread (e.g.
asyncio_0
) used in that case will have already had an event loop created for it due to the use of the internal_syncawait
when serializing the source resource's properties, which ensures an event loop is set for the thread.The fix is to ensure an event loop is created for the thread in the mocks implementation before calling
deserialize_properties
.Fixes #6465