Skip to content

Commit 03a30e9

Browse files
P403n1x87Yun-Kimncybulnsrip-ddwconti27
authored andcommitted
chore(di): ensure signals in wrapping context (#12239)
We make sure that the signals queue is always added to the wrapping context when they signals context are opened. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [ ] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --------- Co-authored-by: Yun Kim <[email protected]> Co-authored-by: Nicole Cybul <[email protected]> Co-authored-by: Nick Ripley <[email protected]> Co-authored-by: William Conti <[email protected]> Co-authored-by: Christophe Papazian <[email protected]> Co-authored-by: Munir Abdinur <[email protected]> Co-authored-by: Laplie Anderson <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 119a3ab commit 03a30e9

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

ddtrace/debugging/_debugger.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,6 @@ def has_probes(self) -> bool:
160160
return bool(self.probes)
161161

162162
def _open_signals(self) -> None:
163-
frame = self.__frame__
164-
assert frame is not None # nosec
165-
166-
thread = threading.current_thread()
167-
168-
signal: Optional[Signal] = None
169-
170163
# Group probes on the basis of whether they create new context.
171164
context_creators: List[Probe] = []
172165
context_consumers: List[Probe] = []
@@ -175,39 +168,47 @@ def _open_signals(self) -> None:
175168

176169
signals: Deque[Signal] = deque()
177170

178-
# Trigger the context creators first, so that the new context can be
179-
# consumed by the consumers.
180-
for probe in chain(context_creators, context_consumers):
181-
# Because new context might be created, we need to recompute it
182-
# for each probe.
183-
trace_context = self._tracer.current_trace_context()
184-
185-
try:
186-
signal = Signal.from_probe(
187-
probe,
188-
frame=frame,
189-
thread=thread,
190-
trace_context=trace_context,
191-
meter=self._probe_meter,
192-
)
193-
except TypeError:
194-
log.error("Unsupported probe type: %s", type(probe))
195-
continue
171+
try:
172+
frame = self.__frame__
173+
thread = threading.current_thread()
196174

197-
try:
198-
signal.do_enter()
199-
except Exception:
200-
log.exception("Failed to enter signal %r", signal)
201-
continue
202-
signals.append(signal)
175+
# Trigger the context creators first, so that the new context can be
176+
# consumed by the consumers.
177+
for probe in chain(context_creators, context_consumers):
178+
try:
179+
signal = Signal.from_probe(
180+
probe,
181+
frame=frame,
182+
thread=thread,
183+
# Because new context might be created, we need to
184+
# recompute it for each probe.
185+
trace_context=self._tracer.current_trace_context(),
186+
meter=self._probe_meter,
187+
)
188+
except TypeError:
189+
log.error("Unsupported probe type: %s", type(probe))
190+
continue
203191

204-
# Save state on the wrapping context
205-
self.set("start_time", time.monotonic_ns())
206-
self.set("signals", signals)
192+
try:
193+
signal.do_enter()
194+
except Exception:
195+
log.exception("Failed to enter signal %r", signal)
196+
continue
197+
signals.append(signal)
198+
finally:
199+
# Save state on the wrapping context
200+
self.set("start_time", time.monotonic_ns())
201+
self.set("signals", signals)
207202

208203
def _close_signals(self, retval=None, exc_info=(None, None, None)) -> None:
209204
end_time = time.monotonic_ns()
210-
signals = cast(Deque[Signal], self.get("signals"))
205+
206+
try:
207+
signals = cast(Deque[Signal], self.get("signals"))
208+
except KeyError:
209+
log.error("Signal contexts were not opened for function probe over %s", self.__wrapped__)
210+
return
211+
211212
while signals:
212213
# Open probe signals are ordered, with those that have created new
213214
# tracing context first. We need to finalize them in reverse order,

0 commit comments

Comments
 (0)