Skip to content

Commit 6007981

Browse files
committed
chore(di): ensure signals in wrapping context
We make sure that the signals queue is always added to the wrapping context when they signals context are opened.
1 parent af9098c commit 6007981

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)