Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions workers/proxy_worker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,29 @@ def get_current_invocation_id() -> Optional[Any]:
if thread_invocation_id is not None:
return thread_invocation_id

# Customer threads follow the documented pattern of assigning
# ``context.thread_local_storage.invocation_id``. That object is the
# runtime's own threading.local, so consult it as well.
runtime_local = _get_runtime_invocation_id_local()
if runtime_local is not None:
runtime_invocation_id = getattr(runtime_local, 'invocation_id', None)
if runtime_invocation_id is not None:
return runtime_invocation_id

return getattr(_invocation_id_local, 'invocation_id', None)


def _get_runtime_invocation_id_local() -> Optional[threading.local]:
if not _library_worker:
return None
try:
context_module = sys.modules.get(
f"{_library_worker.__name__}.bindings.context")
return getattr(context_module, '_invocation_id_local', None)
except AttributeError:
return None


class AsyncLoggingHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
# Since we disable console log after gRPC channel is initiated,
Expand Down
20 changes: 20 additions & 0 deletions workers/tests/unittest_proxy/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,26 @@ def test_get_current_invocation_id_fallback_to_thread(self):
result = get_current_invocation_id()
self.assertEqual(result, thread_id_value)

def test_get_current_invocation_id_runtime_thread_local_storage(self):
"""Documented context.thread_local_storage assignment in a customer
thread (e.g. asyncio.to_thread) must be honoured."""
runtime_pkg = types.ModuleType("fake_runtime")
runtime_context = types.ModuleType("fake_runtime.bindings.context")
runtime_context._invocation_id_local = threading.local()
dispatcher_module._library_worker = runtime_pkg
dispatcher_module._library_worker_has_cv = False

def work():
runtime_context._invocation_id_local.invocation_id = "tls-1860"
return get_current_invocation_id()

async def run():
return await asyncio.to_thread(work)

with patch.dict(sys.modules,
{"fake_runtime.bindings.context": runtime_context}):
self.assertEqual(asyncio.run(run()), "tls-1860")

@patch('proxy_worker.dispatcher.asyncio._get_running_loop')
@patch('proxy_worker.dispatcher.asyncio.current_task')
def test_get_current_invocation_id_asyncio_task_context(
Expand Down