a2aproject / a2aproject/a2a-python

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

未关闭
#1,221 2 条评论 0 个 reaction 已指派 1 人 已被 @ftnext 认领 在 GitHub 查看
component: server
主要语言
Python
星标
2.1k
派生
496
平均合并
4 天 17 小时
30 天内合并 PR
12

描述

### 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

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。