Skip to content
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

[sdk/python] Fix mocks issue when passing a resource more than once #6479

Merged
merged 4 commits into from Mar 9, 2021

Conversation

justinvp
Copy link
Member

@justinvp justinvp commented Mar 8, 2021

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 that deserialize_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

@justinvp justinvp requested a review from komalali March 8, 2021 15:42
CHANGELOG_PENDING.md Outdated Show resolved Hide resolved
sdk/python/lib/pulumi/runtime/sync_await.py Outdated Show resolved Hide resolved
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 that `deserialize_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`.
@justinvp justinvp merged commit 0b1414d into master Mar 9, 2021
@pulumi-bot pulumi-bot deleted the justin/pymocks branch March 9, 2021 21:48
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.

Unit test with Mock fails
2 participants