Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -103,6 +103,17 @@ def xe_js():
|
|
| 103 |
mimetype="application/javascript"
|
| 104 |
)
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
@app.route("/execute", methods=["POST"])
|
| 107 |
def execute():
|
| 108 |
print("=" * 60)
|
|
@@ -124,6 +135,15 @@ def execute():
|
|
| 124 |
print(code)
|
| 125 |
print("-" * 60)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# stdout / stderr をキャプチャ
|
| 128 |
stdout_buf = io.StringIO()
|
| 129 |
stderr_buf = io.StringIO()
|
|
@@ -132,6 +152,11 @@ def execute():
|
|
| 132 |
exec_globals = {"__name__": "__main__"}
|
| 133 |
print(f"[execute] 実行環境(globals)初期キー: {list(exec_globals.keys())}")
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
try:
|
| 136 |
print("[execute] コード実行を開始します")
|
| 137 |
with contextlib.redirect_stdout(stdout_buf), contextlib.redirect_stderr(stderr_buf):
|
|
@@ -154,6 +179,26 @@ def execute():
|
|
| 154 |
"result": None,
|
| 155 |
})
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
except Exception as e:
|
| 158 |
print("[execute] 例外が発生しました")
|
| 159 |
print(f"[execute] 例外タイプ: {type(e).__name__}")
|
|
@@ -180,6 +225,10 @@ def execute():
|
|
| 180 |
}), 200
|
| 181 |
|
| 182 |
finally:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
print("[execute] リクエスト処理を終了します")
|
| 184 |
print("=" * 60)
|
| 185 |
|
|
|
|
| 103 |
mimetype="application/javascript"
|
| 104 |
)
|
| 105 |
|
| 106 |
+
import signal
|
| 107 |
+
|
| 108 |
+
class ExecutionTimeout(Exception):
|
| 109 |
+
"""コード実行が制限時間を超えたときに送出する例外"""
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def _alarm_handler(signum, frame):
|
| 114 |
+
raise ExecutionTimeout("コード実行が制限時間を超えました")
|
| 115 |
+
|
| 116 |
+
|
| 117 |
@app.route("/execute", methods=["POST"])
|
| 118 |
def execute():
|
| 119 |
print("=" * 60)
|
|
|
|
| 135 |
print(code)
|
| 136 |
print("-" * 60)
|
| 137 |
|
| 138 |
+
# タイムアウト秒(リクエストで指定可能、デフォルト10秒)
|
| 139 |
+
try:
|
| 140 |
+
timeout_sec = int(data.get("timeout", 10))
|
| 141 |
+
except (TypeError, ValueError):
|
| 142 |
+
timeout_sec = 10
|
| 143 |
+
# 安全のため範囲を制限(1秒〜60秒)
|
| 144 |
+
timeout_sec = max(1, min(timeout_sec, 60))
|
| 145 |
+
print(f"[execute] タイムアウト設定: {timeout_sec} 秒")
|
| 146 |
+
|
| 147 |
# stdout / stderr をキャプチャ
|
| 148 |
stdout_buf = io.StringIO()
|
| 149 |
stderr_buf = io.StringIO()
|
|
|
|
| 152 |
exec_globals = {"__name__": "__main__"}
|
| 153 |
print(f"[execute] 実行環境(globals)初期キー: {list(exec_globals.keys())}")
|
| 154 |
|
| 155 |
+
# 既存の SIGALRM ハンドラを退避
|
| 156 |
+
old_handler = signal.signal(signal.SIGALRM, _alarm_handler)
|
| 157 |
+
signal.alarm(timeout_sec)
|
| 158 |
+
print(f"[execute] SIGALRM を {timeout_sec} 秒で設定しました")
|
| 159 |
+
|
| 160 |
try:
|
| 161 |
print("[execute] コード実行を開始します")
|
| 162 |
with contextlib.redirect_stdout(stdout_buf), contextlib.redirect_stderr(stderr_buf):
|
|
|
|
| 179 |
"result": None,
|
| 180 |
})
|
| 181 |
|
| 182 |
+
except ExecutionTimeout as e:
|
| 183 |
+
print("[execute] タイムアウトにより実行を中断しました")
|
| 184 |
+
print(f"[execute] タイムアウト秒: {timeout_sec}")
|
| 185 |
+
|
| 186 |
+
captured_stdout = stdout_buf.getvalue()
|
| 187 |
+
captured_stderr = stderr_buf.getvalue()
|
| 188 |
+
|
| 189 |
+
print(f"[execute] タイムアウト時 stdout ({len(captured_stdout)} 文字):")
|
| 190 |
+
print(captured_stdout)
|
| 191 |
+
print(f"[execute] タイムアウト時 stderr ({len(captured_stderr)} 文字):")
|
| 192 |
+
print(captured_stderr)
|
| 193 |
+
|
| 194 |
+
return jsonify({
|
| 195 |
+
"success": False,
|
| 196 |
+
"stdout": captured_stdout,
|
| 197 |
+
"stderr": captured_stderr,
|
| 198 |
+
"error": f"ExecutionTimeout: {e}",
|
| 199 |
+
"traceback": None,
|
| 200 |
+
}), 200
|
| 201 |
+
|
| 202 |
except Exception as e:
|
| 203 |
print("[execute] 例外が発生しました")
|
| 204 |
print(f"[execute] 例外タイプ: {type(e).__name__}")
|
|
|
|
| 225 |
}), 200
|
| 226 |
|
| 227 |
finally:
|
| 228 |
+
# アラームを必ず解除し、元のハンドラに戻す
|
| 229 |
+
signal.alarm(0)
|
| 230 |
+
signal.signal(signal.SIGALRM, old_handler)
|
| 231 |
+
print("[execute] SIGALRM を解除しました")
|
| 232 |
print("[execute] リクエスト処理を終了します")
|
| 233 |
print("=" * 60)
|
| 234 |
|