a2aproject / a2aproject/a2a-python

[Feat]: Allow configuring `shutdown_grace_period` for SSE responses

Đang mở
#1,221 2 bình luận 0 reaction 1 người được giao Được @ftnext nhận Xem trên GitHub
component: server
Ngôn ngữ chính
Python
Star
2.1k
Fork
496
Merge trung bình
4 ngày 17 giờ
Pull request đã merge (30 ngày)
12

Mô tả

### Is your feature request related to a problem? Please describe.

A2A SDK v1 creates `sse-starlette` `EventSourceResponse` instances for JSON-RPC and REST streaming endpoints, but there is currently no supported way to pass `shutdown_grace_period`.

Because `sse-starlette` defaults this value to `0`, an in-progress SSE response is cancelled immediately when the server receives SIGTERM. This can prevent a finite `message/stream` request from delivering its remaining events and terminal event, even when the ASGI server is configured to wait for in-flight requests during graceful shutdown.

Relevant response creation sites include:

- `src/a2a/server/routes/jsonrpc_dispatcher.py`
- `src/a2a/server/routes/rest_dispatcher.py`

### Describe the solution you'd like

Please provide a supported way to configure `shutdown_grace_period` and pass it to every `EventSourceResponse` created by the A2A HTTP server.

For example, this could be exposed through `create_jsonrpc_routes()` and `create_rest_routes()`, through their dispatchers, or through a general `EventSourceResponse` options/factory parameter.

The existing default of `0` could be preserved for backward compatibility:

```python
routes = create_jsonrpc_routes(
request_handler,
rpc_url="/",
shutdown_grace_period=30.0,
)
```

With this configuration, a finite A2A stream should be allowed to finish naturally within the grace period before being force-cancelled.

### Describe alternatives you've considered

- Subclassing or replacing the A2A dispatchers solely to customize `EventSourceResponse`.
- Monkey-patching `EventSourceResponse`.
- Globally disabling `sse-starlette` automatic graceful drain and relying on the ASGI server timeout. This may cause indefinite streams to block shutdown and is too broad for this use case.

### Additional context

This is separate from #545. That issue concerned `send_timeout` and [was marked obsolete for the v1 implementation](https://github.com/a2aproject/a2a-python/issues/545#issuecomment-4533373962); this request concerns **cooperative shutdown after SIGTERM**.

`sse-starlette` already supports `shutdown_grace_period`,
https://github.com/sysid/sse-starlette#cooperative-shutdown
so the missing piece is a configuration path from the A2A SDK API to `EventSourceResponse`.

The configured value should be documented as being shorter than the ASGI server's graceful-shutdown timeout.

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

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

Mở hướng dẫn đóng góp

Đá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.