Spaces:
Running
Running
Amir Mahla
commited on
Commit
·
1d83133
1
Parent(s):
975f40e
FIX race condition
Browse files
cua2-core/src/cua2_core/services/agent_service.py
CHANGED
|
@@ -55,6 +55,9 @@ class AgentService:
|
|
| 55 |
self._lock = asyncio.Lock()
|
| 56 |
self.max_sandboxes = max_sandboxes
|
| 57 |
self._archival_lock_file: IO[str] | None = None
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Initialize archival service in dedicated process
|
| 60 |
self.archival_service = ArchivalService(
|
|
@@ -114,6 +117,22 @@ class AgentService:
|
|
| 114 |
|
| 115 |
async def create_id_and_sandbox(self, websocket: WebSocket) -> str:
|
| 116 |
"""Create a new ID and sandbox"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
async with self._lock:
|
| 118 |
uuid = str(uuid4())
|
| 119 |
while uuid in self.active_tasks:
|
|
|
|
| 55 |
self._lock = asyncio.Lock()
|
| 56 |
self.max_sandboxes = max_sandboxes
|
| 57 |
self._archival_lock_file: IO[str] | None = None
|
| 58 |
+
self._start_time = (
|
| 59 |
+
time.time()
|
| 60 |
+
) # Track app start time to prevent sandbox creation in first 30s
|
| 61 |
|
| 62 |
# Initialize archival service in dedicated process
|
| 63 |
self.archival_service = ArchivalService(
|
|
|
|
| 117 |
|
| 118 |
async def create_id_and_sandbox(self, websocket: WebSocket) -> str:
|
| 119 |
"""Create a new ID and sandbox"""
|
| 120 |
+
# Prevent sandbox creation for the first 30 seconds after app start
|
| 121 |
+
# This prevents spawning sandboxes for all users already connected when app restarts
|
| 122 |
+
elapsed_time = time.time() - self._start_time
|
| 123 |
+
if elapsed_time < 30:
|
| 124 |
+
logger.info(
|
| 125 |
+
f"Skipping sandbox creation (app started {elapsed_time:.1f}s ago, "
|
| 126 |
+
f"waiting for 30s grace period)"
|
| 127 |
+
)
|
| 128 |
+
# Still create UUID and register websocket, but don't acquire sandbox
|
| 129 |
+
async with self._lock:
|
| 130 |
+
uuid = str(uuid4())
|
| 131 |
+
while uuid in self.active_tasks:
|
| 132 |
+
uuid = str(uuid4())
|
| 133 |
+
self.task_websockets[uuid] = websocket
|
| 134 |
+
return uuid
|
| 135 |
+
|
| 136 |
async with self._lock:
|
| 137 |
uuid = str(uuid4())
|
| 138 |
while uuid in self.active_tasks:
|