STINNER Victor added the comment: The problem is the weakref callback of _module_locks. It seems like the callback is called after a second thread replaced _module_locks[name]
* Thread 1: _get_module_lock('random') sets _module_locks['random'] and registers a weakref callback on the newly created lock * Thread 1: the lock is destroyed, the weakref callback will be called as soon as possible * Thread 2: _get_module_lock('random') sees that the weakref is dead and so replaces _module_locks['random'] with a new lock, and registers a new weakref callback on the second lock * Thread 1: Call the weakref callback removing _module_locks['random'] -- BUG: the callback is called too late, after Thread 2 already sets a new lock * Thread 2: The lock is destroyed, the second weakref callback will be called as soon as possible * Thread 2: The second callback is called and fails with KeyError since _module_locks['random'] was already called in the Thread 1 in the meanwhile ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue31070> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com