Skip to content

Commit 04a2027

Browse files
mitsuhikomattrobenolt
authored andcommitted
Merge pull request #3191 from getsentry/bugfix/locked-context-patching
Use a thread lock to patch contexts.
1 parent 60648b5 commit 04a2027

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
from __future__ import absolute_import
22

3+
from threading import Lock
34
from sentry.utils.imports import import_string
45

56

67
class PatchContext(object):
78
def __init__(self, target, callback):
89
target, attr = target.rsplit('.', 1)
910
target = import_string(target)
10-
self.func = getattr(target, attr)
1111
self.target = target
1212
self.attr = attr
1313
self.callback = callback
14+
self._lock = Lock()
15+
with self._lock:
16+
self.func = getattr(target, attr)
1417

1518
def __enter__(self):
1619
self.patch()
@@ -20,19 +23,21 @@ def __exit__(self, exc_type, exc_value, traceback):
2023
self.unpatch()
2124

2225
def patch(self):
23-
func = getattr(self.target, self.attr)
26+
with self._lock:
27+
func = getattr(self.target, self.attr)
2428

25-
def wrapped(*args, **kwargs):
26-
__traceback_hide__ = True # NOQA
27-
return self.callback(self.func, *args, **kwargs)
29+
def wrapped(*args, **kwargs):
30+
__traceback_hide__ = True # NOQA
31+
return self.callback(self.func, *args, **kwargs)
2832

29-
wrapped.__name__ = func.__name__
30-
if hasattr(func, '__doc__'):
31-
wrapped.__doc__ = func.__doc__
32-
if hasattr(func, '__module__'):
33-
wrapped.__module__ = func.__module__
33+
wrapped.__name__ = func.__name__
34+
if hasattr(func, '__doc__'):
35+
wrapped.__doc__ = func.__doc__
36+
if hasattr(func, '__module__'):
37+
wrapped.__module__ = func.__module__
3438

35-
setattr(self.target, self.attr, wrapped)
39+
setattr(self.target, self.attr, wrapped)
3640

3741
def unpatch(self):
38-
setattr(self.target, self.attr, self.func)
42+
with self._lock:
43+
setattr(self.target, self.attr, self.func)

0 commit comments

Comments
 (0)