from __future__ import absolute_import
from typing import Any, Tuple

import sys
import time
import ctypes
import threading
import six
from six.moves import range

# Based on http://code.activestate.com/recipes/483752/

class TimeoutExpired(Exception):
    '''Exception raised when a function times out.'''
    def __str__(self):
        return 'Function call timed out.'

def timeout(timeout, func, *args, **kwargs):
    '''Call the function in a separate thread.
       Return its return value, or raise an exception,
       within approximately 'timeout' seconds.

       The function may receive a TimeoutExpired exception
       anywhere in its code, which could have arbitrary
       unsafe effects (resources not released, etc.).
       It might also fail to receive the exception and
       keep running in the background even though
       timeout() has returned.

       This may also fail to interrupt functions which are
       stuck in a long-running primitive interpreter
       operation.'''

    class TimeoutThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.result = None # type: Any
            self.exc_info = None # type: Tuple[type, BaseException, Any]

            # Don't block the whole program from exiting
            # if this is the only thread left.
            self.daemon = True

        def run(self):
            try:
                self.result = func(*args, **kwargs)
            except BaseException:
                self.exc_info = sys.exc_info()

        def raise_async_timeout(self):
            # Called from another thread.
            # Attempt to raise a TimeoutExpired in the thread represented by 'self'.
            tid = ctypes.c_long(self.ident)
            result = ctypes.pythonapi.PyThreadState_SetAsyncExc(
                tid, ctypes.py_object(TimeoutExpired))
            if result > 1:
                # "if it returns a number greater than one, you're in trouble,
                # and you should call it again with exc=NULL to revert the effect"
                #
                # I was unable to find the actual source of this quote, but it
                # appears in the many projects across the Internet that have
                # copy-pasted this recipe.
                ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)

    thread = TimeoutThread()
    thread.start()
    thread.join(timeout)

    if thread.isAlive():
        # Gamely try to kill the thread, following the dodgy approach from
        # http://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.
        for i in range(10):
            thread.raise_async_timeout()
            time.sleep(0.1)
            if not thread.isAlive():
                break
        raise TimeoutExpired

    if thread.exc_info:
        # Raise the original stack trace so our error messages are more useful.
        # from http://stackoverflow.com/a/4785766/90777
        six.reraise(thread.exc_info[0], thread.exc_info[1], thread.exc_info[2])
    return thread.result

@@ -44,29 +44,90 @@
  typ
-ing import Any, Tuple
+es import TracebackType%0Afrom typing import Any, Callable, Optional, Tuple, TypeVar
 %0A%0Aim
@@ -376,32 +376,58 @@
  __str__(self):%0A
+        # type: () -%3E str%0A
         return '
@@ -453,16 +453,46 @@
  out.'%0A%0A
+ResultT = TypeVar('ResultT')%0A%0A
 def time
@@ -528,16 +528,84 @@
 wargs):%0A
+    # type: (float, Callable%5B..., ResultT%5D, *Any, **Any) -%3E ResultT%0A
     '''C
@@ -1249,32 +1249,63 @@
 __init__(self):%0A
+            # type: () -%3E None%0A
             thre
@@ -1371,19 +1371,33 @@
 # type: 
-Any
+Optional%5BResultT%5D
 %0A       
@@ -1430,16 +1430,25 @@
 # type: 
+Optional%5B
 Tuple%5Bty
@@ -1466,19 +1466,30 @@
 eption, 
-Any
+TracebackType%5D
 %5D%0A%0A     
@@ -1634,32 +1634,63 @@
  def run(self):%0A
+            # type: () -%3E None%0A
             try:
@@ -1824,16 +1824,16 @@
 info()%0A%0A
-
         
@@ -1855,32 +1855,63 @@
 _timeout(self):%0A
+            # type: () -%3E None%0A
             # Ca

Annotate zerver/lib/timeout.py.