π OpenHands: AI-Driven Development
Fix download workspace zip file event loop hanging (#6722)
diwu-sfeb5be2ab637e
diff --git a/openhands/runtime/action_execution_server.py b/openhands/runtime/action_execution_server.pyindex d9ee74c429..1a2d4f1d4a 100644--- a/openhands/runtime/action_execution_server.py+++ b/openhands/runtime/action_execution_server.py@@ -8,7 +8,6 @@ NOTE: this will be executed inside the docker sandbox.import argparseimport asyncioimport base64-import ioimport mimetypesimport osimport shutil@@ -21,12 +20,13 @@ from zipfile import ZipFilefrom fastapi import Depends, FastAPI, HTTPException, Request, UploadFilefrom fastapi.exceptions import RequestValidationError-from fastapi.responses import JSONResponse, StreamingResponse+from fastapi.responses import FileResponse, JSONResponsefrom fastapi.security import APIKeyHeaderfrom openhands_aci.editor.editor import OHEditorfrom openhands_aci.editor.exceptions import ToolErrorfrom openhands_aci.editor.results import ToolResultfrom pydantic import BaseModel+from starlette.background import BackgroundTaskfrom starlette.exceptions import HTTPException as StarletteHTTPExceptionfrom uvicorn import run@@ -631,7 +631,7 @@ if __name__ == '__main__':raise HTTPException(status_code=500, detail=str(e))@app.get('/download_files')- async def download_file(path: str):+ def download_file(path: str):logger.debug('Downloading files')try:if not os.path.isabs(path):@@ -642,7 +642,7 @@ if __name__ == '__main__':if not os.path.exists(path):raise HTTPException(status_code=404, detail='File not found')- with tempfile.TemporaryFile() as temp_zip:+ with tempfile.NamedTemporaryFile(suffix='.zip', delete=False) as temp_zip:with ZipFile(temp_zip, 'w') as zipf:for root, _, files in os.walk(path):for file in files:@@ -650,15 +650,11 @@ if __name__ == '__main__':zipf.write(file_path, arcname=os.path.relpath(file_path, path))- temp_zip.seek(0) # Rewind the file to the beginning after writing- content = temp_zip.read()- # Good for small to medium-sized files. For very large files, streaming directly from the- # file chunks may be more memory-efficient.- zip_stream = io.BytesIO(content)- return StreamingResponse(- content=zip_stream,+ return FileResponse(+ path=temp_zip.name,media_type='application/zip',- headers={'Content-Disposition': f'attachment; filename={path}.zip'},+ filename=f'{os.path.basename(path)}.zip',+ background=BackgroundTask(lambda: os.unlink(temp_zip.name)),)except Exception as e:diff --git a/openhands/runtime/impl/action_execution/action_execution_client.py b/openhands/runtime/impl/action_execution/action_execution_client.pyindex 258dcf3a85..683c16578d 100644--- a/openhands/runtime/impl/action_execution/action_execution_client.py+++ b/openhands/runtime/impl/action_execution/action_execution_client.py@@ -1,4 +1,5 @@import os+import shutilimport tempfileimport threadingfrom abc import abstractmethod@@ -143,12 +144,10 @@ class ActionExecutionClient(Runtime):stream=True,timeout=30,) as response:- with tempfile.NamedTemporaryFile(delete=False) as temp_file:- total_length = 0- for chunk in response.iter_content(chunk_size=8192):- if chunk: # filter out keep-alive new chunks- total_length += len(chunk)- temp_file.write(chunk)+ with tempfile.NamedTemporaryFile(+ suffix='.zip', delete=False+ ) as temp_file:+ shutil.copyfileobj(response.raw, temp_file, length=16 * 1024)return Path(temp_file.name)except requests.Timeout:raise TimeoutError('Copy operation timed out')diff --git a/openhands/server/routes/files.py b/openhands/server/routes/files.pyindex 1581d3abad..eaa805174f 100644--- a/openhands/server/routes/files.py+++ b/openhands/server/routes/files.py@@ -3,7 +3,6 @@ import tempfilefrom fastapi import (APIRouter,- BackgroundTasks,HTTPException,Request,UploadFile,@@ -12,6 +11,7 @@ from fastapi import (from fastapi.responses import FileResponse, JSONResponsefrom pathspec import PathSpecfrom pathspec.patterns import GitWildMatchPattern+from starlette.background import BackgroundTaskfrom openhands.core.exceptions import AgentRuntimeUnavailableErrorfrom openhands.core.logger import openhands_logger as logger@@ -309,31 +309,25 @@ async def save_file(request: Request):@app.get('/zip-directory')-async def zip_current_workspace(- request: Request, conversation_id: str, background_tasks: BackgroundTasks-):+def zip_current_workspace(request: Request, conversation_id: str):try:logger.debug('Zipping workspace')runtime: Runtime = request.state.conversation.runtimepath = runtime.config.workspace_mount_path_in_sandboxtry:- zip_file = await call_sync_from_async(runtime.copy_from, path)+ zip_file_path = runtime.copy_from(path)except AgentRuntimeUnavailableError as e:logger.error(f'Error zipping workspace: {e}')return JSONResponse(status_code=500,content={'error': f'Error zipping workspace: {e}'},)- response = FileResponse(- path=zip_file,+ return FileResponse(+ path=zip_file_path,filename='workspace.zip',- media_type='application/x-zip-compressed',+ media_type='application/zip',+ background=BackgroundTask(lambda: os.unlink(zip_file_path)),)-- # This will execute after the response is sent (So the file is not deleted before being sent)- background_tasks.add_task(zip_file.unlink)-- return responseexcept Exception as e:logger.error(f'Error zipping workspace: {e}')raise HTTPException(