2121from flask import Flask , request , jsonify , send_from_directory
2222from flask_cors import CORS
2323
24- # ---------------------------------------------------------------------------
25- # Configuration
26- # ---------------------------------------------------------------------------
27-
28- SESSION_TTL = 3600 # 1 hour of inactivity before cleanup
29- CLEANUP_INTERVAL = 60 # Check for stale sessions every 60 seconds
30- EXEC_TIMEOUT = 35 # Server-side timeout for exec/eval (slightly > worker's 30s)
31- WORKER_SCRIPT = str (Path (__file__ ).parent / "worker.py" )
24+ from pathview .config import (
25+ WORKER_SCRIPT ,
26+ SERVER_TIMEOUT ,
27+ INIT_TIMEOUT ,
28+ SESSION_TTL ,
29+ CLEANUP_INTERVAL ,
30+ )
3231
3332# ---------------------------------------------------------------------------
3433# Session management
@@ -80,7 +79,7 @@ def read_line(self) -> dict | None:
8079 except json .JSONDecodeError :
8180 continue
8281
83- def read_line_timeout (self , timeout : float = EXEC_TIMEOUT ) -> dict | None :
82+ def read_line_timeout (self , timeout : float = SERVER_TIMEOUT ) -> dict | None :
8483 """Read one JSON line with a timeout. Returns None on EOF or timeout.
8584
8685 Raises TimeoutError if no response within the timeout period.
@@ -116,7 +115,7 @@ def ensure_initialized(self, packages: list[dict] | None = None) -> list[dict]:
116115 init_msg ["packages" ] = packages
117116 self .send_message (init_msg )
118117 while True :
119- resp = self .read_line ( )
118+ resp = self .read_line_timeout ( timeout = INIT_TIMEOUT )
120119 if resp is None :
121120 raise RuntimeError ("Worker process died during initialization" )
122121 messages .append (resp )
@@ -480,7 +479,8 @@ def api_stream_exec():
480479 if not session :
481480 return jsonify ({"error" : "No active session" }), 404
482481 try :
483- session .send_message ({"type" : "stream-exec" , "code" : code })
482+ with session .lock :
483+ session .send_message ({"type" : "stream-exec" , "code" : code })
484484 return jsonify ({"status" : "queued" })
485485 except Exception as e :
486486 return jsonify ({"error" : str (e )}), 500
@@ -497,7 +497,8 @@ def api_stream_stop():
497497 if not session :
498498 return jsonify ({"status" : "stopped" })
499499 try :
500- session .send_message ({"type" : "stream-stop" })
500+ with session .lock :
501+ session .send_message ({"type" : "stream-stop" })
501502 return jsonify ({"status" : "stopped" })
502503 except Exception as e :
503504 return jsonify ({"error" : str (e )}), 500
0 commit comments