summaryrefslogtreecommitdiff
path: root/psutil/_common.py
diff options
context:
space:
mode:
authorXuehai Pan <XuehaiPan@outlook.com>2021-10-03 00:15:48 +0800
committerGitHub <noreply@github.com>2021-10-02 18:15:48 +0200
commit29214fc820cb2c8ac062da8e9f99f3459f10f93c (patch)
tree689e8e9d18ebc3350cb16fd6b0b3e3ae07c880ec /psutil/_common.py
parent53a6c03e36db43d70b3b4e18abe7b81eaab235ce (diff)
downloadpsutil-29214fc820cb2c8ac062da8e9f99f3459f10f93c.tar.gz
Fix thread safety of cached functions lock-freely (#1949)
Signed-off-by: XuehaiPan <XuehaiPan@pku.edu.cn>
Diffstat (limited to 'psutil/_common.py')
-rw-r--r--psutil/_common.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/psutil/_common.py b/psutil/_common.py
index 771461d6..d5548d1c 100644
--- a/psutil/_common.py
+++ b/psutil/_common.py
@@ -440,6 +440,7 @@ def memoize_when_activated(fun):
>>> foo()
>>>
"""
+
@functools.wraps(fun)
def wrapper(self):
try:
@@ -451,7 +452,11 @@ def memoize_when_activated(fun):
except KeyError:
# case 3: we entered oneshot() ctx but there's no cache
# for this entry yet
- ret = self._cache[fun] = fun(self)
+ ret = fun(self)
+ try:
+ self._cache[fun] = ret
+ except AttributeError:
+ pass # inconsistency caused by multi-threading, just ignore it
return ret
def cache_activate(proc):