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

Make rpc calls using an asyncio task context MOD-3632 #2178

Merged
merged 31 commits into from
Sep 19, 2024

Conversation

freider
Copy link
Contributor

@freider freider commented Sep 2, 2024

Wrap all grpc api calls in asyncio.tasks that are cancelled when the Client is closed, and raise a custom error when that happens + for future calls on an already closed Client.

This allows the client to shut down more gracefully by stopping rpc calls prior to unwinding the synchronicity event loop, instead of triggering a generic StreamTerminatedError or similar that we don't know wether to retry or not.

@freider freider changed the title Make rpc calls using an asyncio task context Make rpc calls using an asyncio task context MOD-3632 Sep 2, 2024
@erikbern
Copy link
Contributor

erikbern commented Sep 2, 2024

Instead of translating the grpc stubs dynamically, we might want to generate our own stub code using a plugin. It's pretty sparsely documented but should be doable: https://medium.com/cloud-native-daily/create-and-run-a-protobuf-plugin-9471e16e8ad

Might be a bit cleaner in terms of tracebacks etc, and could let us do some other things too (i.e. enable retries and handle grpc exceptions more cleanly)

@freider
Copy link
Contributor Author

freider commented Sep 3, 2024

Instead of translating the grpc stubs dynamically, we might want to generate our own stub code using a plugin. It's pretty sparsely documented but should be doable: https://medium.com/cloud-native-daily/create-and-run-a-protobuf-plugin-9471e16e8ad

Might be a bit cleaner in terms of tracebacks etc, and could let us do some other things too (i.e. enable retries and handle grpc exceptions more cleanly)

Do you mean like a fork of grpclib's stub generator (https://github.com/vmagamedov/grpclib/blob/master/grpclib/plugin/main.py) or more like an additional generated stub that references the original? Feels like it would be nice to avoid forking/patching, so we can take advantage of potential improvements in grpclib itself, so maybe the latter?

@erikbern
Copy link
Contributor

erikbern commented Sep 5, 2024

Do you mean like a fork of grpclib's stub generator (https://github.com/vmagamedov/grpclib/blob/master/grpclib/plugin/main.py) or more like an additional generated stub that references the original? Feels like it would be nice to avoid forking/patching, so we can take advantage of potential improvements in grpclib itself, so maybe the latter?

yeah no forking right? just an extra plugin

@freider
Copy link
Contributor Author

freider commented Sep 5, 2024

Do you mean like a fork of grpclib's stub generator (https://github.com/vmagamedov/grpclib/blob/master/grpclib/plugin/main.py) or more like an additional generated stub that references the original? Feels like it would be nice to avoid forking/patching, so we can take advantage of potential improvements in grpclib itself, so maybe the latter?

yeah no forking right? just an extra plugin

Yeah exactly - that's what I ended up with, just wanted to clarify that it matched your idea :)

@@ -145,16 +150,6 @@ async def _close(self, prep_for_restore: bool = False):
# Remove cached client.
self.set_env_client(None)

def set_pre_stop(self, pre_stop: Callable[[], Awaitable[None]]):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This appears to not be used anymore 🕺

@freider freider requested review from mwaskom and erikbern September 12, 2024 12:46
_MetadataLike = Union[Mapping[str, _Value], Collection[Tuple[str, _Value]]]


class UnaryUnaryWrapper(Generic[RequestType, ResponseType]):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving these to the modal.client module since they are now tied to a client instance

@freider freider removed the request for review from erikbern September 19, 2024 09:20
@freider
Copy link
Contributor Author

freider commented Sep 19, 2024

@prbot approve

Copy link

@modal-pr-review-automation modal-pr-review-automation bot left a comment

Choose a reason for hiding this comment

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

Approved 👍. @mwaskom will follow-up review this.

@freider freider merged commit 0a17fa3 into main Sep 19, 2024
22 checks passed
@freider freider deleted the freider/client-close-stops-rpcs branch September 19, 2024 09:25
irfansharif added a commit that referenced this pull request Sep 24, 2024
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