Skip to content
Closed
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
23 changes: 20 additions & 3 deletions ddtrace/profiling/_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ def track_gevent_greenlet(greenlet: _Greenlet) -> _Greenlet:
stack_v2.track_greenlet(greenlet_id, greenlet.name or type(greenlet).__qualname__, frame) # type: ignore[attr-defined]
except AttributeError as e:
raise GreenletTrackingError("Cannot track greenlet with no name attribute") from e
except Exception as e:
raise GreenletTrackingError("Cannot track greenlet") from e

# Untrack on completion
try:
greenlet.rawlink(untrack_greenlet)
except AttributeError:
# This greenlet cannot be linked (e.g. the Hub)
pass
except Exception as e:
raise GreenletTrackingError("Cannot link greenlet for untracking") from e

_greenlet_frames[greenlet_id] = frame

Expand Down Expand Up @@ -120,11 +124,20 @@ def link_greenlets(greenlet_id: int, parent_id: int) -> None:
class Greenlet(_Greenlet):
@classmethod
def spawn(cls, *args: t.Any, **kwargs: t.Any) -> _Greenlet:
return track_gevent_greenlet(super().spawn(*args, **kwargs))
greenlet = super().spawn(*args, **kwargs)
try:
return track_gevent_greenlet(greenlet)
except GreenletTrackingError:
# If we cannot track the greenlet, we just return it as is.
return greenlet

@classmethod
def spawn_later(cls, *args: t.Any, **kwargs: t.Any) -> _Greenlet:
return track_gevent_greenlet(super().spawn_later(*args, **kwargs))
greenlet = super().spawn_later(*args, **kwargs)
try:
return track_gevent_greenlet(greenlet)
except GreenletTrackingError:
return greenlet

def join(self, *args: t.Any, **kwargs: t.Any) -> None:
target_id = thread.get_ident(self)
Expand All @@ -137,7 +150,11 @@ def join(self, *args: t.Any, **kwargs: t.Any) -> None:

def wrap_spawn(original: t.Callable[..., _Greenlet]) -> t.Callable[..., _Greenlet]:
def _(*args: t.Any, **kwargs: t.Any) -> _Greenlet:
return track_gevent_greenlet(original(*args, **kwargs))
greenlet = original(*args, **kwargs)
try:
return track_gevent_greenlet(greenlet)
except GreenletTrackingError:
return greenlet

return _

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
profiling: Fix an issue with greenlet support that could cause greenlet
spawning to fail in some rare cases.
Loading