Title: multithreading traceback KeyError when modifying file
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Michael Allen, xtreak
Priority: normal Keywords:

Created on 2015-12-15 17:21 by Michael Allen, last changed 2018-08-06 07:41 by xtreak.

Messages (2)
msg256469 - (view) Author: Michael Allen (Michael Allen) Date: 2015-12-15 17:21
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.

Reproducible with,

import threading
import traceback

def main():
    with open(__file__, 'a') as fp:
        fp.write(' ')

threads = [
    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/", line 810, in __bootstrap_inner
  File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "", line 7, in main
  File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/", line 279, in format_stack
    return format_list(extract_stack(f, limit))
  File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/", line 305, in extract_stack
  File "/Users/me/.pyenv/versions/2.7.10/lib/python2.7/", line 69, in checkcache
    del cache[filename]
KeyError: ''

Possible solution is to ignore KeyError on del cache[filename].
msg323180 - (view) Author: Karthikeyan Singaravelan (xtreak) * Date: 2018-08-06 07:41
It seems there was a major refactor in traceback module with 6bc2c1e7ebf359224e5e547f58ffc2c42cb36a39 where this was fixed in Python 3. Ignoring the KeyError seems reasonable to me.

Date User Action Args
2018-08-06 07:41:56xtreaksetnosy: + xtreak
messages: + msg323180
2018-07-11 07:41:59serhiy.storchakasettype: crash -> behavior
2015-12-15 17:21:04Michael Allencreate