From 45240f64e140be630f2c620a3d65989a88ed6e32 Mon Sep 17 00:00:00 2001 From: Michael Graczyk Date: Wed, 13 May 2020 17:41:57 -0500 Subject: [PATCH 1/2] [3.8] bpo-25872: Fix KeyError using linecache from multiple threads (GH-18007) The crash that this fixes occurs when using traceback and other modules from multiple threads; del cache[filename] can raise a KeyError. (cherry picked from commit d72ea605218bbee6ae46648997d9bb76d0fba460) Co-authored-by: Michael Graczyk --- Lib/linecache.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/linecache.py b/Lib/linecache.py index 3afcce1f0a1456..c87e1807bfafaa 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -73,10 +73,10 @@ def checkcache(filename=None): try: stat = os.stat(fullname) except OSError: - del cache[filename] + cache.pop(filename, None) continue if size != stat.st_size or mtime != stat.st_mtime: - del cache[filename] + cache.pop(filename, None) def updatecache(filename, module_globals=None): @@ -86,7 +86,7 @@ def updatecache(filename, module_globals=None): if filename in cache: if len(cache[filename]) != 1: - del cache[filename] + cache.pop(filename, None) if not filename or (filename.startswith('<') and filename.endswith('>')): return [] From 91eeeecdfb7980bd528089869a6af4bf49fbfae6 Mon Sep 17 00:00:00 2001 From: Andrew Kuchling Date: Thu, 14 May 2020 13:47:37 -0400 Subject: [PATCH 2/2] [3.8] bpo-25872: Add NEWS.d item --- .../next/Library/2020-05-14-13-25-36.bpo-25872.5D5538.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-05-14-13-25-36.bpo-25872.5D5538.rst diff --git a/Misc/NEWS.d/next/Library/2020-05-14-13-25-36.bpo-25872.5D5538.rst b/Misc/NEWS.d/next/Library/2020-05-14-13-25-36.bpo-25872.5D5538.rst new file mode 100644 index 00000000000000..3fd8bac73edbe6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-14-13-25-36.bpo-25872.5D5538.rst @@ -0,0 +1,2 @@ +:mod:`linecache` could crash with a :exc:`KeyError` when accessed from multiple threads. +Fix by Michael Graczyk.