Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,15 @@ import resource
|
|
| 5 |
import multiprocessing
|
| 6 |
import signal
|
| 7 |
import time
|
|
|
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
|
|
|
|
| 10 |
# Store running sandboxes
|
|
|
|
| 11 |
sandboxes = {}
|
|
|
|
| 12 |
|
| 13 |
# -------------------------
|
| 14 |
# Sandbox worker function
|
|
@@ -33,7 +38,7 @@ def sandbox_worker(code: str, sandbox_id: str):
|
|
| 33 |
["python3", code_file],
|
| 34 |
capture_output=True,
|
| 35 |
text=True,
|
| 36 |
-
timeout=
|
| 37 |
)
|
| 38 |
# Save logs
|
| 39 |
with open(os.path.join(user_dir, "stdout.log"), "w") as f:
|
|
@@ -44,6 +49,26 @@ def sandbox_worker(code: str, sandbox_id: str):
|
|
| 44 |
with open(os.path.join(user_dir, "stderr.log"), "w") as f:
|
| 45 |
f.write("Execution timed out (2h limit reached).")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# -------------------------
|
| 48 |
# Gradio interface functions
|
| 49 |
# -------------------------
|
|
@@ -70,6 +95,9 @@ def kill_sandbox(sandbox_id):
|
|
| 70 |
pid = sandboxes[sandbox_id]["pid"]
|
| 71 |
try:
|
| 72 |
os.kill(pid, signal.SIGKILL)
|
|
|
|
|
|
|
|
|
|
| 73 |
del sandboxes[sandbox_id]
|
| 74 |
return f"Sandbox {sandbox_id} killed."
|
| 75 |
except Exception as e:
|
|
@@ -85,27 +113,23 @@ def status_sandbox(sandbox_id):
|
|
| 85 |
# Gradio UI
|
| 86 |
# -------------------------
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
-
gr.Markdown("# Sandbox Backend")
|
| 89 |
-
|
| 90 |
code_input = gr.Textbox(label="Python Code", lines=15, placeholder="Write your code here...")
|
| 91 |
launch_btn = gr.Button("Launch Sandbox")
|
| 92 |
sandbox_id_output = gr.Textbox(label="Sandbox ID")
|
| 93 |
-
|
| 94 |
logs_btn = gr.Button("Fetch Logs")
|
| 95 |
logs_output = gr.Textbox(label="Logs", lines=10)
|
| 96 |
-
|
| 97 |
kill_btn = gr.Button("Kill Sandbox")
|
| 98 |
kill_output = gr.Textbox(label="Kill Status")
|
| 99 |
-
|
| 100 |
status_btn = gr.Button("Check Status")
|
| 101 |
status_output = gr.Textbox(label="Sandbox Status")
|
| 102 |
-
|
| 103 |
-
# Temporary storage for sandbox ID
|
| 104 |
sandbox_id_storage = gr.State(value=None)
|
| 105 |
-
|
| 106 |
-
# -------------------------
|
| 107 |
-
# Button actions
|
| 108 |
-
# -------------------------
|
| 109 |
launch_btn.click(
|
| 110 |
launch_sandbox,
|
| 111 |
inputs=code_input,
|
|
@@ -127,7 +151,4 @@ with gr.Blocks() as demo:
|
|
| 127 |
outputs=status_output
|
| 128 |
)
|
| 129 |
|
| 130 |
-
# -------------------------
|
| 131 |
-
# Launch Gradio app
|
| 132 |
-
# -------------------------
|
| 133 |
demo.launch()
|
|
|
|
| 5 |
import multiprocessing
|
| 6 |
import signal
|
| 7 |
import time
|
| 8 |
+
import threading
|
| 9 |
+
import shutil
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
+
# -------------------------
|
| 13 |
# Store running sandboxes
|
| 14 |
+
# -------------------------
|
| 15 |
sandboxes = {}
|
| 16 |
+
SANDBOX_TTL = 2 * 3600 # 2 hours
|
| 17 |
|
| 18 |
# -------------------------
|
| 19 |
# Sandbox worker function
|
|
|
|
| 38 |
["python3", code_file],
|
| 39 |
capture_output=True,
|
| 40 |
text=True,
|
| 41 |
+
timeout=SANDBOX_TTL
|
| 42 |
)
|
| 43 |
# Save logs
|
| 44 |
with open(os.path.join(user_dir, "stdout.log"), "w") as f:
|
|
|
|
| 49 |
with open(os.path.join(user_dir, "stderr.log"), "w") as f:
|
| 50 |
f.write("Execution timed out (2h limit reached).")
|
| 51 |
|
| 52 |
+
# -------------------------
|
| 53 |
+
# Cleanup thread
|
| 54 |
+
# -------------------------
|
| 55 |
+
def cleanup_sandboxes():
|
| 56 |
+
while True:
|
| 57 |
+
now = time.time()
|
| 58 |
+
for sid, info in list(sandboxes.items()):
|
| 59 |
+
if now - info["start_time"] > SANDBOX_TTL:
|
| 60 |
+
try:
|
| 61 |
+
os.kill(info["pid"], signal.SIGKILL)
|
| 62 |
+
except:
|
| 63 |
+
pass
|
| 64 |
+
folder = f"/tmp/sandbox_{sid}"
|
| 65 |
+
if os.path.exists(folder):
|
| 66 |
+
shutil.rmtree(folder)
|
| 67 |
+
del sandboxes[sid]
|
| 68 |
+
time.sleep(60) # check every minute
|
| 69 |
+
|
| 70 |
+
threading.Thread(target=cleanup_sandboxes, daemon=True).start()
|
| 71 |
+
|
| 72 |
# -------------------------
|
| 73 |
# Gradio interface functions
|
| 74 |
# -------------------------
|
|
|
|
| 95 |
pid = sandboxes[sandbox_id]["pid"]
|
| 96 |
try:
|
| 97 |
os.kill(pid, signal.SIGKILL)
|
| 98 |
+
folder = f"/tmp/sandbox_{sandbox_id}"
|
| 99 |
+
if os.path.exists(folder):
|
| 100 |
+
shutil.rmtree(folder)
|
| 101 |
del sandboxes[sandbox_id]
|
| 102 |
return f"Sandbox {sandbox_id} killed."
|
| 103 |
except Exception as e:
|
|
|
|
| 113 |
# Gradio UI
|
| 114 |
# -------------------------
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
+
gr.Markdown("# Sandbox Backend with Auto-Cleanup")
|
| 117 |
+
|
| 118 |
code_input = gr.Textbox(label="Python Code", lines=15, placeholder="Write your code here...")
|
| 119 |
launch_btn = gr.Button("Launch Sandbox")
|
| 120 |
sandbox_id_output = gr.Textbox(label="Sandbox ID")
|
| 121 |
+
|
| 122 |
logs_btn = gr.Button("Fetch Logs")
|
| 123 |
logs_output = gr.Textbox(label="Logs", lines=10)
|
| 124 |
+
|
| 125 |
kill_btn = gr.Button("Kill Sandbox")
|
| 126 |
kill_output = gr.Textbox(label="Kill Status")
|
| 127 |
+
|
| 128 |
status_btn = gr.Button("Check Status")
|
| 129 |
status_output = gr.Textbox(label="Sandbox Status")
|
| 130 |
+
|
|
|
|
| 131 |
sandbox_id_storage = gr.State(value=None)
|
| 132 |
+
|
|
|
|
|
|
|
|
|
|
| 133 |
launch_btn.click(
|
| 134 |
launch_sandbox,
|
| 135 |
inputs=code_input,
|
|
|
|
| 151 |
outputs=status_output
|
| 152 |
)
|
| 153 |
|
|
|
|
|
|
|
|
|
|
| 154 |
demo.launch()
|