Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Fixes grpc#26322

* Support runtimes without contextvars

* Hoist member declaration higher
  • Loading branch information
gnossen authored May 22, 2021
1 parent 9dabaec commit c0f3464
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ cdef int _get_metadata(void *state,
with nogil:
cb(user_data, NULL, 0, status, c_error_details)
args = context.service_url, context.method_name, callback,
_spawn_callback_async(<object>state, args)
plugin = <object>state
if plugin._stored_ctx is not None:
plugin._stored_ctx.run(_spawn_callback_async, plugin, args)
else:
_spawn_callback_async(<object>state, args)
return 0 # Asynchronous return


Expand Down
11 changes: 11 additions & 0 deletions src/python/grpcio/grpc/_plugin_wrapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ class _Plugin(object):

def __init__(self, metadata_plugin):
self._metadata_plugin = metadata_plugin
self._stored_ctx = None

try:
import contextvars # pylint: disable=wrong-import-position
# The plugin may be invoked on a thread created by Core, which will not
# have the context propagated. This context is stored and installed in
# the thread invoking the plugin.
self._stored_ctx = contextvars.copy_context()
except ImportError:
# Support versions predating contextvars.
pass

def __call__(self, service_url, method_name, callback):
context = _AuthMetadataContext(_common.decode(service_url),
Expand Down

0 comments on commit c0f3464

Please sign in to comment.