Update app.py
Browse files
app.py
CHANGED
|
@@ -405,28 +405,29 @@ class SecurePythonExecutor:
|
|
| 405 |
'__name__': '__main__',
|
| 406 |
'__doc__': None,
|
| 407 |
}
|
|
|
|
| 408 |
|
| 409 |
-
print(f"DEBUG:
|
| 410 |
-
# Execute the code
|
| 411 |
exec(code, restricted_globals)
|
| 412 |
-
print("DEBUG: Code execution completed successfully in thread.")
|
| 413 |
result['success'] = True
|
| 414 |
|
| 415 |
except EOFError as e:
|
| 416 |
# This happens if the WebSocket disconnects during input()
|
| 417 |
result['error'] = f"Input stream closed: {str(e)}"
|
| 418 |
result['success'] = False
|
| 419 |
-
print(f"ERROR: EOFError during execution in thread: {e}")
|
| 420 |
except Exception as e:
|
| 421 |
error_msg = f"{type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
|
| 422 |
-
print(f"ERROR: Unhandled exception during execution in thread: {error_msg}")
|
| 423 |
# Write error to WebSocket and internal buffer
|
| 424 |
try:
|
| 425 |
# Use asyncio.run for writing within a synchronous thread, if necessary
|
| 426 |
# This is generally safe if it's a new event loop per call or in a separate thread.
|
| 427 |
asyncio.run(websocket_io.write(f"Error: {error_msg}\n"))
|
|
|
|
| 428 |
except Exception as write_e:
|
| 429 |
-
print(f"ERROR: Failed to write error to WebSocket from thread: {write_e}")
|
| 430 |
websocket_io.error_buffer.write(error_msg)
|
| 431 |
result['success'] = False
|
| 432 |
finally:
|
|
@@ -447,7 +448,7 @@ class SecurePythonExecutor:
|
|
| 447 |
thread.join(timeout=self.timeout)
|
| 448 |
|
| 449 |
if thread.is_alive():
|
| 450 |
-
print(f"DEBUG: Thread '{thread.name}' timed out after {self.timeout} seconds.")
|
| 451 |
result['error'] = f"Code execution timed out after {self.timeout} seconds"
|
| 452 |
result['success'] = False
|
| 453 |
# Attempt to send timeout message to client
|
|
@@ -456,7 +457,7 @@ class SecurePythonExecutor:
|
|
| 456 |
except Exception:
|
| 457 |
pass # Ignore if websocket is already closed
|
| 458 |
else:
|
| 459 |
-
print(f"DEBUG: Thread '{thread.name}' finished within timeout.")
|
| 460 |
result['output'] = websocket_io.output_buffer.getvalue()
|
| 461 |
if websocket_io.error_buffer.getvalue():
|
| 462 |
result['error'] = websocket_io.error_buffer.getvalue()
|
|
@@ -466,11 +467,11 @@ class SecurePythonExecutor:
|
|
| 466 |
|
| 467 |
# Send a final message to the client indicating execution is complete
|
| 468 |
try:
|
| 469 |
-
print(f"DEBUG: Attempting to send execution_complete message (success={result['success']}).")
|
| 470 |
await websocket_io.send_text(json.dumps({"type": "execution_complete", "result": result}))
|
| 471 |
print("DEBUG: execution_complete message sent.")
|
| 472 |
except WebSocketDisconnect:
|
| 473 |
-
print("DEBUG: WebSocketDisconnect when sending execution_complete.")
|
| 474 |
pass # Ignore if websocket is already closed
|
| 475 |
except Exception as e:
|
| 476 |
print(f"ERROR: Error sending execution_complete message: {e}") # Warning if this fails
|
|
@@ -489,13 +490,12 @@ app = FastAPI(
|
|
| 489 |
version="1.0.0",
|
| 490 |
)
|
| 491 |
|
| 492 |
-
# Serve the HTML frontend
|
| 493 |
# Serve the HTML frontend
|
| 494 |
@app.get("/", response_class=HTMLResponse)
|
| 495 |
async def get_index():
|
| 496 |
"""Serves the interactive frontend."""
|
| 497 |
# This assumes index.html is in the same directory as app.py
|
| 498 |
-
# Explicitly copy index.html to /app/index.html to avoid any ambiguity with '.' [cite:
|
| 499 |
with open("index.html", "r") as f:
|
| 500 |
return HTMLResponse(content=f.read())
|
| 501 |
|
|
|
|
| 405 |
'__name__': '__main__',
|
| 406 |
'__doc__': None,
|
| 407 |
}
|
| 408 |
+
print("DEBUG: Restricted globals prepared.") # ADDED THIS LINE
|
| 409 |
|
| 410 |
+
print(f"DEBUG: Preparing to execute code (thread): {code[:100]}...")
|
|
|
|
| 411 |
exec(code, restricted_globals)
|
| 412 |
+
print("DEBUG: Code execution completed successfully in thread.")
|
| 413 |
result['success'] = True
|
| 414 |
|
| 415 |
except EOFError as e:
|
| 416 |
# This happens if the WebSocket disconnects during input()
|
| 417 |
result['error'] = f"Input stream closed: {str(e)}"
|
| 418 |
result['success'] = False
|
| 419 |
+
print(f"ERROR: EOFError during execution in thread: {e}")
|
| 420 |
except Exception as e:
|
| 421 |
error_msg = f"{type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
|
| 422 |
+
print(f"ERROR: Unhandled exception during execution in thread: {error_msg}")
|
| 423 |
# Write error to WebSocket and internal buffer
|
| 424 |
try:
|
| 425 |
# Use asyncio.run for writing within a synchronous thread, if necessary
|
| 426 |
# This is generally safe if it's a new event loop per call or in a separate thread.
|
| 427 |
asyncio.run(websocket_io.write(f"Error: {error_msg}\n"))
|
| 428 |
+
print("DEBUG: Attempted to write error via websocket_io.write from thread.")
|
| 429 |
except Exception as write_e:
|
| 430 |
+
print(f"ERROR: Failed to write error to WebSocket from thread (inner): {write_e}")
|
| 431 |
websocket_io.error_buffer.write(error_msg)
|
| 432 |
result['success'] = False
|
| 433 |
finally:
|
|
|
|
| 448 |
thread.join(timeout=self.timeout)
|
| 449 |
|
| 450 |
if thread.is_alive():
|
| 451 |
+
print(f"DEBUG: Thread '{thread.name}' timed out after {self.timeout} seconds.")
|
| 452 |
result['error'] = f"Code execution timed out after {self.timeout} seconds"
|
| 453 |
result['success'] = False
|
| 454 |
# Attempt to send timeout message to client
|
|
|
|
| 457 |
except Exception:
|
| 458 |
pass # Ignore if websocket is already closed
|
| 459 |
else:
|
| 460 |
+
print(f"DEBUG: Thread '{thread.name}' finished within timeout.")
|
| 461 |
result['output'] = websocket_io.output_buffer.getvalue()
|
| 462 |
if websocket_io.error_buffer.getvalue():
|
| 463 |
result['error'] = websocket_io.error_buffer.getvalue()
|
|
|
|
| 467 |
|
| 468 |
# Send a final message to the client indicating execution is complete
|
| 469 |
try:
|
| 470 |
+
print(f"DEBUG: Attempting to send execution_complete message (success={result['success']}).")
|
| 471 |
await websocket_io.send_text(json.dumps({"type": "execution_complete", "result": result}))
|
| 472 |
print("DEBUG: execution_complete message sent.")
|
| 473 |
except WebSocketDisconnect:
|
| 474 |
+
print("DEBUG: WebSocketDisconnect when sending execution_complete.")
|
| 475 |
pass # Ignore if websocket is already closed
|
| 476 |
except Exception as e:
|
| 477 |
print(f"ERROR: Error sending execution_complete message: {e}") # Warning if this fails
|
|
|
|
| 490 |
version="1.0.0",
|
| 491 |
)
|
| 492 |
|
|
|
|
| 493 |
# Serve the HTML frontend
|
| 494 |
@app.get("/", response_class=HTMLResponse)
|
| 495 |
async def get_index():
|
| 496 |
"""Serves the interactive frontend."""
|
| 497 |
# This assumes index.html is in the same directory as app.py
|
| 498 |
+
# [cite_start]Explicitly copy index.html to /app/index.html to avoid any ambiguity with '.' [cite: 5, 6]
|
| 499 |
with open("index.html", "r") as f:
|
| 500 |
return HTMLResponse(content=f.read())
|
| 501 |
|