a2aproject / a2aproject/a2a-python

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

Aperta
#1,221 2 commenti 0 reazioni 1 assegnatario Rivendicata da @ftnext Vedi su GitHub
component: server
Lingua principale
Python
Stelle
2.1k
Fork
496
Merge medio
4g 17h
PR unite (30g)
12

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.