Skip to content
Merged
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
48 changes: 48 additions & 0 deletions packages/runtime-sdk/src/_workers_sdk_entropy_import_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sys
from contextlib import contextmanager
from importlib.metadata import PackageNotFoundError, version

from _cloudflare.allow_entropy import (
allow_bad_entropy_calls,
Expand All @@ -17,6 +18,29 @@
)


@contextmanager
def allow_bad_entropy_calls_for_version(
package_name: str,
num_calls: int,
min_version: tuple[int, ...],
max_version: tuple[int, ...] | None = None,
):
try:
package_version = tuple(map(int, version(package_name).split(".")))
except PackageNotFoundError:
yield
return

if package_version < min_version or (
max_version is not None and package_version > max_version
):
yield
return

with allow_bad_entropy_calls(num_calls):
yield


class STATE:
imported_rust_package = False
numpy_random = None
Expand Down Expand Up @@ -180,6 +204,30 @@ def langchain_openai_chat_models_base_context(module):
yield


@register_exec_patch("opentelemetry.context")
@contextmanager
def opentelemetry_context(module):
# OpenTelemetry creates three UUID-backed keys.
with allow_bad_entropy_calls_for_version("opentelemetry-api", 3, (1, 40, 0)):
yield


@register_exec_patch("opentelemetry.trace.propagation")
@contextmanager
def opentelemetry_trace_propagation_context(module):
# OpenTelemetry creates a UUID-backed key.
with allow_bad_entropy_calls(1):
yield


@register_exec_patch("opentelemetry.baggage")
@contextmanager
def opentelemetry_baggage_context(module):
# OpenTelemetry creates a UUID-backed key.
with allow_bad_entropy_calls(1):
yield
Comment thread
ryanking13 marked this conversation as resolved.


@register_exec_patch("litestar.openapi.controller")
@register_exec_patch("litestar.constants")
@contextmanager
Expand Down
5 changes: 0 additions & 5 deletions packages/runtime-sdk/tests/test_in_workerd.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ def test_in_workerd( # noqa: PLR0913, PLR0917 (too-many-arguments)
"wsgi requires pyodide.ffi.run_sync (JSPI), unavailable before 2026-01-01"
)

if test_dir.name == "entropy-patches" and python_version == "3.14":
pytest.skip(
"TODO: enable me after https://github.com/cloudflare/workerd/pull/7200 lands in wrangler"
)

if test_dir.name == "http-client" and python_version < "3.14":
pytest.skip("HTTP client compatibility tests require Python 3.14 or newer")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ dependencies = [
# "langchain-openai",
# "openai",
"litestar",
"opentelemetry-api",
"mcp; python_version >= '3.14'",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ruff: noqa: F401
import sys

if sys.version_info >= (3, 14):
import mcp


def test_import():
# make sure this file is collected by pytest
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ruff: noqa: F401
import opentelemetry.context
Comment thread
ryanking13 marked this conversation as resolved.
import opentelemetry.trace
Comment thread
ryanking13 marked this conversation as resolved.


def test_import():
# make sure this file is collected by pytest
pass
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
test_aiohttp_websocket,
test_langsmith,
test_litestar,
test_mcp,
test_numpy,
test_opentelemetry,
test_pydantic,
test_rust_packages,
test_ssl_avoidance,
Expand Down
Loading