diff --git a/.gitea/workflows/gate-check-v3.yml b/.gitea/workflows/gate-check-v3.yml index f8d2b42c..7d9e4541 100644 --- a/.gitea/workflows/gate-check-v3.yml +++ b/.gitea/workflows/gate-check-v3.yml @@ -72,7 +72,8 @@ jobs: set -euo pipefail # Fetch all open PRs and run gate-check on each pr_numbers=$(python3 -c " - import urllib.request, json, os + import urllib.request, json, os, socket + socket.setdefaulttimeout(15) token = os.environ['GITEA_TOKEN'] req = urllib.request.Request( 'https://git.moleculesai.app/api/v1/repos/${{ github.repository }}/pulls?state=open&limit=100', diff --git a/tools/gate-check-v3/gate_check.py b/tools/gate-check-v3/gate_check.py index f25be93e..c705e77a 100644 --- a/tools/gate-check-v3/gate_check.py +++ b/tools/gate-check-v3/gate_check.py @@ -35,6 +35,11 @@ GITEA_HOST = os.environ.get("GITEA_HOST", "git.moleculesai.app") GITEA_TOKEN = os.environ.get("GITEA_TOKEN", os.environ.get("GITHUB_TOKEN", "")) API_BASE = f"https://{GITEA_HOST}/api/v1" +# Timeout for all HTTP requests (seconds). Prevents indefinite hangs when the +# Gitea instance is slow or unreachable. Must be short enough that a timeout +# failure doesn't block the overall job beyond a reasonable window. +DEFAULT_TIMEOUT = 15 + def api_get(path: str) -> dict | list: url = f"{API_BASE}{path}" @@ -46,7 +51,7 @@ def api_get(path: str) -> dict | list: }, ) try: - with urllib.request.urlopen(req) as r: + with urllib.request.urlopen(req, timeout=DEFAULT_TIMEOUT) as r: return json.loads(r.read()) except urllib.error.HTTPError as e: body = e.read().decode(errors="replace") @@ -521,12 +526,12 @@ def run(repo: str, pr_number: int, post_comment: bool = False) -> dict: comment_id = our_comments[-1]["id"] url = f"{API_BASE}/repos/{owner}/{name}/issues/comments/{comment_id}" req = urllib.request.Request(url, data=json.dumps({"body": comment_body}).encode(), headers=headers, method="PATCH") - with urllib.request.urlopen(req) as r: + with urllib.request.urlopen(req, timeout=DEFAULT_TIMEOUT) as r: r.read() else: url = f"{API_BASE}/repos/{owner}/{name}/issues/{pr_number}/comments" req = urllib.request.Request(url, data=json.dumps({"body": comment_body}).encode(), headers=headers, method="POST") - with urllib.request.urlopen(req) as r: + with urllib.request.urlopen(req, timeout=DEFAULT_TIMEOUT) as r: r.read() except urllib.error.HTTPError as e: if e.code == 403: