From 46bb3769a9344d294270a05f3ee898f4900f76b4 Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Wed, 18 Mar 2020 10:13:05 -0700 Subject: [PATCH] Override the auth plugin behavior --- .../grpc/_cython/_cygrpc/aio/iomgr/iomgr.pyx.pxi | 11 +++++++++++ .../grpc/_cython/_cygrpc/credentials.pyx.pxi | 14 ++++++++------ .../tests_aio/unit/secure_call_test.py | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/aio/iomgr/iomgr.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/aio/iomgr/iomgr.pyx.pxi index ac62c41e0f2..f5b62af5287 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/aio/iomgr/iomgr.pyx.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/aio/iomgr/iomgr.pyx.pxi @@ -212,7 +212,18 @@ cdef void asyncio_run_loop(size_t timeout_ms) with gil: pass +def _auth_plugin_callback_wrapper(object cb, + str service_url, + str method_name, + object callback): + asyncio.get_event_loop().call_soon(cb, service_url, method_name, callback) + + def install_asyncio_iomgr(): + # Auth plugins invoke user provided logic in another thread by default. We + # need to override that behavior by registering the call to the event loop. + set_async_callback_func(_auth_plugin_callback_wrapper) + asyncio_resolver_vtable.resolve = asyncio_resolve asyncio_resolver_vtable.resolve_async = asyncio_resolve_async diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi index c736b7a10c5..24d1e2a3b77 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi @@ -34,12 +34,14 @@ cdef class CallCredentials: raise NotImplementedError() -cdef int _get_metadata( - void *state, grpc_auth_metadata_context context, - grpc_credentials_plugin_metadata_cb cb, void *user_data, - grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], - size_t *num_creds_md, grpc_status_code *status, - const char **error_details) except * with gil: +cdef int _get_metadata(void *state, + grpc_auth_metadata_context context, + grpc_credentials_plugin_metadata_cb cb, + void *user_data, + grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], + size_t *num_creds_md, + grpc_status_code *status, + const char **error_details) except * with gil: cdef size_t metadata_count cdef grpc_metadata *c_metadata def callback(metadata, grpc_status_code status, bytes error_details): diff --git a/src/python/grpcio_tests/tests_aio/unit/secure_call_test.py b/src/python/grpcio_tests/tests_aio/unit/secure_call_test.py index e6b69331d24..7efaddd607e 100644 --- a/src/python/grpcio_tests/tests_aio/unit/secure_call_test.py +++ b/src/python/grpcio_tests/tests_aio/unit/secure_call_test.py @@ -126,5 +126,5 @@ class TestStreamStreamSecureCall(_SecureCallMixin, AioTestBase): if __name__ == '__main__': - logging.basicConfig() + logging.basicConfig(level=logging.DEBUG) unittest.main(verbosity=2)