Skip to content

Commit

Permalink
chat: add a timeout to requests.post call
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wwade committed Aug 29, 2022
1 parent a9f7f12 commit 67d03fc
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions jobrunner/mail/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PostError(Exception):
pass


def _postToGChat(text, uri, threadId=None):
def _postToGChat(text, uri, timeout, threadId=None):
payload = {
"text": text,
}
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 67d03fc

Please sign in to comment.