@@ -160,13 +160,6 @@ def has_probes(self) -> bool:
160
160
return bool (self .probes )
161
161
162
162
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
-
170
163
# Group probes on the basis of whether they create new context.
171
164
context_creators : List [Probe ] = []
172
165
context_consumers : List [Probe ] = []
@@ -175,39 +168,47 @@ def _open_signals(self) -> None:
175
168
176
169
signals : Deque [Signal ] = deque ()
177
170
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 ()
196
174
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
203
191
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 )
207
202
208
203
def _close_signals (self , retval = None , exc_info = (None , None , None )) -> None :
209
204
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
+
211
212
while signals :
212
213
# Open probe signals are ordered, with those that have created new
213
214
# tracing context first. We need to finalize them in reverse order,
0 commit comments