Only trigger events on thread calling start_trace · python/cpython@99ea70c

Original file line numberDiff line numberDiff line change

@@ -2,6 +2,7 @@

22
33

import fnmatch

44

import sys

5+

import threading

56

import os

67

from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR

78

@@ -36,9 +37,11 @@ def __init__(self):

3637

self._name = 'bdbtracer'

3738

self._tracefunc = None

3839

self._disable_current_event = False

40+

self._tracing_thread = None

3941
4042

def start_trace(self, tracefunc):

4143

self._tracefunc = tracefunc

44+

self._tracing_thread = threading.current_thread()

4245

curr_tool = sys.monitoring.get_tool(self._tool_id)

4346

if curr_tool is None:

4447

sys.monitoring.use_tool_id(self._tool_id, self._name)

@@ -57,6 +60,7 @@ def start_trace(self, tracefunc):

5760

sys.monitoring.set_events(self._tool_id, all_events)

5861
5962

def stop_trace(self):

63+

self._tracing_thread = None

6064

curr_tool = sys.monitoring.get_tool(self._tool_id)

6165

if curr_tool != self._name:

6266

return

@@ -78,6 +82,8 @@ def callback_wrapper(func):

7882
7983

@functools.wraps(func)

8084

def wrapper(self, *args):

85+

if self._tracing_thread != threading.current_thread():

86+

return

8187

try:

8288

frame = sys._getframe().f_back

8389

ret = func(self, frame, *args)