multithreading traceback KeyError when modifying file #70060
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = None closed_at = <Date 2021-05-18.14:27:03.157> created_at = <Date 2015-12-15.17:21:04.333> labels = ['easy', 'type-bug', 'library', '3.9'] title = 'multithreading traceback KeyError when modifying file' updated_at = <Date 2021-05-18.14:27:03.156> user = 'https://bugs.python.org/MichaelAllen'
activity = <Date 2021-05-18.14:27:03.156> actor = 'iritkatriel' assignee = 'none' closed = True closed_date = <Date 2021-05-18.14:27:03.157> closer = 'iritkatriel' components = ['Library (Lib)'] creation = <Date 2015-12-15.17:21:04.333> creator = 'Michael Allen' dependencies =  files =  hgrepos =  issue_num = 25872 keywords = ['patch', 'easy'] message_count = 12.0 messages = ['256469', '323180', '359390', '359392', '359427', '370301', '370302', '381409', '393187', '393872', '393874', '393875'] nosy_count = 10.0 nosy_names = ['akuchling', 'python-dev', 'Michael Allen', 'cheryl.sabella', 'miss-islington', 'xtreak', 'Michael Graczyk', 'rahul-kumi', 'iritkatriel', 'uniocto'] pr_nums = ['12061', '17360', '18007', '20079', '20092', '20511', '25913', '26208', '26211', '26212'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue25872' versions = ['Python 3.9']
The text was updated successfully, but these errors were encountered:
Modifying a file while getting a stacktrace across multiple threads causes linecache's cache to bust and del to be called on the global cache variable. This is not thread safe and raises a KeyError.
import threading import traceback def main(): with open(__file__, 'a') as fp: fp.write(' ') traceback.format_stack() threads = [ threading.Thread(target=main) for i in range(100) ] map(lambda t: t.start(), threads) map(lambda t: t.join(), threads)
I see the following error,
Exception in thread Thread-56: Traceback (most recent call last): File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) File "test.py", line 7, in main traceback.format_stack() File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/traceback.py", line 279, in format_stack return format_list(extract_stack(f, limit)) File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/traceback.py", line 305, in extract_stack linecache.checkcache(filename) File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/linecache.py", line 69, in checkcache del cache[filename] KeyError: 'test.py'
Possible solution is to ignore KeyError on del cache[filename].
This issue still exists in Python 3. The repro just needs to be changed so that the threads are actually started.
- map(lambda t: t.start(), threads) - map(lambda t: t.join(), threads) + [t.start() for t in threads] + [t.join() for t in threads]
My fix is linked.