Skip to content

[Python] Added registered methods support in AsyncIO stack #41796

Open
Zgoda91 wants to merge 23 commits into
grpc:masterfrom
Zgoda91:asyncio_registered_methods
Open

[Python] Added registered methods support in AsyncIO stack #41796
Zgoda91 wants to merge 23 commits into
grpc:masterfrom
Zgoda91:asyncio_registered_methods

Conversation

@Zgoda91

@Zgoda91 Zgoda91 commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Fixing #39800 and #39061

Work done:

  1. Added client-sided registered method support for AsyncIO
  2. Server-sided registered methods support for AsyncIO
  3. Extended AsyncIO observability test suite, verify metric collection with the registered methods feature enabled for all possible RPCs:
  • unary unary
  • unary stream
  • stream unary
  • stream stream

Should be merged after #41573 as this code is an extension for mentioned PR.

@Zgoda91 Zgoda91 force-pushed the asyncio_registered_methods branch from eb9a7bc to 6a188ca Compare March 4, 2026 17:24
@Zgoda91

Zgoda91 commented Mar 4, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds client-side registered method support for the gRPC Python AsyncIO stack, primarily for observability purposes. It also enables server-side observability for AsyncIO and extends the test suite to cover these new features. The changes are well-structured, introducing necessary modifications across the Cython and Python layers, and include comprehensive tests for both registered and unregistered methods. My feedback includes a suggestion to refactor duplicated code for better maintainability and a recommendation to reduce a long sleep in the tests to improve execution speed.

Comment thread src/python/grpcio/grpc/_cython/_cygrpc/observability.pyx.pxi Outdated
@Zgoda91 Zgoda91 changed the title [Python] Added registered methods support to AsyncIO stack [Python] Added registered methods support in AsyncIO stack Mar 5, 2026
@asheshvidyut

Copy link
Copy Markdown
Member

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds support for registered methods in the Python AsyncIO stack for observability. However, it introduces two significant Denial of Service (DoS) vulnerabilities related to NULL pointer dereferences: one in observability hooks where call tracers are dereferenced without NULL checks, and another regression in the AsyncIO server stack causing early unreffing of gRPC calls. Addressing these is crucial for server stability. Additionally, there are opportunities for minor refactoring to improve code maintainability by reducing duplication.

Comment thread src/python/grpcio/grpc/_cython/_cygrpc/observability.pyx.pxi Outdated
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/observability.pyx.pxi Outdated
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi
Comment thread src/python/grpcio/grpc/aio/_channel.py Outdated
@Zgoda91 Zgoda91 force-pushed the asyncio_registered_methods branch 2 times, most recently from 491ae84 to 4918c90 Compare March 12, 2026 17:46
@Zgoda91 Zgoda91 force-pushed the asyncio_registered_methods branch 2 times, most recently from f32034c to 0756a51 Compare March 19, 2026 01:04
@Zgoda91 Zgoda91 force-pushed the asyncio_registered_methods branch from 0756a51 to e34b3ef Compare March 19, 2026 01:07
@Zgoda91 Zgoda91 marked this pull request as ready for review March 19, 2026 09:11
@Zgoda91 Zgoda91 added release notes: yes Indicates if PR needs to be in release notes kokoro:run and removed bloat/none kokoro:run labels Mar 19, 2026

@sergiitk sergiitk 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.

f2f review comments

Comment thread src/python/grpcio/grpc/aio/_channel.py Outdated
Comment thread src/python/grpcio/grpc/aio/_channel.py
Comment thread src/python/grpcio/grpc/aio/_channel.py Outdated
@sergiitk sergiitk requested a review from asheshvidyut March 26, 2026 04:51

@asheshvidyut asheshvidyut 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.

Will it be possible to run Pyright over the changed files, to see if the Type Annotations are correct?

We are planning to add Pyright in CI soon - #41210

Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/channel.pyx.pxi Outdated
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/channel.pyx.pxi
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi Outdated
@asheshvidyut

Copy link
Copy Markdown
Member

Will review other files after we have consensus on the comments added above.

@sergiitk

Copy link
Copy Markdown
Member

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request implements support for registered methods in gRPC AsyncIO, enhancing performance for both client-side calls and server-side request handling. The implementation includes updates to the Cython layer for registered call APIs, a new method resolver for efficient dispatching, and comprehensive test coverage for interceptors and observability. Review feedback highlights a potential race condition in the registration of call handles and the risk of a KeyError when retrieving registered methods on the server, recommending the use of synchronization primitives and safer dictionary access.

Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/channel.pyx.pxi
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi Outdated
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi
Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi Outdated

@asheshvidyut asheshvidyut 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.

LGTM.

Comment thread src/python/grpcio/grpc/_cython/_cygrpc/aio/server.pyx.pxi Outdated

@asheshvidyut asheshvidyut 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.

LGTM

@Zgoda91

Zgoda91 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request implements support for registered method handlers in gRPC Asyncio Python, updating both client and server call paths to handle registered call handles and adding corresponding unit and observability tests. The review feedback highlights a critical bug in the server main loop where a failed request call future could permanently stop the server from listening to that method, and suggests making the test helper assert_eventually asynchronous to avoid blocking the event loop thread with time.sleep.

Comment on lines +1091 to +1097
try:
rpc_state = completed.result()
except:
# This slot failed because server is shutting down, so
# skip it (no limiter change) but keep serving the other
# calls in this batch
continue

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.

high

If completed.result() raises an exception while the server is still running (self._status == AIO_SERVER_STATUS_RUNNING), the except block will execute continue and skip the recreation of the request call future for this slot. This means that if a call request fails once (e.g., due to a transient error or internal error), the server will permanently stop listening for new calls on that specific registered method (or stop accepting generic calls entirely if it was the generic slot).

We should ensure that the request call future is recreated if the server is still running.

                    try:
                        rpc_state = completed.result()
                    except:
                        # This slot failed because server is shutting down, so
                        # skip it (no limiter change) but keep serving the other
                        # calls in this batch
                        if self._status == AIO_SERVER_STATUS_RUNNING:
                            new_fut = self._make_request_call_future(method_bytes)
                            pending_futures[new_fut] = method_bytes
                        continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lang/Python release notes: yes Indicates if PR needs to be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants