From 67d03fc6f8d5bf29b37f690f01ff029e94b2e835 Mon Sep 17 00:00:00 2001 From: Wade Carpenter Date: Mon, 29 Aug 2022 09:14:03 -0700 Subject: [PATCH] chat: add a timeout to requests.post call The new pylint advises us that programs using requests.METHOD without a timeout can hang forever, which seems surprising given that they're using TCP connections, but even still, two hours is probably too long as well. Added a timeout to "chatmail" with a default value of 10 seconds. --- jobrunner/mail/chat.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/jobrunner/mail/chat.py b/jobrunner/mail/chat.py index 07c6a29..b90bb00 100644 --- a/jobrunner/mail/chat.py +++ b/jobrunner/mail/chat.py @@ -77,7 +77,7 @@ class PostError(Exception): pass -def _postToGChat(text, uri, threadId=None): +def _postToGChat(text, uri, timeout, threadId=None): payload = { "text": text, } @@ -86,7 +86,7 @@ def _postToGChat(text, uri, threadId=None): } if threadId: payload["thread"] = {"name": threadId} - ret = requests.post(uri, json=payload, headers=headers) + ret = requests.post(uri, json=payload, headers=headers, timeout=timeout) try: return ret.json()["thread"]["name"] except (KeyError, TypeError): @@ -154,6 +154,9 @@ def parseArgs(args=None): ap.add_argument("-f", dest="inFile", action="store") ap.add_argument("-T", "--new-thread", action="store_true", help="Do not re-use the previous chat thread for each hook") + ap.add_argument("--timeout", type=int, default=10, + help="Specify timeout (in seconds) for REST API requests " + "(default=%(default)s)") ap.add_argument("toAddr", metavar="to-addr", nargs="+") return ap.parse_args(args) @@ -191,7 +194,7 @@ def main(args=None): if not opts.new_thread and config.chatmailReuseThreads else None) - newThreadId = _postToGChat(msg, hook, threadId=threadId) + newThreadId = _postToGChat(msg, hook, opts.timeout, threadId=threadId) threadCache.put(hook, newThreadId) return OK