From 04159a674cee6c5f89f6239d0e447a1d2208fa2c Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 5 Apr 2022 11:39:12 -0700 Subject: [PATCH] timeout: Minor comment cleanups. We remove the StackOverflow link because it is now so dated as to be irrelevant -- it does not use `self.ident`, and cargo-cults the return value of PyThreadState_SetAsyncExc. --- zerver/lib/timeout.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/zerver/lib/timeout.py b/zerver/lib/timeout.py index 46ef82c083..3f62b7c2c1 100644 --- a/zerver/lib/timeout.py +++ b/zerver/lib/timeout.py @@ -56,12 +56,12 @@ def timeout(timeout: float, func: Callable[[], ResultT]) -> ResultT: self.exc_info = sys.exc_info() def raise_async_timeout(self) -> None: - # Called from another thread. - # Attempt to raise a TimeoutExpired in the thread represented by 'self'. - assert self.ident is not None # Thread should be running; c_long expects int - tid = ctypes.c_long(self.ident) + # This function is called from another thread; we attempt + # to raise a TimeoutExpired in _this_ thread. + assert self.ident is not None ctypes.pythonapi.PyThreadState_SetAsyncExc( - tid, ctypes.py_object(TimeoutExpired) + ctypes.c_long(self.ident), + ctypes.py_object(TimeoutExpired), ) thread = TimeoutThread() @@ -69,11 +69,8 @@ def timeout(timeout: float, func: Callable[[], ResultT]) -> ResultT: thread.join(timeout) if thread.is_alive(): - # Gamely try to kill the thread, following the dodgy approach from - # https://stackoverflow.com/a/325528/90777 - # - # We need to retry, because an async exception received while the - # thread is in a system call is simply ignored. + # We need to retry, because an async exception received while + # the thread is in a system call is simply ignored. for i in range(10): thread.raise_async_timeout() time.sleep(0.1) @@ -94,5 +91,6 @@ def timeout(timeout: float, func: Callable[[], ResultT]) -> ResultT: if thread.exc_info[1] is not None: # Died with some other exception; re-raise it raise thread.exc_info[1].with_traceback(thread.exc_info[2]) - assert thread.result is not None # assured if above did not reraise + + assert thread.result is not None return thread.result