From fa98f9e3e4c6a7097801a5b208a01de5faae24db Mon Sep 17 00:00:00 2001 From: pavsoss Date: Thu, 11 Jun 2026 03:35:40 +0530 Subject: [PATCH] fix: add timeout for code runner --- backend/routes/plugins.py | 3 ++- backend/tests/test_api.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/routes/plugins.py b/backend/routes/plugins.py index a08f938..3f4983e 100644 --- a/backend/routes/plugins.py +++ b/backend/routes/plugins.py @@ -9,6 +9,7 @@ import subprocess import tempfile import os +import sys from fastapi import APIRouter, HTTPException from models.schemas import PluginRun, PluginResult from services import db_service @@ -152,7 +153,7 @@ def _coderunner(code: str) -> str: try: result = subprocess.run( - ["python3", tmp], + [sys.executable, tmp], capture_output=True, text=True, timeout=5 ) output = result.stdout or result.stderr or "(no output)" diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index b2f9949..2b83e30 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -130,6 +130,22 @@ def test_unknown_plugin(): assert r.status_code == 400 +def test_coderunner_success(): + r = client.post("/api/plugins/run", json={"plugin": "coderunner", "input": "print('hello world')"}) + assert r.status_code == 200 + assert r.json()["success"] + assert "hello world" in r.json()["output"] + + +def test_coderunner_timeout(): + r = client.post("/api/plugins/run", json={ + "plugin": "coderunner", + "input": "import time\ntime.sleep(6)" + }) + assert r.status_code == 200 + assert r.json()["success"] + assert "Timeout" in r.json()["output"] + # ─── Settings ──────────────────────────────────────────── def test_get_settings(): r = client.get("/api/settings/")