diff --git a/Lib/linecache.py b/Lib/linecache.py index 4b97be3f05f8d8a..a8bef690ef86b81 100644 --- a/Lib/linecache.py +++ b/Lib/linecache.py @@ -63,10 +63,12 @@ def checkcache(filename=None): try: stat = os.stat(fullname) except os.error: - del cache[filename] + # Use pop instead of del because in a multithreaded case, this + # could have been deleted by another thread. + cache.pop(filename, None) continue if size != stat.st_size or mtime != stat.st_mtime: - del cache[filename] + cache.pop(filename, None) # likewise def updatecache(filename, module_globals=None): diff --git a/Misc/NEWS.d/next/Library/2019-02-27-15-00-22.bpo-25872.ItVO2B.rst b/Misc/NEWS.d/next/Library/2019-02-27-15-00-22.bpo-25872.ItVO2B.rst new file mode 100644 index 000000000000000..06a6bd579ba5e0e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-27-15-00-22.bpo-25872.ItVO2B.rst @@ -0,0 +1,3 @@ +Fixed a race condition in linecache.checkcache() that sometimes caused an +exception if two threads generated a traceback simultaneously. +Patch by Christopher Unkel.