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
24 changes: 20 additions & 4 deletions sentry_sdk/integrations/ray.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import functools
import sys

import sentry_sdk
Expand All @@ -17,7 +18,6 @@
import ray # type: ignore[import-not-found]
except ImportError:
raise DidNotEnable("Ray not installed.")
import functools

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -54,12 +54,13 @@ def new_remote(f=None, *args, **kwargs):

def wrapper(user_f):
# type: (Callable[..., Any]) -> Any
def new_func(*f_args, _tracing=None, **f_kwargs):
@functools.wraps(user_f)
def new_func(*f_args, _sentry_tracing=None, **f_kwargs):
# type: (Any, Optional[dict[str, Any]], Any) -> Any
_check_sentry_initialized()

transaction = sentry_sdk.continue_trace(
_tracing or {},
_sentry_tracing or {},
op=OP.QUEUE_TASK_RAY,
name=qualname_from_function(user_f),
origin=RayIntegration.origin,
Expand All @@ -78,6 +79,19 @@ def new_func(*f_args, _tracing=None, **f_kwargs):

return result

# Patching new_func signature to add the _sentry_tracing parameter to it
# Ray later inspects the signature and finds the unexpected parameter otherwise
signature = inspect.signature(new_func)
params = list(signature.parameters.values())
params.append(
inspect.Parameter(
"_sentry_tracing",
kind=inspect.Parameter.KEYWORD_ONLY,
default=None,
)
)
new_func.__signature__ = signature.replace(parameters=params) # type: ignore[attr-defined]

Comment on lines 84 to 94

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential bug: Adding the _tracing parameter without checking for its existence can cause a ValueError if the user's function already has a parameter with that name, crashing the application.
  • Description: The Ray integration modifies the signature of a user's function to add a _tracing parameter. However, it does not first check if a parameter with that name already exists. If a user decorates a function that already has a _tracing parameter, the code attempts to add a duplicate. This will cause inspect.signature.replace() to raise a ValueError because duplicate parameter names are not allowed. This error is unhandled and will crash the application when the function is decorated, preventing the application from starting.

  • Suggested fix: Before appending the _tracing parameter to the params list, iterate through the existing parameters to check if one with the name _tracing already exists. If it does, do not append the new parameter.
    severity: 0.7, confidence: 0.95

Did we get this right? 👍 / 👎 to inform future reviews.

if f:
rv = old_remote(new_func)
else:
Expand All @@ -99,7 +113,9 @@ def _remote_method_with_header_propagation(*args, **kwargs):
for k, v in sentry_sdk.get_current_scope().iter_trace_propagation_headers()
}
try:
result = old_remote_method(*args, **kwargs, _tracing=tracing)
result = old_remote_method(
*args, **kwargs, _sentry_tracing=tracing
)
span.set_status(SPANSTATUS.OK)
except Exception:
span.set_status(SPANSTATUS.INTERNAL_ERROR)
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/ray/test_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def example_task():
else:
example_task = ray.remote(example_task)

# Function name shouldn't be overwritten by Sentry wrapper
assert example_task._function_name == "tests.integrations.ray.test_ray.example_task"

with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
worker_envelopes = ray.get(example_task.remote())

Expand Down