Fix race condition in thread_keypress