Skip to content

Commit 7fda1f4

Browse files
committed
fix(core): kill sandbox timeout and set id to none
1 parent 3efaa94 commit 7fda1f4

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/server/core/acontext_core/schema/orm/sandbox_log.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class SandboxLog(CommonMixin):
2626
)
2727
}
2828
)
29-
backend_sandbox_id: str = field(metadata={"db": Column(String, nullable=False)})
29+
backend_sandbox_id: str | None = field(
30+
metadata={"db": Column(String, nullable=True)}
31+
)
3032
backend_type: str = field(metadata={"db": Column(String, nullable=False)})
3133

3234
history_commands: dict = field(metadata={"db": Column(JSONB, nullable=False)})

src/server/core/acontext_core/service/data/sandbox.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def _get_backend_sandbox_id(
5151
result = await db_session.execute(stmt)
5252
backend_id = result.scalar_one_or_none()
5353
if backend_id is None:
54-
return Result.reject(f"Sandbox {sandbox_id} not found")
54+
return Result.reject(f"Sandbox {sandbox_id} not found or was killed.")
5555
return Result.resolve(backend_id)
5656

5757

@@ -124,7 +124,18 @@ async def kill_sandbox(db_session: AsyncSession, sandbox_id: asUUID) -> Result[b
124124
backend = SANDBOX_CLIENT.use_backend()
125125
success = await backend.kill_sandbox(backend_sandbox_id)
126126

127+
# Set backend_sandbox_id to None to indicate the sandbox is killed
128+
stmt = (
129+
update(SandboxLog)
130+
.where(SandboxLog.id == sandbox_id)
131+
.values(backend_sandbox_id=None)
132+
)
133+
await db_session.execute(stmt)
134+
127135
LOG.info(f"Killed sandbox {sandbox_id} (backend: {backend_sandbox_id})")
136+
await _update_will_total_alive_seconds(
137+
db_session, sandbox_id, reset_alive_seconds=0
138+
)
128139
return Result.resolve(success)
129140
except ValueError as e:
130141
return Result.reject(f"Sandbox backend not available: {e}")
@@ -295,9 +306,7 @@ async def download_file(
295306
if success:
296307
# Append to generated_files using PostgreSQL JSONB || operator
297308
# Use COALESCE to handle NULL values
298-
new_entry = [
299-
{"sandbox_path": from_sandbox_file, "s3_path": download_to_s3_key}
300-
]
309+
new_entry = [{"sandbox_path": from_sandbox_file}]
301310
stmt = (
302311
update(SandboxLog)
303312
.where(SandboxLog.id == sandbox_id)

src/server/core/tests/service/test_sandbox.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,7 @@ async def test_download_file_logs_to_generated_files(self, mock_sandbox_backend)
296296
files = sandbox_log.data.generated_files
297297
assert len(files) == 2
298298
assert files[0]["sandbox_path"] == "/app/output.txt"
299-
assert files[0]["s3_path"] == "project/outputs/output.txt"
300299
assert files[1]["sandbox_path"] == "/app/report.pdf"
301-
assert files[1]["s3_path"] == "project/reports/report.pdf"
302300

303301
# Clean up
304302
await session.delete(project)
@@ -362,5 +360,11 @@ async def test_kill_sandbox_success(self, mock_sandbox_backend):
362360
assert kill_result.ok()
363361
assert kill_result.data is True
364362

363+
# Verify backend_sandbox_id is set to None after kill
364+
await session.commit()
365+
sandbox_log = await session.get(SandboxLog, unified_id)
366+
assert sandbox_log is not None
367+
assert sandbox_log.backend_sandbox_id is None
368+
365369
# Clean up
366370
await session.delete(project)

0 commit comments

Comments
 (0)