Skip to content

Commit cf4f7be

Browse files
committed
Use signal.alarm for timeouts if it's available
1 parent fa1f3bd commit cf4f7be

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

docs/source/conf.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -489,24 +489,31 @@ def setup(app):
489489
import binascii
490490
paramiko.client.hexlify = lambda x: binascii.hexlify(x).decode()
491491
paramiko.util.safe_string = lambda x: '' # function result never *actually used*
492-
class EndlessLoop(BaseException): pass
493-
def sigabrt_handler(signum, frame):
494-
raise EndlessLoop()
495-
# thread.interrupt_main received the signum parameter in Python 3.10
496-
if sys.version_info >= (3, 10):
497-
signal.signal(signal.SIGABRT, sigabrt_handler)
498-
def alrm_handler():
499-
try:
500-
import thread
501-
except ImportError:
502-
import _thread as thread
503-
# pre Python 3.10 this raises a KeyboardInterrupt in the main thread.
504-
# it might not show a traceback in that case, but it will stop the endless loop.
505-
thread.interrupt_main(signal.SIGABRT)
506-
timer = threading.Timer(interval=180, function=alrm_handler) # three minutes
492+
class EndlessLoop(Exception): pass
493+
if hasattr(signal, 'alarm'):
494+
def alrm_handler(sig, frame):
495+
signal.alarm(180) # three minutes
496+
raise EndlessLoop()
497+
signal.signal(signal.SIGALRM, alrm_handler)
498+
signal.alarm(600) # ten minutes
499+
else:
500+
def sigabrt_handler(signum, frame):
501+
raise EndlessLoop()
502+
# thread.interrupt_main received the signum parameter in Python 3.10
503+
if sys.version_info >= (3, 10):
504+
signal.signal(signal.SIGABRT, sigabrt_handler)
505+
def alrm_handler():
506+
try:
507+
import thread
508+
except ImportError:
509+
import _thread as thread
510+
# pre Python 3.10 this raises a KeyboardInterrupt in the main thread.
511+
# it might not show a traceback in that case, but it will stop the endless loop.
512+
thread.interrupt_main(signal.SIGABRT)
513+
timer = threading.Timer(interval=180, function=alrm_handler) # three minutes
514+
timer.daemon = True
515+
timer.start()
516+
import threading
517+
timer = threading.Timer(interval=600, function=alrm_handler) # ten minutes
507518
timer.daemon = True
508519
timer.start()
509-
import threading
510-
timer = threading.Timer(interval=600, function=alrm_handler) # ten minutes
511-
timer.daemon = True
512-
timer.start()

0 commit comments

Comments
 (0)