Skip to content

Commit

Permalink
fix(main): handle UnboundLocalError when subprocess fails
Browse files Browse the repository at this point in the history
- Introduced a local variable `return_code` to store the return code of the process.
- The `handle_error` function now uses `return_code` which is initially set to None. This avoids referencing the `process` variable before it is assigned, in case the subprocess fails to launch.
  • Loading branch information
mdbecker committed Jun 27, 2023
1 parent 39d1797 commit 79f7dd3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions docker.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DB_URI=sqlite:////app/data/database.db
EXECUTABLE=./echo_args.sh
14 changes: 8 additions & 6 deletions gull_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,29 @@ async def handle_error(request, status_code, detail, stderr="", returncode=1):
log = await create_and_log(request, stderr=stderr, returncode=returncode)
raise HTTPException(status_code=status_code, detail=detail)

# Updated post_llm function
@app.post("/llm")
async def post_llm(request: Dict[str, Any], cli_json=Depends(load_cli_json)):
LLMRequest = create_llm_request_model(cli_json)
validated_request = LLMRequest(**request)
command = convert_request_to_cli_command(validated_request, cli_json)

return_code = None

try:
process = await asyncio.create_subprocess_exec(*command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_bytes, stderr_bytes = await asyncio.wait_for(process.communicate(), timeout=60)
stdout = stdout_bytes.decode("utf-8")
stderr = stderr_bytes.decode("utf-8")
return_code = process.returncode
except asyncio.TimeoutError:
await handle_error(request, status_code=504, detail="Server processing timed out.", returncode=process.returncode)
await handle_error(request, status_code=504, detail="Server processing timed out.", returncode=return_code)
except Exception as e:
await handle_error(request, status_code=500, detail="Internal Server Error", stderr=str(e), returncode=process.returncode)
await handle_error(request, status_code=500, detail="Internal Server Error", stderr=str(e), returncode=return_code)
else:
if process.returncode != 0:
await handle_error(request, status_code=422, detail=stderr, stderr=stderr, returncode=process.returncode)
if return_code != 0:
await handle_error(request, status_code=422, detail=stderr, stderr=stderr, returncode=return_code)

# Logging successful response
await create_and_log(request, stdout=stdout, returncode=process.returncode)
await create_and_log(request, stdout=stdout, returncode=return_code)

return {'response': stdout}

0 comments on commit 79f7dd3

Please sign in to comment.