diff options
author | Xuehai Pan <XuehaiPan@outlook.com> | 2021-10-03 00:15:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-02 18:15:48 +0200 |
commit | 29214fc820cb2c8ac062da8e9f99f3459f10f93c (patch) | |
tree | 689e8e9d18ebc3350cb16fd6b0b3e3ae07c880ec | |
parent | 53a6c03e36db43d70b3b4e18abe7b81eaab235ce (diff) | |
download | psutil-29214fc820cb2c8ac062da8e9f99f3459f10f93c.tar.gz |
Fix thread safety of cached functions lock-freely (#1949)
Signed-off-by: XuehaiPan <XuehaiPan@pku.edu.cn>
-rw-r--r-- | psutil/_common.py | 7 |
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): |