Azure-Samples / Azure-Samples/simple-agent-functions-python

Top-level CopilotClient import crashes entire Function host if github-copilot-sdk fails to install

Đang mở
#1 2 bình luận 1 reaction 2 người được giao Được @paulyuk nhận Xem trên GitHub
Ngôn ngữ chính
Bicep
Star
4
Fork
5
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

## Problem

`function_app.py` imports `CopilotClient` and initializes it at module level:

```python
from copilot import CopilotClient, PermissionHandler
client = CopilotClient()
```

If the `github-copilot-sdk` package fails to install on Azure Functions Linux (Flex Consumption), this crashes the **entire** Function host with a 503. No endpoints load — not just the agent endpoint, but **all** HTTP triggers and **all** timer triggers become unavailable.

## Root Cause

Azure Functions Python v2 loads `function_app.py` at startup. Any top-level import failure prevents the module from loading, which prevents the Functions runtime from discovering and registering **any** routes. The host returns `Function host is not running` (503) for every request.

## Impact

- All HTTP endpoints return 503 (not just the agent endpoint)
- All timer triggers stop firing (scheduled jobs silently fail)
- No error is surfaced in the Azure Portal — the app appears deployed but is completely non-functional
- Difficult to diagnose: the deployment succeeds, the function list shows empty, and logs may not clearly indicate the import failure

## Reproduction

1. Deploy the sample to Azure Functions Flex Consumption (Python 3.12+)
2. If `github-copilot-sdk` fails to install (pip resolution error, platform incompatibility, etc.), the entire app is down
3. Observe: `curl https://.azurewebsites.net/` returns `Function host is not running`

## Suggested Fix

Make the Copilot SDK import lazy — only load when the agent endpoint is actually called:

```python
import azure.functions as func

app = func.FunctionApp()

# Lazy init — does not crash the host if copilot SDK is missing
_copilot_client = None

def _get_copilot_client():
global _copilot_client
if _copilot_client is None:
from copilot import CopilotClient
_copilot_client = CopilotClient()
return _copilot_client

@app.route(route="ask", methods=["POST"])
async def ask(req: func.HttpRequest) -> func.HttpResponse:
try:
client = _get_copilot_client()
# ... use client
except ImportError:
return func.HttpResponse("Copilot SDK not available", status_code=503)
```

This ensures that if the Copilot SDK is unavailable, only `/api/ask` fails — other endpoints and timer triggers continue to work normally.

## Additional Consideration

The same pattern applies to `from copilot.tools import define_tool` and `from pydantic import BaseModel` — these should also be lazily imported if used alongside other endpoints that should remain functional independently.

## Environment

- Azure Functions Flex Consumption (Python 3.12+)
- `github-copilot-sdk` (latest)
- Multi-endpoint function app with timer triggers + HTTP triggers

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.