Closed adamnovak closed 1 year ago
I wonder if globals().copy().items()
would be sufficient. I would think the GIL would block during the copy operation.
@adamnovak I couldn't find a quick way to replicate the behavior to test. Can you see if adding copy()
resolves it for your usecase?
I don't have a real reproduction for this; I've only ever seen it once and a reliable reproduction would involve writing a bunch of code to make a bunch of globals in a bunch of threads.
I think adding a copy()
call would be the right way to address the issue; the dict
documentation doesn't say that copy()
can raise exceptions, or that it internally does any detectable "iteration".
In the
get_keyboard_codes()
function,blessed
iterates overglobals().items()
:https://github.com/jquast/blessed/blob/1.19.1/blessed/keyboard.py#L114
If another Python thread exists, it can create (or destroy?) a global variable while Blessed is iterating. This can cause an exception, like this one that occurred in my project:
Either Blessed needs to make a snapshot of
globals()
and then loop over it, orget_keyboard_codes()
needs to catch this error and retry its loop overglobals().items()
repeatedly until it gets through without a concurrent modification from another thread.